Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-20-2006, 10:06 AM   #1
LQ Newbie
Registered: Oct 2004
Location: Denver, USA
Distribution: Ubuntu 9.04
Posts: 20

Rep: Reputation: 0
bash while loop problem

I'm really new to bash scripts, so am probably making a stupidly obvious error, but why does this :

while [$N<10000]; do
EXIST_MLNET=cat /proc/"$N"/status | grep "mlnet"
if [ $EXIST_MLNET = "Name: mlnet" ] then
echo $N > /home/andy/Temp/mlnet.found
incr N

give me this :

[andy@andy andy]$ ./
./ line 7: syntax error near unexpected token `done'
./ line 7: `done'

What I'm trying to do is find the PID of mlnet, so that I can then kill it.

any help would be greatly apprecaited!

btw, I'm using Mandrake 10.1
Old 04-20-2006, 10:35 AM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

A simpler way to get PIDs is:
ps -A | grep mlnet | awk '{ print $1 }'
Anyway, the problem with your code may be the
EXIST_MLNET=cat /proc/"$N"/status | grep "mlnet"
line. It should probably read
EXIST_MLNET=`cat /proc/"$N"/status | grep "mlnet"`
and you're missing 'fi' at the end of your 'if' block.


Last edited by ilikejam; 04-20-2006 at 10:39 AM.
Old 04-20-2006, 11:20 AM   #3
LQ Newbie
Registered: Oct 2004
Location: Denver, USA
Distribution: Ubuntu 9.04
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks Dave!

I had no idea the ps command existed, that makes it much easier!
Old 04-20-2006, 11:52 AM   #4
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Your script should look more like ...


for N in $(seq 1 10000)
  EXIST_MLNET=$(cat /proc/"$N"/status 2>/dev/null | grep "mlnet")
  if [ "$EXIST_MLNET" = "Name: mlnet" ]
    echo $N > /home/andy/Temp/mlnet.found
Use the for loop since you are counting, and it saves you from having to increment N. I also added pipping the standard errors from cat to /dev/null so that you won't see a message everytime you try to access a process that doesn't exist. The only thing that you are going to want to watch out for is if you have more than one process with mlnet in it. The > redirect will over-write that file, so you will only end up with the last occurrance. You can use >> if you want to append to that file everytime it finds an instance. If this is the case, add an rm statement to the beginning of the script so that you get clean data everytime.

If you were to use your while loop, your condition would be $N -lt 10000, and not what you have. incr is not a Bash command either. You'd need to use

let 'N +=1'

let N++

(( N++ ))
to increment your value.

Check out the Advanced Bash Scripting guide at

for a good over-view of the Bash scripting language. Lots of good information in there.

I agree though. There are easier ways to find the pid of a process.


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
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
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM
bash for loop problem deadlock Programming 5 09-04-2003 04:32 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

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