BASH Pipe question

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers BASH Pipe question
# 1  
Old 09-22-2009
BASH Pipe question

Hi all,

I'm new to shell scripting, but enjoying myself! Smilie

I'm trying to execute the following statement:
alias evol="ps -AH | grep evol | cut -d' ' -f2 | kill -9"

As you might guess. I'm wanting to use a single command 'evol' to kill all the processes containing the phrase 'evol' (having some trouble with my evolution-exchange connector which keeps hanging on address look-ups. Anyway - killing evolution isn't enough - I thenm have to kill all the other processes, usually 4 of them. the repetition of searching for the pids and then killing them is getting to me Smilie).

I find the bit up until the last pipe is fine. It produces a nice little list of pids to kill. After that though, kill throws an error. I suspect its something to do with the format of the list I'm trying to pipe to it...Smilie

Any help?

Regards, Mark
# 2  
Old 09-22-2009
cut -d is unreliable in this case, as the ps output is formatted. so if you have two process ids of different length (i.e., 345, 23456) there will be leading spaces on the shorter one.

instead, use 'cut -c1-5'

then, you have to embed your command in a loop, like this:
for each in `command`;do kill -9 $each;done

the var $each is automatically created. the result of command in backticks is a list over which the for loop iterates.

# 3  
Old 09-22-2009

Thanks very much - that's exactly what I wanted to do.
I have subsequently worked out I can achieve the same result with 'killall -9 -ir evol*' which is briefer. However, your answer addresses the underlying question I had of how to get a the listed (piped) results to be loped through.

Thanks very much!!!Smilie
# 4  
Old 09-23-2009
killall behaves differently between different operating systems, so beware. Check if pkill is available on your OS, as that's likely to be safer. Smilie
# 5  
Old 09-23-2009
alias evol="kill -9 $(ps -AH | grep evol | awk '{ print $2 }')"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pipe 2 bash loops together

What is the proper way to run two bash loops in the same command? The two below loops run separately, the problem is when I pipe them I get an error that the file used for the second loop does not exist. I am not sure how to wait for the first loop to complete and then start the second. Thank... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. UNIX for Advanced & Expert Users

[BASH] Read pipe of unkown number of arguments?

Heays So i have that script to which i'd like to pipe (rather than just regular arguments) some data from another virtual output command. Simplified: echo * | When i know how many args i expect, i can handle this simple by: && \ read ONE TWO && \ set ONE TWO echo "$1 : $2... (7 Replies)
Discussion started by: sea
7 Replies

3. Shell Programming and Scripting

Using pipe in bash - is this the expected behaviour?

Hi all, Am trying to convert a script from ksh to bash :wall:. One of the sub is something like below: #!/bin/bash declare -a array01 step_01_test() { local count=0 ps -ef | grep watch | grep -v grep | awk '{ print $8 }' | while read line do let count=${count}+1 ... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

5. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

6. Shell Programming and Scripting

Bash code to create named Pipe

Guy's, I need help with creating a pipe, I found this code online but not exactly sure what different parts are doing. Will someone be able to help me with explaining what code is doing? Also what I want is to have everything the same directory. Meaning I am working in directory: I want... (5 Replies)
Discussion started by: INHF
5 Replies

7. Shell Programming and Scripting

Bash Script then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

8. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

9. Shell Programming and Scripting

pipe | question

how do you pipe the results to next statement as argument? somecommand | grep $result somefile how do you reference $result with?? (12 Replies)
Discussion started by: convenientstore
12 Replies

10. UNIX for Dummies Questions & Answers

Question in reference to the pipe |

My question is can you use the pipe more than one time in the same command line? By the way I am new to UNIX. Thanks in advanced! (3 Replies)
Discussion started by: elkoreanopr
3 Replies
Login or Register to Ask a Question