LinuxQuestions.org
Review your favorite Linux distribution.
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 03-11-2008, 09:46 AM   #1
sudleyplace
LQ Newbie
 
Registered: Mar 2008
Posts: 6

Rep: Reputation: 0
Trouble With FOR Loop In Simple Bash Script


I thought I copied the following script correctly, but it doesn't seem to work. In particular, although I can type ps auxww | etc at the command line and get a PID, the for loop in the script doesn't run:

Code:
#! /bin/bash
#
echo -n "Stopping rsyncd:  ";
for pid in $( ps auxww | grep "rsync" | grep -v grep | grep -v pico | cut -c10-14 );
do
kill -9 $pid;
rm -f /var/lock/rsyncd.lock;
echo "[ OK ]";
exit;
done;
echo "[ FAILURE ]";
1. Why doesn't the for loop run?
2. Do I need all those semicolons?
3. After you get this working, do you see any way to simplify it?
4. Is there a tutorial on shell scripts which covers the concepts I've missed?

Last edited by sudleyplace; 03-11-2008 at 10:08 AM.
 
Old 03-11-2008, 10:37 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,019
Blog Entries: 54

Rep: Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766
Quote:
Originally Posted by sudleyplace View Post
Why doesn't the for loop run?
Because you exit prematurely? Debugging helps show that: try running the script as 'sh -vx scriptname'.


Quote:
Originally Posted by sudleyplace View Post
Do I need all those semicolons?
Not unless you want to string commands together: "for item in something; do action; action && { echo OK; exit 0; }; done; exit 0"


Quote:
Originally Posted by sudleyplace View Post
After you get this working, do you see any way to simplify it?
It's a common mistake to use 'ps' to grep for PIDs. Instead you could use 'pgrep'.
Even better, you can combine pgrep and kill and use 'pkill' instead: pkill -9 -f 'rsync'.


Quote:
Originally Posted by sudleyplace View Post
Is there a tutorial on shell scripts which covers the concepts I've missed?
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 03-11-2008, 11:43 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
See killall, which will do what you are trying to script.
 
Old 03-11-2008, 01:21 PM   #4
sudleyplace
LQ Newbie
 
Registered: Mar 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Because you exit prematurely? Debugging helps show that: try running the script as 'sh -vx scriptname'.
Thanks -- knowing the debug commands helps a lot.

Quote:
Originally Posted by unSpawn View Post
Not unless you want to string commands together: "for item in something; do action; action && { echo OK; exit 0; }; done; exit 0"
OK, now I understand.

Quote:
Originally Posted by unSpawn View Post
It's a common mistake to use 'ps' to grep for PIDs. Instead you could use 'pgrep'.
Even better, you can combine pgrep and kill and use 'pkill' instead: pkill -9 -f 'rsync'.
I don't seem to have a pgrep or pkill on my system (WestHost.com), however ps seems to work in this context. Am I missing something (besides pgrep/pkill)?

I did figure out why the for loop didn't execute correctly. I did a copy and paste to get the original script, however that script contained a spurious character (\224) in it which does not display. When I ran the script through the shell debugger, I saw the extraneous char. Removing it allowed the script to run.

All good references. Many thanks!
 
Old 03-11-2008, 05:40 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,019
Blog Entries: 54

Rep: Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766Reputation: 2766
Quote:
Originally Posted by sudleyplace View Post
I don't seem to have a pgrep or pkill on my system (WestHost.com)
If you run a VPS there maybe you could install the package. It's standard, not exotic. Good you found out the char thing.
 
Old 03-11-2008, 07:42 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

Rep: Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026
Another very good Linux tutorial: http://rute.2038bug.com/index.html.gz
 
Old 03-12-2008, 04:55 AM   #7
sudleyplace
LQ Newbie
 
Registered: Mar 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
If you run a VPS there maybe you could install the package. It's standard, not exotic. Good you found out the char thing.
I found another tool killproc (similar to pkill) and part of the source function library /etc/init.d/functions which makes it easy to remove a program.
 
Old 03-12-2008, 04:55 AM   #8
sudleyplace
LQ Newbie
 
Registered: Mar 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Another very good Linux tutorial: http://rute.2038bug.com/index.html.gz
Thanks for the reference.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
Bash script while loop problems whammack Programming 7 02-02-2007 05:31 AM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM
bash script for loop drisay Programming 5 12-25-2004 12:32 AM


All times are GMT -5. The time now is 11:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration