LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 04-20-2006, 11:06 AM   #1
MadMusician_uk
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 :


N=1
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
done


give me this :


[andy@andy andy]$ ./find_mlnet.sh
./find_mlnet.sh: line 7: syntax error near unexpected token `done'
./find_mlnet.sh: 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, 11:35 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

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

Dave

Last edited by ilikejam; 04-20-2006 at 11:39 AM.
 
Old 04-20-2006, 12:20 PM   #3
MadMusician_uk
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, 12:52 PM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

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

Code:
#!/bin/bash

for N in $(seq 1 10000)
do
  EXIST_MLNET=$(cat /proc/"$N"/status 2>/dev/null | grep "mlnet")
  if [ "$EXIST_MLNET" = "Name: mlnet" ]
  then
    echo $N > /home/andy/Temp/mlnet.found
  fi
done
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

Code:
let 'N +=1'
or

Code:
let N++
or

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

Check out the Advanced Bash Scripting guide at

http://www.tldp.org/LDP/abs/html/

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.
 
  


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
bash script loop bong.mau Programming 6 09-14-2005 08:38 PM
bash script for loop drisay Programming 5 12-25-2004 01:32 AM
Bash for loop Genjix Programming 5 12-23-2004 03:56 AM
bash script - for loop question rignes Programming 3 10-06-2004 12:16 AM
bash for loop problem deadlock Programming 5 09-04-2003 05:32 AM


All times are GMT -5. The time now is 10:31 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration