LinuxQuestions.org
Visit Jeremy's Blog.
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-19-2007, 05:30 AM   #1
151803
LQ Newbie
 
Registered: Jan 2007
Posts: 22

Rep: Reputation: 15
Loop iteration in Linux scripting.


I have written a script in which I want that if a condition is true the script should execute the loop and after executing it should evaluate the condition oncve again and repeat the execution of loop unless the condition is false.I am giving an example below,Please help.

`echo "cd /home/xmuser/CQ/var/bin"`
`echo ". ./xmenv.ksh"`
`echo "ftfping"` >ftfping.txt
var=`echo "more ftfping.txt" |grep SUCCESSFUL |wc -l`
if [[ $var -ne 1 ]]; then
sleep 30 |./xmstop.ksh
for PIDs in `ps -eaf |grep -E "xmuser|run.sh" | grep -E "xmocoord|/home/xmuser/|/home/db2inst1/|Coordinator|xmo" |awk '{print
$2}'`
do
kill -9 $PIDs >junk
done
var=`echo "more ftfping.txt" |grep SUCCESSFUL |wc -l`
fi


I want to execute the if loop till teh condition is false but the loop is being executed only once.Please advice...........
 
Old 03-19-2007, 05:55 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'm no scripting expert but....
The only condition test I see here is the "if" statement, which is not in the loop. The loop simply runs as long as there are entries in whatever is generated by the statement `ps -eaf.........2{'`
You might want to test this statement by itself to make sure it is producing what it is supposed to.

Other issues:
What output does "sleep 30" generate? (You are using a pipe, which presumes there is an output to be piped to the next command.)

Why the backticks here?:
Quote:
`echo "cd /home/xmuser/CQ/var/bin"`
2 problems here:
Quote:
echo "more ftfping.txt"
First, this will produce literally the string in the double-quotes. Second if you intended to have the output of the more command, then how would the paging for "more" be done? (ie, I'm not sure if you can really use "more" in a script.)
 
Old 03-19-2007, 06:19 AM   #3
151803
LQ Newbie
 
Registered: Jan 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Loop iteration in Linux scripting

ok,Can u please suggest how to put the if condition in loop?
 
Old 03-19-2007, 06:36 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by 151803
ok,Can u please suggest how to put the if condition in loop?
Ummm---you would put it inside the loop....ie between the "do" and "done" statements.

But--if you want a loop which simply executes until a condition is no longer true, then you need "while" or "do-while". For good tutorials and books on scripting, go to http://tldp.org

You might want to write your script in "pseudo-code" to help us see the logic you are trying to implement
 
  


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 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Linux scripting - wget loop northy_ie Programming 7 11-09-2005 08:17 AM
force grep to keep it's place in file for next iteration? jeffreybluml Programming 7 05-13-2005 10:16 AM
pronounce 'iteration' Ikebo General 5 09-28-2004 07:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:11 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
Open Source Consulting | Domain Registration