- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: std in & read problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2004 07:46 PM
02-29-2004 07:46 PM
std in & read problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2004 08:24 PM
02-29-2004 08:24 PM
Re: std in & read problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2004 08:27 PM
02-29-2004 08:27 PM
Re: std in & read problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2004 08:30 PM
02-29-2004 08:30 PM
Re: std in & read problem
However, "read" on Linux does not read from a pipe. It should do though *scratches head*, but doesn't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2004 11:00 AM
03-01-2004 11:00 AM
Re: std in & read problem
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2004 11:19 AM
03-01-2004 11:19 AM
Re: std in & read problem
The IFS is the same as the Linux box..
The mystery continues..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2004 11:44 AM
03-01-2004 11:44 AM
Re: std in & read problem
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 ;)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2004 04:23 PM
03-10-2004 04:23 PM
Re: std in & read problem
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.)