You are not logged in.

#1 2008-02-02 23:21:46

Thrillhouse
Member
From: Arlington, VA, USA
Registered: 2007-05-29
Posts: 175

Easy Question

In BASH, how do you check if a parameter is an integer?  I've Googled, searched the forums and read a few guides but can't find anything.  I would imagine it's a -something option so that I could use if [ -i $1 ] or something along those lines but I can't seem to find anything.


For the strength of the pack is the wolf, and the strength of the wolf is the pack.

Offline

#2 2008-02-02 23:30:15

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: Easy Question

It seems the built in math function $((...)) returns 0 on strings.
var="str"
[ $var != 0 ] && [ $(($var+0)) = 0 ] && echo this is a string.
var="55"
[ $var != 0 ] && [ $(($var+0)) = 0 ] || echo this is an integer.
var="0"
[ $var != 0 ] && [ $(($var+0)) = 0 ] || echo this is an integer.
var="string1with2integers"
[ $var != 0 ] && [ $(($var+0)) = 0 ] && echo this is a string.

seems to work...

Offline

#3 2008-02-03 00:04:38

Thrillhouse
Member
From: Arlington, VA, USA
Registered: 2007-05-29
Posts: 175

Re: Easy Question

That seems to have done the trick.  I thought bash would have something built in for stuff like this but I guess this will work.

Thank you, Gilneas.


For the strength of the pack is the wolf, and the strength of the wolf is the pack.

Offline

Board footer

Powered by FluxBB