Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    67

    Question 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

    Code:
    ls grep 'Something'
    Anyways i know the command which dispays a directory of the utility
    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

    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.
    Any suggestions ???

  2. #2
    Linux 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:

    Code:
    dirname `which gcc`
    or, alternatively:

    Code:
    dirname $(which gcc)
    the 2nd form is better as it can nest.

  3. #3
    Trusted Penguin jayd512's Avatar
    Join Date
    Feb 2008
    Location
    Kentucky
    Posts
    4,071
    Jay

    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.

  4. #4
    Just 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 ??

  5. #5
    Linux 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).

  6. #6
    Just Joined!
    Join Date
    Oct 2011
    Posts
    67
    @atreyu Thanks that clears a lot..
    Since grep states
    Code:
    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)
    I believe standard input here is STDIN

  7. #7
    Linux 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:

    Code:
    ls > ls.log
    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:
    [root@localhost ~]# ls /foo > /tmp/foo.log
    ls: /foo: No such file or directory
    you can send STDOUT to one file and STDERR to another like this:
    Code:
    ls /bin /nobin 1> stdout.log 2> stderr.log
    you can send both STDOUT and STDERR to the same file with:
    Code:
    ls > output.log 2>&1
    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.
    Last edited by atreyu; 01-15-2012 at 05:23 AM. Reason: bold!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...