Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-20-2009, 09:51 PM
|
#1
|
|
Member
Registered: Jun 2008
Posts: 31
Rep:
|
redirecting STDOUT of previous command to STDIN of next command
i'm trying to redirect the output of a command to the input of the next command. not sure if i'm going about this the right way. an easy method would be just to store the output of the previous command in a file and redirect input to read that file, but i'm curious to see if this can be done without writing to any files.
my code:
Code:
exec 7<&0
df <&1 | grep sda1 | awk '{ print $3, $4 }'
read used free
|
|
|
|
11-20-2009, 10:01 PM
|
#2
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
This is reminiscent of another post of yours; I don't quite understand the purpose of your exec statement there, nor of the <&1 item, but simply using the pipe will do what you asked (assuming there's a /dev/sda1):
df | grep sda1 | awk '{ print $3, $4 }'
stdout from df will automatically go through the pipe into grep, whose stdout then goes into the awk command.
And, what are you trying to do with the read statement? Assign the output from awk to those two variables?
Sasha
|
|
|
|
11-20-2009, 10:10 PM
|
#3
|
|
Member
Registered: Jun 2008
Posts: 31
Original Poster
Rep:
|
yep that's exactly what i'm trying to do. awk will output two variables, which i'm trying to assign to $used and $free.
i realize simple thing to so would be just to do
Code:
df | grep sda1 | awk '{ print $3, $4 }' > file
exec < file
read used free
but i want to see if this can be done without redirecting STDOUT to a file.
|
|
|
|
11-20-2009, 11:21 PM
|
#4
|
|
Member
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184
Rep:
|
Code:
line=($(df|grep sda1))
used=${line[2]}
free=${line[3]}
|
|
|
|
11-20-2009, 11:25 PM
|
#5
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by lwasserm
Code:
line=($(df|grep sda1))
used=${line[2]}
free=${line[3]}
|
Now that's nice  -- I had no idea. BTW, have you looked at the man page for `line`? Shortest man-page I've ever seen.
Aaaaalways learning something new around here!
Sasha
|
|
|
|
11-20-2009, 11:38 PM
|
#6
|
|
Member
Registered: Jun 2008
Posts: 31
Original Poster
Rep:
|
hey thanks a lot.
so i guess no way to do this with the standard redirection (exec <& )unless you're redirecting to a file.
according to the man pages line reads one line, so does this mean it can only read the first line of the output? no way to redirect it to the 2nd, 3rd line, etc?
|
|
|
|
11-20-2009, 11:45 PM
|
#7
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,965
|
That's not related to the "line" tool, instead it's at bash arrays where you should be looking, in the bash man page, which is what the var=(<list>) syntax does: create an array.
Code:
$ array=($(echo foo bar moo cow))
$ echo ${#array}
3
$ for i in $(seq 0 ${#array}); do echo ${array[$i]}; done
foo
bar
moo
cow
The purpose of the code by lwasserm is exactly the same, it takes a line, turns it into an array, then you can reference without problems the second, third or n-th element of the array, up to ${#array} which is the higher index available.
|
|
|
|
11-21-2009, 08:34 AM
|
#8
|
|
Member
Registered: Jun 2008
Posts: 31
Original Poster
Rep:
|
someone over at the ubuntu forums posted this solution, process substitution AFAIK pipes the STDOUT directly to STDIN, so this is probably the solution i was looking for
Code:
read used free < <(df | grep sda1 | awk '{ print $3, $4 }') ; echo $used $free
2855260 23362784
i'm curious about the syntax though, why are two < needed? i thought syntax was :
Code:
command <(command)
grep ^d <(ls -l)
but it doesn't seem to work with the read command....
|
|
|
|
11-21-2009, 09:33 AM
|
#9
|
|
Member
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184
Rep:
|
Quote:
Originally Posted by urban.yoga.journeys
...
i'm curious about the syntax though, why are two < needed?
|
'<(command)' takes the output of command and essentially places it in a temporary file named '<(command)' Since 'read' acts on standard input, the leading '<' is necessary to redirect the contents of '<(command)' to the standard input of the read command.
Note that this is not the same as using '<<' at all. There must be a space between "read <" and "<(command)" or there will be an error.
It can get confusing at times. Many of the bash operators are overloaded (in the C++ sense) and are used for different things in different contexts. I find myself going back to the man page or guide very often when writing any kind of halfway complicated script.
|
|
|
|
11-22-2009, 03:16 AM
|
#10
|
|
Member
Registered: Jun 2008
Posts: 31
Original Poster
Rep:
|
cool thanks for the response, that cleared things up
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:53 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|