Operating System - Linux
1828932 Members
2593 Online
109986 Solutions
New Discussion

Re: std in & read problem

 
amit singh_3
Advisor

std in & read problem

hey ,

I am trying following command on HP-UX,
#date |cut -d " " -f 2,3,6 |read var
#echo $var
& it is showing me the value of filtered
date command.

But when I am trying the following command on
Linux ,
#echo $var ,is not showing any things.
why?

Amit singh
7 REPLIES 7
Robert Binkhorst
Trusted Contributor

Re: std in & read problem

Hi,

You're using a different shell on Linux (Bash) than on HP-UX (ksh). You can install pdksh on Linux to also get a korn shell (ksh) there, or you could look into bash. Follow this link for more info: http://www.tldp.org/LDP/abs/html/

Anyway, try this one:
# var=`date |cut -d " " -f 2,3,6`
# echo $var

HTH,

Robert
linux: the choice of a GNU generation
Mark Grant
Honored Contributor

Re: std in & read problem

I like what you are trying to do but you are going about it the wrong way. "date" has a "format" field which will output the date or bits of the date any how you like.

var=`date +"%d %m %y"`

Will get you what you want.

The reason your thing didn't work on Linux is because thre is no field 6 from "date" on linux .

THe above command will work on HPUX and Linux
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: std in & read problem

Ooops. There is a field 6 in the Linux "date".

However, "read" on Linux does not read from a pipe. It should do though *scratches head*, but doesn't.
Never preceed any demonstration with anything more predictive than "watch this"
Stuart Browne
Honored Contributor

Re: std in & read problem

pdksh behaves in the same manner.

In both bash and ksh, you can also do the var=$(cut -d " " -f2,3,6) method.

What's odd is that while 'cut -d " " -f 2,3,6 | read var' doesn't work, the following does:

cut -d " " -f 2,3,6 | while read var; do echo $var;done

I'm still working on trying to figure *that* one out..

I don't have a HPUX box handy, does anybody have what the IFS is for that? ("echo $IFS | od -x" or thorugh hexdump or something).

Both bash and ksh work with the while in there. My thought is it has something to do with the IFS, but.. need more experimentation.
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: std in & read problem

I poked a SCO OSR box, and it behaves in the same manner as the HP-UX shell mentioned above.

The IFS is the same as the Linux box..

The mystery continues..
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: std in & read problem

Ahh, and here's the kicker!

This has to do with the way bash/pdksh deal with pipelined commands.

From the man page of 'bash':

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

Translation:

As the 'read' is in a pipe, a sub-shell is created. The sub-shell thus has a private environment space, and can't be propergated back *up* the environment tree to make it into your current environment.

So, either using "var=`command`", or "var=$(command)" are the only ways to fill a variable from the output of another command.

*Wheee!* :) love a good mystery ;)
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: std in & read problem

And the continuation of a good mystery, is a solution!

Whee!!

Reading through the advanced bash scripting guide (whoa, head spin), it brings up this lovely solution..

It took me 3-4 reads to understand what the hell was going on, but once figured out, it makes sense..

Anyway!:

read var < <(date | cut -d " " -f 2,3,6) ; echo $var

This introduces the variable 'var' to to the current environment, not to a subshell as it would using |'s.

This is a 'bash' solution, and am unsure if it would work in a 'pdksh' or posix 'ksh' environment.

Hope this helps! :)

(Note: brief explanation..

<(COMMAND)

Basically it says that the output of 'command' will get thrown to a file descriptor. As the above does a '< <(COMMAND)', it says take the output of 'COMMAND', and shove it into the STDIN of the read. Thus you get the effect of a pipe, without creating a subshell.)
One long-haired git at your service...