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.

how to exit a script?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-27-2007
filthymonk filthymonk is offline
Registered User
 
Join Date: May 2007
Last Activity: 5 January 2009, 10:10 PM EST
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
how to exit a script?

how do i exit a script in the middle of a loop ?

i have a while loop running for 100 loops for e.g. the while loop will start to loop 100 times and every time increasing the no. if i have a i=50 and its has already looped until 150 and i want to abruptly exit the script if it hits 200 with the if else statement how do i go about exiting the script ?

because i used


Code:
i=200
while [ $i -le 100 ]
do 
      echo $1

      if $1 = 250
      then exit
      fi

done

my server doesn't exit when its 250 it continues till 300 .....
Sponsored Links
    #2  
Old Unix and Linux 06-27-2007
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Quote:
Originally Posted by filthymonk View Post
h


Code:
i=200
while [ $i -le 100 ]
do 
      echo $1

      if $1 = 250
      then exit
      fi

done

my server doesn't exit when its 250 it continues till 300 .....
Is the $1 a typo ? Where do you increment the value of i ?
Sponsored Links
    #3  
Old Unix and Linux 06-27-2007
frijofranco frijofranco is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 4 October 2009, 2:47 AM EDT
Location: Irvine, CA
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Try this out

#!/usr/bin/ksh
i=0
while [[ $i -lt 50 ]]
do
echo $i
if [ $i -eq 25 ]
then
echo "Exiting Loop"
break
fi
i=`expr $i + 1`
done
echo "Exiting script "
exit;
    #4  
Old Unix and Linux 06-27-2007
filthymonk filthymonk is offline
Registered User
 
Join Date: May 2007
Last Activity: 5 January 2009, 10:10 PM EST
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Uuuuuhhhhh it works perfectly ! thanks frijofranco.

However, how do i end a script if i cannot find a file

for e.g.


Code:
if `find *.txt` = "No such file or Directory"
then 
break
fi

could anyone show me where have i gone wrong and what is the right command to execute .. thanks

and is there anyway i can stop the showing of error??

for e.g. i am pulling files from one of the statistics folder and if they can't find any file there would be an error saying no such file or directory is there anyway i can stop it ?
Sponsored Links
    #5  
Old Unix and Linux 06-28-2007
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 16 March 2015, 4:56 AM EDT
Location: Bangalore,India
Posts: 2,136
Thanks: 14
Thanked 150 Times in 143 Posts
Quote:
Originally Posted by filthymonk View Post
Uuuuuhhhhh it works perfectly ! thanks frijofranco.

However, how do i end a script if i cannot find a file

for e.g.


Code:
if `find *.txt` = "No such file or Directory"
then 
break
fi

could anyone show me where have i gone wrong and what is the right command to execute .. thanks

and is there anyway i can stop the showing of error??

for e.g. i am pulling files from one of the statistics folder and if they can't find any file there would be an error saying no such file or directory is there anyway i can stop it ?

Code:
var=`find *.txt 2>/dev/null` 
if [[ -z "$var" ]]
then 
break
fi

Sponsored Links
    #6  
Old Unix and Linux 06-28-2007
frijofranco frijofranco is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 4 October 2009, 2:47 AM EDT
Location: Irvine, CA
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Its Simple....

Just use

filename="/tmp/a.txt"

if [ -f "$filename" ]
then
echo "File Found"
else
echo "File Not Found"
break #if you want to exit the looop, give exit if you want to exit the script.
fi

go for find if you want to search and the file name/path is unknown.
Sponsored Links
    #7  
Old Unix and Linux 06-28-2007
filthymonk filthymonk is offline
Registered User
 
Join Date: May 2007
Last Activity: 5 January 2009, 10:10 PM EST
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
thanks frijo and anbu. How come i declare

d=01
i=1
while [ $i -le 31 ]
d=$(( d+1))
i=$(( i+1))
echo $d
done

when i do the above script i dun get
01 02 03 04 05 06 07 ---> 31 , instead i got 01 2 3 4 5 6 7 ---> 31
??? i got no problem with double digits but single digits just won't show the 0 even if i change d=$(( d+01))
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
How to grep sql error in shell script and exit the script? allinshell99 Shell Programming and Scripting 2 05-27-2010 09:06 PM
trying to grep the first few lines of a continuos script, and exit the script anyidea k00061804 UNIX for Dummies Questions & Answers 5 07-22-2009 12:15 PM
Exit out of the Script Command inside a Script jmungai UNIX for Dummies Questions & Answers 6 02-20-2009 04:26 PM
Script exit Krrishv Shell Programming and Scripting 3 06-23-2008 05:27 PM
Where can I find a list of exit codes? (Exit code 64) jkuchar747 UNIX for Dummies Questions & Answers 3 12-07-2004 05:08 PM



All times are GMT -4. The time now is 05:14 AM.