Purpose of "read" and "$END$" in ksh ?

Hi,

Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator?

cat $AMS/version|read a b verno d
DBVer=$(/usr/bin/printf "%7s" $verno)

I checked that the cat $AMS/version command returns following output:

Application Release 12.2L01 as of Fri Nov 2 22:11:46 EDT 2007

But when it is being piped through read in the above lines, nothing is available in DBVer parameter when I output using echo $DBVer. :eek:

Also what is the purpose of $END$ as in the following code:

$AMS/bin/procmon testdb << $END$

Thanks

read takes one line, unless you muck with env I suppose, and puts the first field in the first variable and so on with the rest in the last variable. Apparently the version is the 3rd field of the first line. The cat is lazy, read <file is cheaper than a fork, pipe, exec of cat.

<< is a converter of script lines to stdin, and stops on a line with the following token only. The script lines are expanded for variables and such, so I prefer echo '...'|, where single quotes keep things literal. << copies the processed lines to a temp file, so it is the cost of a temp file create versus a pipe, echo being a builtin.

1 Like

Thanks for the reply.

1) But why it wouldn't return anything in DBVer parameter as that is empty after running those lines ?

2) So how this token $END$ is generated ? What key sequence would do that?

Regards,

"echo 1 2| read a b c" means a=1, b=2, c=, garbage in is garbage out.

It is just a string, a token made up by the author. I use <<!, if I use it at all, as it takes less space and works fine. Must be exactly and only on a line by itself. Nice to keep lists one item a line for easy maintenance and viewing, not "a b ccc ddd ee ff ggg". You deserve neat, clean, clear code.

1 Like

This is a Shell "Here" document. All the ensuing lines up to but not including the string $END$ are presented on the input read channel of the program $AMS/bin/procmon .
There should be a corresponding $END$ in column 1 a number of lines later in the script.

1 Like

Yeah, interview answer, here document. The token can be anything, 'EOF' is very popular.

The $ are a real bad token character choices, normally being used in shell and environmental variable retreivals. The make it look magic, ooooh, ooooh, Levitosa, accio broom! :smiley:

1 Like

Ok so if I try to test the example you have sent:
--------------------
#!/bin/ksh
echo 1 2 | read a b c
echo $b
--------------------

$b still has blank value instead of 2 !

Maybe the last gets the last and the first gets the first, but you can see it does not work for uncooperative data.

$ echo 1 2 | read a b c;echo $a-$b-$c
1-2-

You have a different ksh, I can see!

It looks that your ksh is not ksh. If your ksh is ksh then output is 12.2L01. Also zsh give result 12.2L01. But all other about posix shell give nothing.

echo Application Release 12.2L01 as of Fri Nov 2 22:11:46 EDT 2007 | read a b verno d
DBVer=$(/usr/bin/printf "%7s" $verno)
echo $DBVer   # echo 12.2L01

What to do ?
Download ksh.

How about

printf "%(%Y-%m-%d+%H:%M:%S)T\n" now

What is output ? If you have ksh93 then output is date.
You can do same always using method which works in all about posix shells.

read a b verno d <<EOF
$(cat  $AMS/version)
EOF
echo "verno:$verno"

read from pipe is nice but works only in ksh and zsh.

In some environment you need to look

cd /usr/bin
ls -l  ksh* dash* bash* sh*

and to look which are linked - maybe not real some shell, only about - linked.

It's behaving like old Bourne Shell not Korn Shell.
Is that shebang line the very first line in the script? Anywhere else and it will be ignored.

Seems confused, $a=cat $b=$AMS/version and verno and d are blank ! Commands in a here document are text unless run under meta like `` or $().