Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-28-2009, 01:43 AM   #1
LQ Newbie
Registered: Dec 2008
Posts: 7

Rep: Reputation: 0
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:

if test $variable
if test $variable -gt 0
echo "good data exists"
else "no good data exists"
It partially works, but I get either "too many arguments" or "integer expression expected"

Thanks for any help.
Old 03-28-2009, 01:54 AM   #2
LQ Newbie
Registered: Dec 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Hello me,

I think I've found the answer:

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


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!)

Old 03-28-2009, 08:13 AM   #3
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,487

Rep: Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627
Several ideas here:

Check also the link in my signature about bash scripting.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Checking whether a variable is alphabetic basildon Linux - Newbie 4 09-21-2008 04:03 PM
Help installing Numeric-23.6 Multra Linux - Newbie 3 10-26-2004 05:14 PM
chmod u+s = numeric? kidwired Linux - Newbie 4 01-19-2003 11:24 PM

All times are GMT -5. The time now is 12:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration