read command

'Morning

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 :

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

Could someone help me!

regards
nymus

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?

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

I've made new test:
This syntax doesn't run:

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

But this syntax is running:

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

Why...? where's the problem
Thx

FYI. From BASH Frequently-Asked Questions

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 :frowning: really a bad things
Regards,ny

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

Hi guys

blowtorch has perhaps right! or are my environment variables not right??
It's very strange because the only manner that the 'read command' run on my box is like so:

echo "enter a number"
read number
echo $number

Anyway.. thanks for your help
ny

Not strange at all. That is the expected result and it's explained by that FAQ article that r2007 quoted. A pipeline like:
somecommand | read variable
doesn't work because a subshell is spawned to execute that read command. That subshell sets "variable" and then it exits. The original shell now resumes and it doesn't have a copy of the value of "variable".

That syntax works in ksh because the Korn shell arranges that when the last command in a pipeline is a shell built-in command, that command is run in the context of the cuurent shell. This means that despite appearances ksh will not treat "somecommand | read variable" as true pipeline. This feature works in ksh alone. Not even pdksh supports this syntax and this is the most frequently reported pdksh "bug".

Ok, great! That's means that many commands in my ksh script will don't run in bash!
How to know the difference between the ksh and the bash shell and wich commands will products a subshell??
What's the best way to learn the bash shell because I've seen on internet some bash tutorial but they don't speak about subshell and many commands are described like ksh?
What's means "pdksh"??
:-)thanks

There's a lot of stuff that is different between bash and ksh. pdksh is a shell that was written to be as close as possible to ksh. Unlike ksh, pdksh is open source. Get a good ksh book and a good bash book and read them both. Then write a few hundred shell scripts in both languages. At that point the differences will seem clear.

Well, I've understood... :wink:
Thanks Perderabo and have a nice weekend!
nymus

be careful, there should have a blank character between the two "<"s
read var < < echo $(vmstat 1 1 |...)

Hi r2007

I'm receiving the following error with a blank character between the two "<"s
-bash: syntax error near unexpected token `<'

sorry, i am not look into your script last time. it shold like this[untested]:

read var < <(vmstat 1 1 |...)

there has a blank between tow "<"s, and no blank between "<" and "("

Out of interest ... is there any reason you cant use back quotes? For example:

var=`vmstat 1 1|sed 1,2d|awk '{printf("%s\n",$1)}'`

or slightly simpler:

var=`vmstat 1 1|sed 1,2d|awk '{print($1)}'`

Hi r2007
Great, it's running!
Thanks