LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-02-2012, 11:04 AM   #1
sagobra
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
Post 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!
 
Old 04-02-2012, 11:29 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,948

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by sagobra View Post
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?
 
Old 04-02-2012, 03:48 PM   #3
sagobra
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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!)
 
Old 04-02-2012, 06:50 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,948

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by sagobra View Post
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.
 
Old 04-03-2012, 03:36 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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

Last edited by David the H.; 04-03-2012 at 03:38 AM.
 
Old 04-03-2012, 06:51 AM   #6
sagobra
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks David, I am going to spend a bit of time on this tonight, I will post my results later.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Newbie needs help with script RavhisingRickRude Linux - Newbie 6 12-26-2010 04:12 AM
Help a script newbie unholy Programming 2 09-19-2004 08:05 AM


All times are GMT -5. The time now is 12:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration