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. ...
- 02-27-2010 #1
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
- 02-27-2010 #2Linux 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:
...recursively list files and the files in each subdirectory of the current oneCode:ls -R
...read a file in manageable, terminal-sized chunks.Code:less file.txt
...instead of using less to read a file, have it take the output of ls -Rand make it more manageable to read.Code:ls -R | less
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.
...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.Code:kill $(ps aux |grep evolution |awk '{print $2}')
- 02-28-2010 #3
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
- 02-28-2010 #4
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}'
- 02-28-2010 #5
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 processnujinini
Linux User #489667
- 02-28-2010 #6
I don't have access to Linux machine right now. Execute command in steps and check where it throws error.
Make sure to type correctly. Bash is case and space sensitive.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}')It is amazing what you can accomplish if you do not care who gets the credit.
New Users: Read This First
- 02-28-2010 #7
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 ~]$My player is still working...Code:[jun@localhost ~]$ kill $(ps aux |grep Movie_Player |awk '{print $2}') bash: kill: (11159) - No such process [jun@localhost ~]$
Last edited by nujinini; 02-28-2010 at 07:21 AM.
nujinini
Linux User #489667
- 02-28-2010 #8Linux 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 ...
- 02-28-2010 #9Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
- 02-28-2010 #10Thanks! Done.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 ~]$
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


Reply With Quote
