Linux and Korn Shell Question - Questions

This is a discussion on Linux and Korn Shell Question - Questions ; I'm transferring a script from an IBM AIX box to a Red Hat Enterprise Linux server. One of the first lines in the script determines the logged in user and terminal the script is running under. The syntax is: who ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Linux and Korn Shell Question

  1. Linux and Korn Shell Question

    I'm transferring a script from an IBM AIX box to a Red Hat Enterprise
    Linux server. One of the first lines in the script determines the
    logged in user and terminal the script is running under. The syntax is:

    who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal

    The first part give the standard output for the "who am i" command, the
    first field being the user and the second the terminal. The "awk"
    command prints just the user and terminal with the idea that the output
    will be piped into the read command, giving me shell variables for the
    user and the terminal. This line works fine under AIX and the Korn
    shell. On Linux, each piece works fine at the CLI, but the entire line
    doesn't give me any values for the user and terminal variables. I
    believe the issue is within the pipe. Is there some parameter, switch
    or something that needs to be different under LInux? The begining of
    the script is forcing the shell to the korn shell (/bin/ksh) and I've
    verified that. Any help would be appreciated.

    Michael R. James
    dritzz721@verizon.net

  2. Re: Linux and Korn Shell Question

    On 2006-09-12, Michael James wrote:
    > I'm transferring a script from an IBM AIX box to a Red Hat Enterprise
    > Linux server. One of the first lines in the script determines the
    > logged in user and terminal the script is running under. The syntax is:
    >
    > who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal
    >
    > The first part give the standard output for the "who am i" command, the
    > first field being the user and the second the terminal. The "awk"
    > command prints just the user and terminal with the idea that the output
    > will be piped into the read command, giving me shell variables for the
    > user and the terminal. This line works fine under AIX and the Korn
    > shell. On Linux, each piece works fine at the CLI, but the entire line
    > doesn't give me any values for the user and terminal variables. I
    > believe the issue is within the pipe. Is there some parameter, switch
    > or something that needs to be different under LInux? The begining of
    > the script is forcing the shell to the korn shell (/bin/ksh) and I've
    > verified that. Any help would be appreciated.


    In most shells, each element in a pipeline is run in a subshell,
    and thus cannot change variables in the calling process. Korn shell
    is different in that it runs the last element in a pipeline in the
    current shell.

    However, if you want to run the script with bash (or even with ksh)
    you can do it more efficiently by changing that line to:

    set -- `who am i`
    user=$1
    terminal=$2

    However, it still may not work, depending on how the user is logged
    in. If I am logged directly into X through a display manager, 'who
    am i' does not display anything. Try typing 'who am i' at the
    command line to see whether you get any output.

    You can also get that information with the 'whoami' (one word, not
    three) and 'tty' commands. The user name should also be in $USER
    and/or $LOGNAME.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  3. Re: Linux and Korn Shell Question



    Michael James wrote:

    > I'm transferring a script from an IBM AIX box to a Red Hat Enterprise
    > Linux server. One of the first lines in the script determines the
    > logged in user and terminal the script is running under. The syntax is:
    >
    > who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal
    >
    > The first part give the standard output for the "who am i" command,
    > the first field being the user and the second the terminal. The "awk"
    > command prints just the user and terminal with the idea that the
    > output will be piped into the read command, giving me shell variables
    > for the user and the terminal. This line works fine under AIX and the
    > Korn shell. On Linux, each piece works fine at the CLI, but the
    > entire line doesn't give me any values for the user and terminal
    > variables. I believe the issue is within the pipe. Is there some
    > parameter, switch or something that needs to be different under
    > LInux? The begining of the script is forcing the shell to the korn
    > shell (/bin/ksh) and I've verified that. Any help would be appreciated.


    This works fine on my Fedora Core 5 installation with ksh version
    "1993-12-28 r". It doesn't work when I use bash. Can you show us the
    script and the crontab entry, or how you are trying to run it from the
    command line?


  4. Re: Linux and Korn Shell Question

    On Tue, 12 Sep 2006 14:01:32 -0400
    Steve Cousins wrote:

    >
    >
    > Michael James wrote:
    >
    > > I'm transferring a script from an IBM AIX box to a Red Hat
    > > Enterprise Linux server. One of the first lines in the script
    > > determines the logged in user and terminal the script is running
    > > under. The syntax is:
    > >
    > > who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal
    > >
    > > The first part give the standard output for the "who am i" command,
    > > the first field being the user and the second the terminal. The
    > > "awk" command prints just the user and terminal with the idea that
    > > the output will be piped into the read command, giving me shell
    > > variables for the user and the terminal. This line works fine
    > > under AIX and the Korn shell. On Linux, each piece works fine at
    > > the CLI, but the entire line doesn't give me any values for the
    > > user and terminal variables. I believe the issue is within the
    > > pipe. Is there some parameter, switch or something that needs to
    > > be different under LInux? The begining of the script is forcing
    > > the shell to the korn shell (/bin/ksh) and I've verified that. Any
    > > help would be appreciated.

    >
    > This works fine on my Fedora Core 5 installation with ksh version
    > "1993-12-28 r". It doesn't work when I use bash. Can you show us
    > the script and the crontab entry, or how you are trying to run it
    > from the command line?
    >

    yes, because FC5 uses the original ksh while rhel installs by default
    the pdksh version, which in some ways behaves sometimes different from
    the original ksh. In the meantime, there is the original ksh available
    for rhel (for 3 at least since UP3) and you should try that, because it
    will make the migration of AIX environments and scripts to Linux much
    more easy: 'It is useful in circumstances where precise compatibility
    with AT&T ksh semantics is required'.

    Have a look at:
    http://www.redhat.com/docs/manuals/e...U3-x86-en.html

    hth
    Georg

  5. Re: Linux and Korn Shell Question

    On 2006-09-12, Michael James wrote:
    > I'm transferring a script from an IBM AIX box to a Red Hat Enterprise
    > Linux server. One of the first lines in the script determines the
    > logged in user and terminal the script is running under. The syntax is:
    >
    > who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal


    Let me check ...

    echo this that | read a b
    fails

    echo this that > A ; read a b < A
    works

    echo this that |& read -p a b
    works

    (but I can't get the coprocess, "|&" to work in your example)

    So... there seems to be a problem with the read.

    So ... let's drop it.

    As "who am i" outputs a list of data, let's put it into an
    array and grab the desired data.
    set -A mytemp `who am i`; user="${mytemp[0]}"; terminal="${mytemp[1]}"; unset mytemp

    Does that do it?

  6. Re: Linux and Korn Shell Question

    On 2006-09-13, Spamless wrote:
    > On 2006-09-12, Michael James wrote:
    >> I'm transferring a script from an IBM AIX box to a Red Hat Enterprise
    >> Linux server. One of the first lines in the script determines the
    >> logged in user and terminal the script is running under. The syntax is:
    >>
    >> who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal

    >
    > Let me check ...
    >
    > echo this that | read a b
    > fails
    >
    > echo this that > A ; read a b < A
    > works
    >
    > echo this that |& read -p a b
    > works
    >
    > (but I can't get the coprocess, "|&" to work in your example)
    >
    > So... there seems to be a problem with the read.
    >
    > So ... let's drop it.
    >
    > As "who am i" outputs a list of data, let's put it into an
    > array and grab the desired data.
    > set -A mytemp `who am i`; user="${mytemp[0]}"; terminal="${mytemp[1]}"; unset mytemp
    >
    > Does that do it?


    The above is using Fedora Core2 (so, your mileage may vary).

  7. Re: Linux and Korn Shell Question

    On Tue, 12 Sep 2006 14:17:12 GMT, Michael James
    wrote:

    >I'm transferring a script from an IBM AIX box to a Red Hat Enterprise
    >Linux server. One of the first lines in the script determines the
    >logged in user and terminal the script is running under. The syntax is:
    >
    >who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal
    >
    >The first part give the standard output for the "who am i" command, the
    >first field being the user and the second the terminal. The "awk"
    >command prints just the user and terminal with the idea that the output
    >will be piped into the read command, giving me shell variables for the
    >user and the terminal. This line works fine under AIX and the Korn
    >shell. On Linux, each piece works fine at the CLI, but the entire line
    >doesn't give me any values for the user and terminal variables. I
    >believe the issue is within the pipe. Is there some parameter, switch
    >or something that needs to be different under LInux? The begining of
    >the script is forcing the shell to the korn shell (/bin/ksh) and I've
    >verified that. Any help would be appreciated.
    >
    >Michael R. James
    >dritzz721@verizon.net


    I'm on RedHat ES4, but I removed the pdksh and installed the real ksh
    that is also part of the release. The script works for me:

    # rpm -q ksh
    ksh-20050202-0.4E.2

    # who am i | awk '{ printf "%s %s\n", $1, $2; } ' | read user terminal
    # echo $user
    scooper
    # echo $terminal
    pts/0

    Steve

+ Reply to Thread