Computing.Net > Forums > Unix > check numeric value entered

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

check numeric value entered

Reply to Message Icon

Original Message
Name: cf0sro
Date: April 24, 2003 at 13:06:10 Pacific
Subject: check numeric value entered
OS: redhat 7.3
CPU/Ram: pIII 512MByte
Comment:

how do I check if a variable contains a numeric value before trying to perform a calculation on it.
The variable is read in from the keyboard by a shell script


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: April 24, 2003 at 13:30:26 Pacific
Reply: (edit)


# this function sets the exit to non zero if $1 non numeric
is_digit () {
expr "$1" + 1 > /dev/null 2>&1
}

# all on one line
is_digit $1 || { echo "$1 is non-numeric" && exit 1; }

Regards,

Nails


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software