Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19
Hello Everyone! I tried to google but instead of getting a more straight from the shoulder answer, I was given choices that seemed to bring me farther from my interest. ...
  1. #1
    Linux Engineer nujinini's Avatar
    Join Date
    Apr 2009
    Posts
    1,229

    What Does the Pipe "|" Basically do Please?

    Hello Everyone!

    I tried to google but instead of getting a more straight from the shoulder answer, I was given choices that seemed to bring me farther from my interest. Thus I run to my reliable community.

    Please forgive my petty question but can somebody please explain even in a brief manner the use of the "pipe" (|).

    I would appreciate it if I am given some examples also since I can understand it better when I see the actual application. My apologies for that

    Thank you in advance!
    nujinini
    Linux User #489667

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    The pipe can be used to pass the output of one command to the next. Standard output (stdout) of one command becomes standard input (stdin) for the next.

    Example:
    Code:
    ls -R
    ...recursively list files and the files in each subdirectory of the current one
    Code:
    less file.txt
    ...read a file in manageable, terminal-sized chunks.
    Code:
    ls -R | less
    ...instead of using less to read a file, have it take the output of ls -Rand make it more manageable to read.

    Almost all unix command-line tools can read from stdin as well as the expected write to stdout. With this you can string an entire series of commands together, each depending on what they are fed.

    Code:
    kill $(ps aux |grep evolution |awk '{print $2}')
    ...this command kills what is output from the commands chained together inside the parentheses. The first command outputs a snapshot list of running processes; grep filters this down to lines containing the term evolution; awk then takes the second token which would equate to the process ID. The returned value to the kill command is a series of process IDs. A command like this might be more useful than killall when you not only want to kill processes by a certain name but also associated processes, perhaps a python script for a plugin or similar.

  3. #3
    Linux Engineer nujinini's Avatar
    Join Date
    Apr 2009
    Posts
    1,229
    Thank you bigtomrodney

    So if you may allow me to make an actual example on my end, I played movie player and then tried to kill it with the command below. Obviously I didn't get it right. It's still playing. What wrong with my script please?

    Code:
    [jun@localhost ~]$ kill $(ps aux | grep Movie_Player | awk 'print $2)
    >
    nujinini
    Linux User #489667

  4. #4
    Just Joined! amenditman's Avatar
    Join Date
    Apr 2008
    Location
    Valley of Gold
    Posts
    37
    I don't know for sure if this will fix it, but I noticed you left out the braces around print $2.

    bigtomrodney example used '{print $2}'

  5. #5
    Linux Engineer nujinini's Avatar
    Join Date
    Apr 2009
    Posts
    1,229
    Thanks for pointing it out. I tried to put them braces but still didn't work though.

    Code:
    [jun@localhost ~]$ kill $(ps aux | grep Movie_Player | awk '{print $2}')
    bash: kill: (3729) - No such process
    [jun@localhost ~]$ kill $(ps aux | grep Movie_Player | awk '{print $2}')
    bash: kill: (3736) - No such process
    nujinini
    Linux User #489667

  6. #6
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    I don't have access to Linux machine right now. Execute command in steps and check where it throws error.
    Code:
    ps aux
    ps aux |grep Movie_Player
    ps aux |grep Movie_Player |awk '{print $2}'
    kill $(ps aux |grep Movie_Player |awk '{print $2}')
    Make sure to type correctly. Bash is case and space sensitive.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  7. #7
    Linux Engineer nujinini's Avatar
    Join Date
    Apr 2009
    Posts
    1,229
    Hello Casper,

    Code:
    jun      10805  0.0  0.0   5000  1588 pts/0    Ss   15:59   0:00 bash
    jun      10823 18.8  1.7 253768 51868 ?        Sl   15:59   0:06 totem
    jun      10840  0.0  0.0   4660   968 pts/0    R+   16:00   0:00 ps aux
    Code:
    [[jun@localhost ~]$ ps aux |grep Movie_Player
    jun      11146  0.0  0.0   4212   708 pts/0    S+   16:18   0:00 grep Movie_Player
    [jun@localhost ~]$
    Code:
    [jun@localhost ~]$ ps aux |grep Movie_Player |awk '{print $2}'
    11154
    [jun@localhost ~]$
    Code:
    [jun@localhost ~]$ kill $(ps aux |grep Movie_Player |awk '{print $2}')
    bash: kill: (11159) - No such process
    [jun@localhost ~]$
    My player is still working...
    Last edited by nujinini; 02-28-2010 at 07:21 AM.
    nujinini
    Linux User #489667

  8. #8
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    241
    You might wanna replace the word movie_player with "totem" I guess thats the movie player program name ...

  9. #9
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Quote Originally Posted by zombykillah View Post
    You might wanna replace the word movie_player with "totem" I guess thats the movie player program name ...
    Just back now! That's exactly the problem. You'd have to use the process name.

  10. #10
    Linux Engineer nujinini's Avatar
    Join Date
    Apr 2009
    Posts
    1,229
    Code:
    [jun@localhost ~]$ kill $(ps aux |grep Movie_Player |awk '{print $2}')
    bash: kill: (6030) - No such process
    
    [jun@localhost ~]$ kill $(ps aux |grep Totem |awk '{print $2}')
    bash: kill: (6034) - No such process
    
    [jun@localhost ~]$ kill $(ps aux |grep totem |awk '{print $2}')
    bash: kill: (6038) - No such process
    
    [jun@localhost ~]$
    Thanks! Done.

    The third scrypt killed the totem. I'm just wondering it still said
    bash: kill: (603 - No such process though.

    If I may not be too indulgent, may I please ask what the process behind the command was?

    1) I understand "kill". Why do I have to put a "$" again?
    2) ps aux was to identify the (PID of totem that was piped "|" to kill it..
    3) What is action of "grep", and why do we have to pipe "|" it again for awk'{print $2}') which I also do not understand the action of.

    I hope you won't find my question to be as clear as mud, darn . Thanks!
    nujinini
    Linux User #489667

Page 1 of 2 1 2 LastLast

Posting Permissions

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