[olug] Test for integer in bash
Luke -Jr
luke at dashjr.org
Fri Nov 30 20:10:10 UTC 2007
On Wednesday 28 November 2007, Obi-Wan wrote:
> > echo "$1" | grep ^[0-9]*$ > /dev/null
> >
> > testInt
> >
> > Secondly, I'm sure you've noticed that the last test of nothing
> > returns as a match. Why can't I get the correct regex, echo 900 |
> > grep ^[0-9]+$ , to work? echo 900 | grep ^[0-9]{1,}$ fails too.
>
> In your last example, you're trying to use perl regex, which doesn't
> work in grep (or even egrep). What you want is "grep "^[0-9][0-9]*$".
or:
grep '^[0-9]\+$' <<<'900'
grep '^[0-9]\{1,\}$' <<<'900'
or even (best):
[[ '900' =~ ^[0-9]+$ ]] && echo YAY || echo FAIL
More information about the OLUG
mailing list