LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   redirecting STDOUT of previous command to STDIN of next command (http://www.linuxquestions.org/questions/linux-general-1/redirecting-stdout-of-previous-command-to-stdin-of-next-command-770546/)

urban.yoga.journeys 11-20-2009 10: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:

Code:

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


GrapefruiTgirl 11-20-2009 11: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

urban.yoga.journeys 11-20-2009 11: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

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.

lwasserm 11-21-2009 12:21 AM

Code:

line=($(df|grep sda1))
used=${line[2]}
free=${line[3]}


GrapefruiTgirl 11-21-2009 12:25 AM

Quote:

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

urban.yoga.journeys 11-21-2009 12:38 AM

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-21-2009 12:45 AM

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.

urban.yoga.journeys 11-21-2009 09: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

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

lwasserm 11-21-2009 10:33 AM

Quote:

Originally Posted by urban.yoga.journeys (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.

urban.yoga.journeys 11-22-2009 04:16 AM

cool thanks for the response, that cleared things up


All times are GMT -5. The time now is 05:23 AM.