LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-14-2012, 09:18 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
BASH: check if the first argument match a number


I am writing a bash script and I need to check if the first argument matchs a number. The number can be either a decimal number or a real number, no mater how many decimal places or how large it is. 37.987 or 956 or 1 or 3.14159, anything like that.

How can I state this condition in if [[ ... ]] ?

Thanks for your help!
 
Old 11-14-2012, 09:44 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
The classic bash test for an integer is [[ $var =~ ^[0-9]+$ ]]

That needs to be extended to accommodate a possible decimal point: [[ $1 =~ ^([0-9]*\.)?[0-9]+$ ]]

It works on the posted numbers but would not work on negative numbers or numbers with thousands separators such as 1,000. OK?
 
Old 11-14-2012, 10:28 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A non-bash solution using awk
Code:
if echo $number | awk '$0 != $0 + 0 { exit 1 }'
then
  echo $number is a number
else
  echo $number is not a number
fi
This manages floating-point numbers, negative numbers and scientific notation. I'm not sure about thousand-separators.
 
Old 11-14-2012, 10:45 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,114

Rep: Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336
Is this is for a homework assignment?

Essentially bash variables are strings. I assume you are trying to match an exact number and therefore it is just a string comparison.
if [[ "$var" == "value" ]] then

BTW bash can only handle integer math. If you need some floating point math capability then bc is one option.

http://linux.about.com/od/commands/l/blcmdl1_bc.htm
 
Old 11-15-2012, 07:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,974

Rep: Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179
@michaelk - I find the following to be a curious statement:
Quote:
Essentially bash variables are strings.
I am not sure how you came up with this?

Allow me to demonstrate why I think it is incorrect:
Code:
$ cat string_and_num_test.sh
#!/bin/bash

s='100'
n=100

[[ $x < "12" ]] && echo true for string

(( n < 12 )) && echo true for num

$ ./string_and_num_test.sh
true for string
We see the test works for the string as characters are compared from left to right however the number test clearly shows that it is being calculated as a number.

Now unless bash does some magic to number tests compared to string ones I would say you can have both string and numerical variables.
 
Old 11-15-2012, 11:10 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,114

Rep: Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336Reputation: 5336
http://www.tldp.org/LDP/abs/html/untyped.html
 
  


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
PHP + MYSQL: Invalid parameter number: number of bound variables does not match... OrangeGrover Linux - Software 6 05-08-2013 05:36 PM
[SOLVED] Bash Script - Check Argument with REGEX mrm5102 Programming 9 04-10-2012 10:07 AM
why sector number not match block number? bitzsk Linux - Kernel 1 06-09-2009 05:32 AM
Bash : how do i check the user input and make sure is only chracter or only number ? CheeSen Programming 10 09-07-2008 03:15 PM
how to find out if argument is number: bash kubicon Linux - Newbie 1 09-19-2003 04:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 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
Open Source Consulting | Domain Registration