Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'IF' statement 1

Status
Not open for further replies.

rik1551

IS-IT--Management
Sep 14, 2001
10
GB
Hi,

This is probably a basic UNIX scripting question, but I guess that if you don't ask you never learn!
I'm trying to write a script that does a number of things but I have become stuck on one particular part.
I have a variable which is defined in the script as the result of a search. The variable will either be a time in the following format nn:nn or a time in this format (nn:nn). How can I use an 'IF' statement to echo a success if the variable is the time stamp without the brackets, and echo a fail if the variable is the time in brackets.

cheers

Richard
 
TIME=12:45
echo $TIME | grep "([0-9][0-9]:[0-9][0-9])"
if [ $? = 0 ] ; then
echo "format is with parens..."
else
echo $TIME | grep "[0-9][0-9]:[0-9][0-9]"
if [ $? = 0 ] ; then
echo "valid format without parens..."
fi
fi
 
Another way to do it:
Code:
case $TIME in 
    [0-9][0-9]:[0-9][0-9]) echo "TIME OK";;
    *) echo "TIME NOT OK";;
esac
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top