The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read command COD Shell Programming and Scripting 4 04-26-2008 01:49 PM
help with READ command fiol73 UNIX for Dummies Questions & Answers 2 12-15-2006 11:13 AM
Read Command Woodbird25 UNIX for Dummies Questions & Answers 2 03-10-2004 02:06 PM
Cat and read command mango Shell Programming and Scripting 3 12-29-2003 10:55 AM
while using read command I want too..... nhatch UNIX for Dummies Questions & Answers 1 08-08-2002 05:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-22-2005
Registered User
 

Join Date: Apr 2005
Posts: 47
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
Reply With Quote
Forum Sponsor
  #2  
Old 07-22-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
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?
Reply With Quote
  #3  
Old 07-22-2005
Registered User
 

Join Date: Apr 2005
Posts: 47
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
Reply With Quote
  #4  
Old 07-22-2005
Registered User
 

Join Date: Apr 2005
Posts: 47
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
Reply With Quote
  #5  
Old 07-22-2005
r2007's Avatar
Registered User
 

Join Date: Jun 2005
Location: China
Posts: 73
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))
Reply With Quote
  #6  
Old 07-22-2005
Registered User
 

Join Date: Apr 2005
Posts: 47
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
Reply With Quote
  #7  
Old 07-22-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
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..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0