LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Checking variable is numeric (https://www.linuxquestions.org/questions/programming-9/checking-variable-is-numeric-715028/)

ijf99 03-28-2009 01:43 AM

Checking variable is numeric
 
Hi folks,

Basic question I assume.

I need to check whether a shell script variable contains non-zero numeric data to proceed. The variable should contain numeric values, but may in fact contain zeroes, blank space or nothing.

So far, I have tried various combinations of:

Code:

if test $variable
then
if test $variable -gt 0
then
echo "good data exists"
else "no good data exists"
fi
fi

It partially works, but I get either "too many arguments" or "integer expression expected"

Thanks for any help.
ijf

ijf99 03-28-2009 01:54 AM

Hello me,

I think I've found the answer:

replaced if test $variable
with: if test -n $variable

and

replaced: if test $variable -gt 0
with: if test $variable \> 0

Please let me know if anything unexpected may happen.

(Thanks to they guy (Rinkel?) with the Advanced Bash-Scripting Guide in his sig!)

ijf

jlinkels 03-28-2009 08:13 AM

Several ideas here:
http://www.linuxquestions.org/questi...values-352226/

Check also the link in my signature about bash scripting.

jlinkels


All times are GMT -5. The time now is 02:46 AM.