Discussion:
Linux and Korn Shell Question
(too old to reply)
Michael James
2006-09-12 14:17:12 UTC
Permalink
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
***@verizon.net
Chris F.A. Johnson
2006-09-12 18:38:26 UTC
Permalink
Post by Michael James
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
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 | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Steve Cousins
2006-09-12 18:01:32 UTC
Permalink
Post by Michael James
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
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?
Georg Klein
2006-09-12 19:42:01 UTC
Permalink
On Tue, 12 Sep 2006 14:01:32 -0400
Post by Steve Cousins
Post by Michael James
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
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/enterprise/RHEL-3-Manual/release-notes/as-x86/RELEASE-NOTES-U3-x86-en.html

hth
Georg
Spamless
2006-09-13 09:40:33 UTC
Permalink
Post by Michael James
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
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?
Spamless
2006-09-13 09:42:12 UTC
Permalink
Post by Spamless
Post by Michael James
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
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).
Steve Cooper
2006-09-14 10:59:37 UTC
Permalink
Post by Michael James
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
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
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

Loading...