Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Help in while loop

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-05-2013   -   Original Discussion by learnbash
learnbash's Unix or Linux Image
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   -   Original Discussion by learnbash
CarloM's Unix or Linux Image
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 16 January 2018, 10:25 AM EST
Location: London, UK
Posts: 1,114
Thanks: 128
Thanked 261 Times in 244 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   -   Original Discussion by learnbash
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 18 January 2018, 4:14 AM EST
Location: Simplicity
Posts: 3,933
Thanks: 333
Thanked 1,315 Times in 1,185 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   -   Original Discussion by learnbash
learnbash's Unix or Linux Image
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   -   Original Discussion by learnbash
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Part Time Freelancer
 
Join Date: Nov 2012
Last Activity: 30 October 2017, 11:43 PM EDT
Posts: 1,793
Thanks: 119
Thanked 630 Times in 560 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   -   Original Discussion by learnbash
CarloM's Unix or Linux Image
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 16 January 2018, 10:25 AM EST
Location: London, UK
Posts: 1,114
Thanks: 128
Thanked 261 Times in 244 Posts


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

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

Sponsored Links
Closed

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
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:53 PM.