LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 03-28-2009, 02:43 AM   #1
ijf99
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:

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
 
Old 03-28-2009, 02:54 AM   #2
ijf99
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

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
 
Old 03-28-2009, 09:13 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,244

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

Check also the link in my signature about bash scripting.

jlinkels
 
  


Reply


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 03: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 01:08 AM
Checking whether a variable is alphabetic basildon Linux - Newbie 4 09-21-2008 05:03 PM
Help installing Numeric-23.6 Multra Linux - Newbie 3 10-26-2004 06:14 PM
chmod u+s = numeric? kidwired Linux - Newbie 4 01-20-2003 12:24 AM


All times are GMT -5. The time now is 06:34 AM.

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