LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie Script Help (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-script-help-937691/)

sagobra 04-02-2012 10:04 AM

Newbie Script Help
 
Hi as part of an assessment I have been asked to create a bash shell script but im RUBBISH at scripting!

I need to create a script that will kill processes of a given name using the commands (among others possibly) pidof, ps -ef and awk. Any help would be greatly appreciated!

TB0ne 04-02-2012 10:29 AM

Quote:

Originally Posted by sagobra (Post 4642696)
Hi as part of an assessment I have been asked to create a bash shell script but im RUBBISH at scripting!

I need to create a script that will kill processes of a given name using the commands (among others possibly) pidof, ps -ef and awk. Any help would be greatly appreciated!

Similar questions are asked here often. The standard answer is "We are not going to write your script for you, but will be glad to HELP. Post what you've written so far".

If you want hints, read the man pages on the commands you listed, along with the kill and killall commands. There are also THOUSANDS of bash scripting tutorials you can easily find with Google...have you looked at any of them?

sagobra 04-02-2012 02:48 PM

Hi thanks for the reply, so far I have the following:

#!/bin/bash
PROCESS_NAME="$1"
ps -ef | grep "$PROCESS_NAME" | awk '{print $1}'

(like I said RUBBISH!)

TB0ne 04-02-2012 05:50 PM

Quote:

Originally Posted by sagobra (Post 4642882)
Hi thanks for the reply, so far I have the following:

#!/bin/bash
PROCESS_NAME="$1"
ps -ef | grep "$PROCESS_NAME" | awk '{print $1}'

(like I said RUBBISH!)

Ok...so your goal is to KILL the process(s), right?? Wouldn't it make sense then to actually put a "kill" statement in there? Right now, all you're doing is printing out the first field. Which isn't the PID. Think about what you're trying to do, and walk through it one step at a time:
  1. Get the process name from the user in the $1. You're doing that now (I'd suggest an exit if the user doesn't provide a process name)
  2. Look through the running processes to see what matches (hint: look at the "-i" option in grep)
  3. Get the PID number for them. It's NOT the first field.
  4. You may have multiple processes that match a name, so you'll need to loop through (hint: look at a "for" loop) and kill them all
Again, see the bash scripting tutorials. One of many is:
http://tldp.org/LDP/abs/html/

Unless you practice and solve problems on your own, you'll NEVER be good at it.

David the H. 04-03-2012 02:36 AM

TBOne has it right. Break your requirements down into individual steps and figure out how to do them first. Then assemble them into your final script.

I don't think it will hurt to give you a couple of quick tips too.

1) Notice how one of the processes you get in the output is the search process itself? How can you eliminate that?

2) It's not generally necessary to use grep and awk together. awk can do its own line matching.

3) Don't try to do everything at once. Break the script up into smaller steps. Capture your output into variables, or an array, if you get a list of values. Then process those.

I highly recommend the BashGuide for learning the basic concepts, and the related FAQ and Pitfalls pages for overcoming common issues and errors:

http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls

sagobra 04-03-2012 05:51 AM

Thanks David, I am going to spend a bit of time on this tonight, I will post my results later.


All times are GMT -5. The time now is 12:13 AM.