LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trouble With FOR Loop In Simple Bash Script (http://www.linuxquestions.org/questions/linux-newbie-8/trouble-with-for-loop-in-simple-bash-script-627247/)

sudleyplace 03-11-2008 09:46 AM

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?

unSpawn 03-11-2008 10:37 AM

Quote:

Originally Posted by sudleyplace (Post 3085134)
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 (Post 3085134)
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 (Post 3085134)
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 (Post 3085134)
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/

matthewg42 03-11-2008 11:43 AM

See killall, which will do what you are trying to script.

sudleyplace 03-11-2008 01:21 PM

Quote:

Originally Posted by unSpawn (Post 3085180)
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 (Post 3085180)
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 (Post 3085180)
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!

unSpawn 03-11-2008 05:40 PM

Quote:

Originally Posted by sudleyplace (Post 3085342)
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.

chrism01 03-11-2008 07:42 PM

Another very good Linux tutorial: http://rute.2038bug.com/index.html.gz

sudleyplace 03-12-2008 04:55 AM

Quote:

Originally Posted by unSpawn (Post 3085594)
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.

sudleyplace 03-12-2008 04:55 AM

Quote:

Originally Posted by chrism01 (Post 3085689)
Another very good Linux tutorial: http://rute.2038bug.com/index.html.gz

Thanks for the reference.


All times are GMT -5. The time now is 11:54 AM.