Results 1 to 7 of 7
So i read that pipe symbol helps redirect the output of one command as an input to another command.. like
Code:
ls grep 'Something'
Anyways i know the command which ...
- 01-15-2012 #1Just Joined!
- Join Date
- Oct 2011
- Posts
- 67
Help using the pipe symbol
So i read that pipe symbol helps redirect the output of one command as an input to another command.. like
Anyways i know the command which dispays a directory of the utilityCode:ls grep 'Something'
like
which gcc would display
usr/bin/gcc
and
dirname 'usr/bin/gcc' would display
usr/bin
So how can i mix those commands up so that the output of which command serves as an input to the dirname command . I tried the following combinations but couldnt get it to work
Any suggestions ???Code:[zedan@localhost bin]$ which gcc | dirname dirname: missing operand Try `dirname --help' for more information. [zedan@localhost bin]$ dirname | which gcc /usr/bin/gcc dirname: missing operand Try `dirname --help' for more information.
- 01-15-2012 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
you've almost got it, but in this case, you don't actually need a pipe.
The dirname command wants an argument passed to it (i.e., a filename with a path), not STDIN. So you'd go:
or, alternatively:Code:dirname `which gcc`
the 2nd form is better as it can nest.Code:dirname $(which gcc)
- 01-15-2012 #3
A pipe acts kind of like a connector for two programs.
Linux pipe tutorial | Linux basic configurations
Tips For Linux - Pipes - Get the most out of your shellJay
New users, read this first.
New Member FAQ
Registered Linux User #463940
I do not respond to Private Messages asking for Linux help. Please, keep it on the public boards.
- 01-15-2012 #4Just Joined!
- Join Date
- Oct 2011
- Posts
- 67
I did read some tutorials on the Linux pipes. However the $ thing seems to make sense. This might sound a bit weird since i am asking this question without doing any kind of research but how do u we know know when to use a pipe or a $. The $ makes sense since it just treats it like a command
dirname $(which gcc)
however the pipe seems to confuse me like
ls | grep 'SearchString'
does placing a pipe after a command make it an input or what ??
- 01-15-2012 #5Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
When you add "|" after a command, you are piping the program's output to the STDIN (standard input) of another command.
So when you do "ls | grep blah", you are running "ls" and sending it's output to the STDIN of grep. You can do this b/c grep accepts STDIN as a valid source to act upon (grep can also look in a file, of course). Lots of commands take STDIN in, but not all. When in doubt, read the manpage of the command, e.g. "man grep" (and look for STDIN or standard input).
- 01-15-2012 #6Just Joined!
- Join Date
- Oct 2011
- Posts
- 67
@atreyu Thanks that clears a lot..
Since grep states
I believe standard input here is STDINCode:grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name)
- 01-15-2012 #7Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
you believe the truth.
STDIN = standard input (can be thought of as the stuff you type on your keyboard that gets sent to the computer)
STDOUT = standard output (i.e. what gets displayed when you run commands, send to the terminal by default)
STDERR = standard error (also output generated by programs, but sent to a special file handle, also defaults to terminal)
In bash, you can send STDOUT to a file like this:
This will redirect the output of contents of the current directory to the file "ls.log". However, say you run "ls" on a file that does not exist. The "ls.log" file created by the above redirection would be empty and you'd see errors on the terminal, e.g:Code:ls > ls.log
you can send STDOUT to one file and STDERR to another like this:Code:[root@localhost ~]# ls /foo > /tmp/foo.log ls: /foo: No such file or directory
you can send both STDOUT and STDERR to the same file with:Code:ls /bin /nobin 1> stdout.log 2> stderr.log
The use of the '>' is called a redirection, as in, you are redirecting the output (either STDOUT or STDERR) to a file descriptor other than the default.Code:ls > output.log 2>&1
Last edited by atreyu; 01-15-2012 at 05:23 AM. Reason: bold!


Reply With Quote