[SOLVED] redirecting STDOUT of previous command to STDIN of next command
Linux - GeneralThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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?
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.
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
...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.