Help in while loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help in while loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-05-2013
learnbash learnbash is offline
Banned
 
Join Date: Mar 2008
Last Activity: 28 July 2014, 4:29 PM EDT
Posts: 364
Thanks: 31
Thanked 3 Times in 3 Posts
Help in while loop

I have below code, that check abcd that is fine, when it not find abcd it check for 1 minutes that is also fine but i want one enhancement when it failed at the end it print "please check again" but if i add this line after fi statement it is constantly printing, i don't want that, please suggest.


Code:
tp=1
while [ $tp -le 2]

do

  echo 

  command=`ls -l |grep abcd`

  if [ "$command" == "123" ]

  then

  echo "ok"

  break

  else
  let tp++
  sleep 30
  echo "nook"

  fi

done


Last edited by learnbash; 11-05-2013 at 01:13 PM..
Sponsored Links
    #2  
Old 11-05-2013
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 25 November 2014, 9:28 AM EST
Location: London, UK
Posts: 1,064
Thanks: 113
Thanked 242 Times in 227 Posts
You could check the value of $tp (which will be 3 if you fail) before outputting your warning message. Alternatively, explicitly set a variable inside the 123 check to indicate that you succeeded, and check that.
Sponsored Links
    #3  
Old 11-05-2013
MadeInGermany MadeInGermany is online now Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 28 November 2014, 3:08 AM EST
Location: Simplicity
Posts: 2,055
Thanks: 134
Thanked 603 Times in 547 Posts
... or test the ok condition again.
But I wonder how something that matches "abcd" can ever be "123".
    #4  
Old 11-05-2013
learnbash learnbash is offline
Banned
 
Join Date: Mar 2008
Last Activity: 28 July 2014, 4:29 PM EDT
Posts: 364
Thanks: 31
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by MadeInGermany View Post
... or test the ok condition again.
But I wonder how something that matches "abcd" can ever be "123".
how i can test the ok condition again? Please update the code.
Sponsored Links
    #5  
Old 11-05-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 November 2014, 4:34 AM EST
Posts: 1,634
Thanks: 78
Thanked 532 Times in 486 Posts
Quote:
Originally Posted by learnbash View Post
how i can test the ok condition again? Please update the code.
To check condition again you can use infinite loop something like this


Code:
#!/bin/bash
x=0
while : 
do

n=0
while [ $n -le 5 ];do 

if [ $n -eq  3 ]; then
    echo "Going to Break internal while loop but will check again" $x
    break
fi

n=$((n+1))

done
x=$((x+1))
done

Sponsored Links
    #6  
Old 11-05-2013
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 25 November 2014, 9:28 AM EST
Location: London, UK
Posts: 1,064
Thanks: 113
Thanked 242 Times in 227 Posts

Code:
tp=1
while [ $tp -le 2]
do
   ...stuff...
done

if [ "$command" != "123" ]
then
   echo "a warning message"
fi

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reset while loop to loop same file multiple times tmalik79 Shell Programming and Scripting 4 10-09-2013 11:34 AM
Array Variable being Assigned Values in Loop, But Gone when Loop Completes??? mrm5102 Shell Programming and Scripting 5 10-19-2012 11:00 AM
HELP PLS - calling function in a while loop ends the loop ?? newbie_01 Shell Programming and Scripting 3 05-29-2012 05:38 PM
Null Handling in Until loop. . .loop won't stop brandono66 Shell Programming and Scripting 4 11-24-2009 04:57 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM



All times are GMT -4. The time now is 04:23 AM.