problem with read command

Hi Guys,

I am just trying to read data from a file.
The command i tried worked well with AIX server. But in SunOS, it is not reading.

cat log.out |sed -n '4,4p'| read value;
+ sed -n 4,4p
+ cat log.out
+ read value
echo $value;
+ echo

This is the log of running the script.

Can you people help me in figuring it out.. and also i want to know how the same command which worked on AIX is not working in SunOS.

Thanks & Regards,
Magesh

I guess you are just going to get the 4th line.
The cat is not needed since sed can read files on it's own.

In bash you can do this

read VAR < <(sed '4!d' infile)
echo $VAR
test-9876

This should work in most shells and would also need no read:

VAR=`sed '4!d' infile`
echo $VAR

zaxxon do we need to have two "<" in the code?

We have Posix-standard and then we have real world :slight_smile: - people who has done the shells.

Only in ksh you can use pipe:

cmd | read var1 var2 ...

Previous zaxxon example include howto read generic one variable:

var=$( cmd )

Previous zaxxon example include howto read more than one variable using bash.

read var1 var2 <  <(cmd)

Generic version for more than one variable read:

read var1 var2 <<EOF
$(cmd)
EOF

KShJi, i am using a ksh only..

But still why i am not able to pass the value to the read command ?

Can you please explain me?

Thanks in advance

cat log.out | sed -n '4,4p'| read value
echo "$value"

Works fine with ksh88 and ksh93. Just tested - echo something, if log.out has more than 3 lines.

this is the content of my file

tcmsd003:/usr/local/dsadm/dsprod/src>cat log.out

  COUNT(*)
----------
         1

Actually from the unix prompt if i run the same sed command, i am getting the desired output, in this case 1. But i have problem in the script only

---------- Post updated at 01:24 PM ---------- Previous update was at 01:23 PM ----------

This is the output of my sed command.

tcmsd003:/usr/local/dsadm/dsprod/src>sed -n '4,4p' log.out
         1

Are you sure that your script has done using ksh ? What is your SHELL variable value ?
1st line:

#!/bin/ksh

and execute flag is on ?

or run it using ksh

ksh script

---------- Post updated at 11:17 AM ---------- Previous update was at 11:14 AM ----------

Script without firstline #!programpath and execute flag is on and you give scriptname, then
script has done using variable SHELL value. Always programpath with correct options in the first line in scriptfile. Awk, sh, sed, perl, python, ...

Safety testing always:
interpreter scriptfile

As said, the < is just when using bash in this example.
Also as said, leave out the useless cat. The most tools can read files on their own and don't need it to be cat'ed & piped.

Use for ksh:

VAR=`awk 'NR == 4 {print $1}' infile`
echo ${VAR}

I changed to awk so you get rid of leading spaces this way.
Try it in your script and tell if it does not work.

Works fine on my AIX box with ksh.

$ cat read.ksh
#!/usr/bin/ksh

sed -n '4{;p;q;}' log.out | read value
echo "Value is '$value'"

$ cat log.out

  COUNT(*)
----------
         1

$ read.ksh
Value is '1'
$ 

If the value that you want to read is the last one in the file, you can replace your sed command by this awk command :

awk 'NF {v=$1} END {print v}' log.out

Jean-Pierre.

Thanks for your replies guys

@kshji, i have the first line as

#!/usr/bin/ksh

Earlier i was running it as

sh -vx script_name

.

At that time, it was not able to read the value. But after i used ksh, i was able to read the value.
But still it throws an error. the error is

test1.ksh[20]: 4,4p: is not an identifier

@Jean-Pierrre, the script runs fine in AIX. But it is throwing a problem in SunOS. I have two servers.

---------- Post updated at 02:45 PM ---------- Previous update was at 02:39 PM ----------

Actually i was using << before read command, then i replaced it with <. But now it throws an another error. Please see the log below.

read value < sed -n '4,4p' log.out;
+ read value -n 4,4p log.out
+ test1.ksh[20]: sed: cannot open

Please let me know where i am going wrong..

If you like to test script with ksh then call

ksh script

not ex. sh.

This works with ksh, bash, zsh and all other posix-sh

value=$(sed -n "4,4p" log.out)
echo "$value"
EOF

cool that worked like charm.. i think i was just trying to understand why we are not able to use "|" in the SunOS. Anyways thanks for your help.. Thanks all

SunOS include same command many times, different version (many other *nix has same status).
There is own old binary = compatible, xpg4 spec version, u95 version, gnu, posix, ...

I think, if you try find, you'll find more than one ksh.

find / -name ksh
find / -name sed

Try | using with all those ksh.

---------- Post updated at 04:41 PM ---------- Previous update was at 04:39 PM ----------

If you like to use latest ksh, then
download from ATT