Help with UNIX READ command usage..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with UNIX READ command usage..
# 1  
Old 02-07-2013
Help with UNIX READ command usage..

Tested on : bash
Will be implementing on : ksh

I dont know if this is weird , or my idea of Unix stdin or stdout is completely messed up , but if I use the following command, I am getting a proper output.
Code:
ls -l | head -1 | while read a ; do echo $a ;done

and the output is soemthing like total 32

But, on the contrary , if I use
Code:
ls -l | head -1 | read a ; echo $a

it yields an empty value

My idea was , as because the commands are piped , the output of ls command will flow to standard input and will be read to the variable a.
But , its happening only when I used the while-read loop , and not the stand alone read .

Am I missing something here ? Please help.



Thanks
Kumarjit.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 02-07-2013 at 06:26 AM.. Reason: fixed code tags
# 2  
Old 02-07-2013
In most shells read a is executed in a subshell. This means that once the pipeline is finished and the subshell with read a is finished, the value of variable $a is not retained. There are various ways to get around this, but the most straight forward in this case is the use of command substitution: a=$( .. )
# 3  
Old 02-07-2013
cmd | read a

You can think it like it looks or how it works in memory.
I'm happy that Korn has thinked as the pipe syntax idea has been. Even you need make some shared mem using to offer the result.

So use ksh if you like to use cmd | read variable(s).
Code:
# works only in ksh, not in Bourne shell or bash or dash or ...
ls -l | head -1 | read a 
echo $a

# pipe is only stdout-stdin channel between process. => read has also own env. So read set variable and die.
# Korn idea is to support how it looks => a is global variable, child process read and set value to the a.

# works in every sh-shells:
read <<EOF
$(ls -l | head -1 )
EOF
echo $a

# or
a=$(ls -l | head -1 )
echo $a

This example show the difference between ideas: (do it using bash and ksh).
Code:
a=1000
ls -l | head -1 | read a
echo $a

Next example is nice:
Code:
f=1111
ls -1 | while read f
do
        echo $f
done
echo "F $f"

Do it using bash, dash, ksh.
# 4  
Old 02-07-2013
Always avoid unsafe/complex here documents
Code:
read a <<EOF
`ls -l | head -1`
EOF
echo $a

Instead do
Code:
a=`ls -l | head -1`
echo $a

$(...) is Posix, old Unix shells need `...`
Posix allows nested $(... $(...)).

As a last less good alternative you can place the echo in an explicit subshell:
Code:
ls -l | head -1 | (read a; echo $a)

# 5  
Old 02-07-2013
I'm not sure why this should be less acceptable. Would you mind to explain? At least you could do some shell computing with the variable read and output the result:
Code:
$ ls -l | head -1 | { read a; compute; sth; different; echo $result; }

# 6  
Old 02-07-2013
Rule of thumb:
do the most in the main shell.
The exception proves the rule.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read all files in a directory for a unix command

Hello, Below, I have a unix command, which can be executable for single file. cat input.txt | sort -k3,3 > output.txt I have 100 input files in a directory. It is hectic and time taking to run the above command for all the 100 files for 100 times. Now, I want to execute the above unix... (2 Replies)
Discussion started by: koneru_18
2 Replies

2. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

3. Shell Programming and Scripting

Unix - Disk usage command

Hi, I need a command that gives me the user who is using more space in the unix box (1 Reply)
Discussion started by: abinaya
1 Replies

4. UNIX for Dummies Questions & Answers

Unix Top Command and sorting by CPU Usage

Ok, so I am using the Top command on my linux VPS to try and see the processes using the most CPU %. I hit the P to sort by CPU % but it wants to sort them from lowest to highest (ascending). My Telnet-SSH screen is only about 60 rows high so the processes with the highest CPU % usage are at the... (6 Replies)
Discussion started by: davemehta
6 Replies

5. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

6. UNIX for Dummies Questions & Answers

Disk Usage in GB and Unix command to find the biggest file/folder

Hi All, Please help me out 1) Command to find the disk usage in GB. I know that du -k will give in kilobites. 2) How to find the Biggest file/folder in a given set of files/folders. Thanks in advance Regards, Manas (8 Replies)
Discussion started by: manas6
8 Replies

7. UNIX for Dummies Questions & Answers

need help on how to read command in unix

can any one please help me how to use "read" command for openig a file.i want to process the file record by record and then needs to search for a particular string at a particular position.can any one please let me know the code.i am new to the shell. below are sample sample records ... (3 Replies)
Discussion started by: raoscb
3 Replies

8. UNIX for Advanced & Expert Users

unix top command (memory usage)

in unix when i use top i get an output like this: load averages: 0.64, 0.57, 0.53 14:04:42 347 processes: 1 running, 1 waiting, 169 sleeping, 172 idle, 4 stopped CPU states: 16.4% user, 2.8% nice, 7.6%... (2 Replies)
Discussion started by: gfhgfnhhn
2 Replies

9. UNIX for Dummies Questions & Answers

disk usage command on unix?

hi, Can anyone tell me how I make a tree on the console showing the largest folders on my UNIX system? I want to know where are located the biggest files so I can free some disk space. thanks a lot. (3 Replies)
Discussion started by: gandoura
3 Replies

10. Shell Programming and Scripting

AWK : read unix command

Hi, Is there a way to have something working like the read Unix command to stop a awk ask a question and get the result. I've try system("read a") but it does not work. Thanks. Franck. (6 Replies)
Discussion started by: fwirbel
6 Replies
Login or Register to Ask a Question