The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 01-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,362
Quote:
Originally Posted by tostay2003 View Post
Hi

I have a variable which should be any number between 1 and 50.

It could also be any string/empty string.

I have a code written below. The point is when the variable contains string.
I don't want the code below to error out. Instead fall in the else bucket.

You don't have an "else bucket"; dothis_2 will be executed if either the numerical test or dothis_1 fails.
Quote:

Code:
[ $var -lt 1 -o $var -gt 50 ] && dothis_1 || dothis_2


Code:
if case $var in
  *[!0-9]* | "" ) false ;;
  *) true ;;
esac && [ $var -ge 1 ] && [ $var -le 50 ]
then
  echo dothis_1
else
  echo dothis_2
fi