LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
Search this Thread
Old 11-20-2009, 09:51 PM   #1
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
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
 
Old 11-20-2009, 10:01 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 11-20-2009, 10:10 PM   #3
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
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.
 
Old 11-20-2009, 11:21 PM   #4
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Code:
line=($(df|grep sda1))
used=${line[2]}
free=${line[3]}
 
Old 11-20-2009, 11:25 PM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by lwasserm View Post
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
 
Old 11-20-2009, 11:38 PM   #6
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
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?
 
Old 11-20-2009, 11:45 PM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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.
 
Old 11-21-2009, 08:34 AM   #8
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
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....
 
Old 11-21-2009, 09:33 AM   #9
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Quote:
Originally Posted by urban.yoga.journeys View Post
...
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.
 
Old 11-22-2009, 03:16 AM   #10
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
cool thanks for the response, that cleared things up
 
  


Reply

Tags
command, redirect, stdin, stdout


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tar, stdin or stdout evil_empire Linux - Newbie 3 06-20-2009 06:43 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
stdout stdin Furlinastis Linux - Newbie 3 08-11-2005 11:00 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM
redirecting stdin and stdout with pipes: convincing apps they're for a terminal? prell Programming 1 09-02-2004 06:38 AM


All times are GMT -5. The time now is 08:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration