# problem with read command

**URL:** <https://community.unix.com/t/problem-with-read-command/246411>\
**Category:** Shell Programming and Scripting\
**Created:** [August 6, 2009, 2:35am UTC](https://community.unix.com/t/problem-with-read-command/246411 "2009-08-06T02:35:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mac4rfree](https://community.unix.com/letter_avatar/mac4rfree/32/6_22734026db12803ebb3e059622ac3534.png) [@mac4rfree](https://community.unix.com/u/mac4rfree)\
**Post date:** [August 6, 2009, 2:35am UTC](https://community.unix.com/t/problem-with-read-command/246411/1 "2009-08-06T02:35:51Z")

</div>

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.

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [August 6, 2009, 2:55am UTC](https://community.unix.com/t/problem-with-read-command/246411/2 "2009-08-06T02:55:32Z")

</div>

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

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![mac4rfree](https://community.unix.com/letter_avatar/mac4rfree/32/6_22734026db12803ebb3e059622ac3534.png) [@mac4rfree](https://community.unix.com/u/mac4rfree)\
**Post date:** [August 6, 2009, 3:25am UTC](https://community.unix.com/t/problem-with-read-command/246411/3 "2009-08-06T03:25:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 6, 2009, 3:28am UTC](https://community.unix.com/t/problem-with-read-command/246411/4 "2009-08-06T03:28:00Z")

</div>

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

Only in ksh you can use pipe:

```nohighlight
cmd | read var1 var2 ...

```

Previous zaxxon example include howto read generic one variable:

```nohighlight
var=$( cmd )

```

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

```nohighlight
read var1 var2 < <(cmd)

```

Generic version for more than one variable read:

```nohighlight
read var1 var2 <<EOF
$(cmd)
EOF

```

---

<div class="post-metadata">

**Author:** ![mac4rfree](https://community.unix.com/letter_avatar/mac4rfree/32/6_22734026db12803ebb3e059622ac3534.png) [@mac4rfree](https://community.unix.com/u/mac4rfree)\
**Post date:** [August 6, 2009, 3:34am UTC](https://community.unix.com/t/problem-with-read-command/246411/5 "2009-08-06T03:34:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 6, 2009, 3:42am UTC](https://community.unix.com/t/problem-with-read-command/246411/6 "2009-08-06T03:42:55Z")

</div>

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![mac4rfree](https://community.unix.com/letter_avatar/mac4rfree/32/6_22734026db12803ebb3e059622ac3534.png) [@mac4rfree](https://community.unix.com/u/mac4rfree)\
**Post date:** [August 6, 2009, 3:54am UTC](https://community.unix.com/t/problem-with-read-command/246411/7 "2009-08-06T03:54:40Z")

</div>

this is the content of my file

```nohighlight
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.

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

```

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 6, 2009, 4:17am UTC](https://community.unix.com/t/problem-with-read-command/246411/8 "2009-08-06T04:17:20Z")

</div>

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

```nohighlight
#!/bin/ksh

```

and execute flag is on ?

or run it using ksh

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [August 6, 2009, 4:43am UTC](https://community.unix.com/t/problem-with-read-command/246411/9 "2009-08-06T04:43:20Z")

</div>

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:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [August 6, 2009, 5:04am UTC](https://community.unix.com/t/problem-with-read-command/246411/10 "2009-08-06T05:04:00Z")

</div>

Works fine on my AIX box with ksh.

```nohighlight
$ 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 :

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

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![mac4rfree](https://community.unix.com/letter_avatar/mac4rfree/32/6_22734026db12803ebb3e059622ac3534.png) [@mac4rfree](https://community.unix.com/u/mac4rfree)\
**Post date:** [August 6, 2009, 5:15am UTC](https://community.unix.com/t/problem-with-read-command/246411/11 "2009-08-06T05:15:42Z")

</div>

Thanks for your replies guys

@kshji, i have the first line as

```nohighlight
#!/usr/bin/ksh

```

Earlier i was running it as

```nohighlight
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

```nohighlight
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.

```nohighlight
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..

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 6, 2009, 5:49am UTC](https://community.unix.com/t/problem-with-read-command/246411/12 "2009-08-06T05:49:20Z")

</div>

If you like to test script with ksh then call

```nohighlight
ksh script

```

not ex. sh.

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

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

```

---

<div class="post-metadata">

**Author:** ![mac4rfree](https://community.unix.com/letter_avatar/mac4rfree/32/6_22734026db12803ebb3e059622ac3534.png) [@mac4rfree](https://community.unix.com/u/mac4rfree)\
**Post date:** [August 6, 2009, 9:29am UTC](https://community.unix.com/t/problem-with-read-command/246411/13 "2009-08-06T09:29:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 6, 2009, 9:41am UTC](https://community.unix.com/t/problem-with-read-command/246411/14 "2009-08-06T09:41:10Z")

</div>

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.

```plaintext
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](http://www.unix.com/showthread.php?t=116242&goto=newpost)
