-   Linux - General (
-   -   redirecting STDOUT of previous command to STDIN of next command ( 11-20-2009 09:51 PM

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:


exec 7<&0
df <&1 | grep sda1 | awk '{ print $3, $4 }'
read used free

GrapefruiTgirl 11-20-2009 10:01 PM

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? :scratch:

Sasha 11-20-2009 10:10 PM

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


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.

lwasserm 11-20-2009 11:21 PM


line=($(df|grep sda1))

GrapefruiTgirl 11-20-2009 11:25 PM


Originally Posted by lwasserm (Post 3764614)

line=($(df|grep sda1))

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

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?

i92guboj 11-20-2009 11:45 PM

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.


$ array=($(echo foo bar moo cow))
$ echo ${#array}
$ for i in $(seq 0 ${#array}); do echo ${array[$i]}; done

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

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


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 :


command <(command)
grep ^d <(ls -l)

but it doesn't seem to work with the read command....

lwasserm 11-21-2009 09:33 AM


Originally Posted by (Post 3764917)
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

cool thanks for the response, that cleared things up

All times are GMT -5. The time now is 06:15 AM.