Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Help in while loop

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 11-05-2013
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 29 December 2015, 12:01 PM EST
Location: London, UK
Posts: 1,107
Thanks: 124
Thanked 258 Times in 242 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 Unix and Linux 11-05-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 10 February 2016, 9:00 AM EST
Location: Simplicity
Posts: 2,873
Thanks: 219
Thanked 896 Times in 810 Posts
... or test the ok condition again.
But I wonder how something that matches "abcd" can ever be "123".
    #4  
Old Unix and Linux 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 Unix and Linux 11-05-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 3 February 2016, 4:42 AM EST
Location: Goa / Bangalore, India
Posts: 1,745
Thanks: 95
Thanked 602 Times in 540 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 Unix and Linux 11-05-2013
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 29 December 2015, 12:01 PM EST
Location: London, UK
Posts: 1,107
Thanks: 124
Thanked 258 Times in 242 Posts

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

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:58 PM.