read command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read command
# 1  
Old 07-22-2005
Question read command

'Morning

Code:
vmstat 1 1|sed 1,2d|awk '{printf("%s\n",$1)}'|read var
echo $var

This syntax run on AIX (ksh) but not on linux (bash).
I think that problem is the read command, because the following syntax is ok :
Code:
vmstat 1 1|sed 1,2d|awk '{printf("%s\n",$1)}'

Could someone help me!

regards
nymus
# 2  
Old 07-22-2005
Strange, you syntax seems to be fine! I am checking the man page for bash on the net, you should not be getting an error. Just to check, can you skip the "\n" in the awk?
# 3  
Old 07-22-2005
Yeah strange, I'm getting any errors! the echo command send me only a blank line...
I've just skipping the "\n" in the awk but it's the same result!
Thanks for your help
nymus
# 4  
Old 07-22-2005
I've made new test:
This syntax doesn't run:
Code:
vmstat 1 1|sed 1,2d|awk '{printf("%s\n",$1)}'|read var
echo $var

But this syntax is running:
Code:
vmstat 1 1 |sed 1,2d |awk '{printf("%s\n",$1) }'|read var;echo $var

Why...? where's the problem
Thx
# 5  
Old 07-22-2005
FYI. From BASH Frequently-Asked Questions
Quote:
E4) If I pipe the output of a command into `read variable', why doesn't
the output show up in $variable when the read command finishes?

This has to do with the parent-child relationship between Unix
processes. It affects all commands run in pipelines, not just
simple calls to `read'. For example, piping a command's output
into a `while' loop that repeatedly calls `read' will result in
the same behavior.

Each element of a pipeline runs in a separate process, a child of
the shell running the pipeline. A subprocess cannot affect its
parent's environment. When the `read' command sets the variable
to the input, that variable is set only in the subshell, not the
parent shell. When the subshell exits, the value of the variable
is lost.

Many pipelines that end with `read variable' can be converted
into command substitutions, which will capture the output of
a specified command. The output can then be assigned to a
variable:

grep ^gnu /usr/lib/news/active | wc -l | read ngroup

can be converted into

ngroup=$(grep ^gnu /usr/lib/news/active | wc -l)

This does not, unfortunately, work to split the text among
multiple variables, as read does when given multiple variable
arguments. If you need to do this, you can either use the
command substitution above to read the output into a variable
and chop up the variable using the bash pattern removal
expansion operators or use some variant of the following
approach.

Say /usr/local/bin/ipaddr is the following shell script:

#! /bin/sh
host `hostname` | awk '/address/ {print $NF}'

Instead of using

/usr/local/bin/ipaddr | read A B C D

to break the local machine's IP address into separate octets, use

OIFS="$IFS"
IFS=.
set -- $(/usr/local/bin/ipaddr)
IFS="$OIFS"
A="$1" B="$2" C="$3" D="$4"

Beware, however, that this will change the shell's positional
parameters. If you need them, you should save them before doing
this.

This is the general approach -- in most cases you will not need to
set $IFS to a different value.

Some other user-supplied alternatives include:

read A B C D << HERE
$(IFS=.; echo $(/usr/local/bin/ipaddr))
HERE

and, where process substitution is available,

read A B C D < <(IFS=.; echo $(/usr/local/bin/ipaddr))
# 6  
Old 07-22-2005
Thanks for your reply!
But my script doesn't run with new syntax : read var << echo $(vmstat 1 1 |...)
I've read on the bash manuel that the read command is a "Bash Builtin Commands" and should running like in other shell.
What's that means? Is bash a restrictive shell...?
I'll must to change all my AIX scripts for my linux box :-( really a bad things
Regards,ny
# 7  
Old 07-22-2005
Hey r2007, thanks for that FAQ. But this works just fine from ksh. Maybe it has something to do with the manner in which forks are called by the shell..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read command

Trying to use the read command. How do you add a 2nd option? In this example I'd like to offer two options, pre and post. If you answer pre, you get one output but if you answer post, you get another output. echo Is this pre or post? read pre if then echo You have typed pre. fi (6 Replies)
Discussion started by: jimmyf
6 Replies

2. UNIX for Beginners Questions & Answers

Need help with read command

Is there a way to make the input of the read command (or some similar command that I'm unaware of) not visible, or with an astrix?? An example: #!/bin/bash # Example echo; echo "Who are you??"; read name if ; then echo "Welcome, the terminal is yours."; exit else "Stranger... (2 Replies)
Discussion started by: Huitzilopochtli
2 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Shell Programming and Scripting

read command

Hello guys, I am trying to a script that reads from key board and use the entered value in the next step. Example: enter folder name read $folder (i will give work) cd /main/$folder/ pwd it should print /main/work ---------- Post updated at 03:31 PM ----------... (1 Reply)
Discussion started by: sharath24
1 Replies

5. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

6. UNIX for Dummies Questions & Answers

Read command

Hi everyone, I have problem while writing a shell script for linux (Red Hat). First I need to create a read command. I tried to google this but so far I can't sort this out. I hope you will be able to help me. I have to read a file like this : GESTION_DATA_SET_variable1_variable2 ... (2 Replies)
Discussion started by: Aswex
2 Replies

7. Shell Programming and Scripting

read command

Is there a way to use the READ command and force the user to enter a non-zero length string? If the user enters a zero length string the user input is rejected. code: print "what is the answer: \n" read answer (2 Replies)
Discussion started by: djehresmann
2 Replies

8. Shell Programming and Scripting

read command in while

hi all iam not able use read command in the while loop in the following program while read line do echo $line echo "enter name" read name echo "your have entered $name" done < work.txt THE READ COMMAND INSIDE THE WHILE LOOP IS NOT WORKING, IS ANY OTHER WAY TO SOLVE THIS... (7 Replies)
Discussion started by: avi.skynet
7 Replies

9. Shell Programming and Scripting

Regarding read command

Hi all, What does -u option indicates in read command. while read -u var1 do . . done < file.txt (1 Reply)
Discussion started by: krishna_gnv
1 Replies

10. Shell Programming and Scripting

read command

I have file which is space filled likE below Note: here spaces are replaced by |. When I use read command to read this file all the spaces are truncated only the default space is not removed. The output is Note: here spaces are replaced by |. Can this default truncation be override? (4 Replies)
Discussion started by: COD
4 Replies
Login or Register to Ask a Question