Re: Bash and testing a variable

2003-02-12 Thread Todd A. Jacobs
On Wed, 12 Feb 2003, Robert P. J. Day wrote: > that will only detect if a string *starts* with at least one integer. Exactly. So, unless you're expecting negative numbers, this will prevent yet another kind of invalid data. -- "Of course I'm in shape! Round's a shape, isn't it?" -- redhat-

Re: Bash and testing a variable

2003-02-12 Thread Robert P. J. Day
On Tue, 11 Feb 2003, Todd A. Jacobs wrote: > That's why constructs like: > > [[ 1 < a ]]; echo $? > > work. Incidentally, *any* number will evaluate to less than "a" in > lexicographic sort order, so this can be used as another valid test for > integers, and will be faster since it doesn't

Re: Bash and testing a variable

2003-02-12 Thread Giulio Orsero
On Tue, 11 Feb 2003 11:07:16 -0500, Gordon <[EMAIL PROTECTED]> wrote: >I've searched all over for an answer to this and haven't found one yet! > >Is there a way in a bash script to test if a variable contains an >integer? I want to create a script that uses some simple arithmetic but >bash bombs

Re: Bash and testing a variable

2003-02-12 Thread Jan
Todd A. Jacobs wrote: On Tue, 11 Feb 2003, Gordon wrote: Is there a way in a bash script to test if a variable contains an integer? I want to create a script that uses some simple arithmetic but Nope. You'll have to do something more arcane: function IsInt { if egrep -q '[[:digit:]]+'

Re: Bash and testing a variable

2003-02-11 Thread Cameron Simpson
On 13:53 11 Feb 2003, Gordon <[EMAIL PROTECTED]> wrote: | It's just kind of wierd that the shell has builtin math functions, but | can't, by itself, tell you if a value is a valid number. You'll find you can usually contrive not to feed non-numbers to your operations and don't need such a test of

Re: Bash and testing a variable

2003-02-11 Thread Cameron Simpson
On 14:07 11 Feb 2003, Robert P. J. Day <[EMAIL PROTECTED]> wrote: | On Tue, 11 Feb 2003, Gordon wrote: | and the matching also supports \( \)-style tagging a la Perl, | so you can get the actual match itself. More like sed really. Perl doesn't slosh the brackets. -- Cameron Simpson, DoD#743

Re: Bash and testing a variable

2003-02-11 Thread Cameron Simpson
On 11:33 11 Feb 2003, Robert P. J. Day <[EMAIL PROTECTED]> wrote: | On Tue, 11 Feb 2003, Gordon wrote: | $ man expr | | the "expr" utility is, IMNSHO, sadly overlooked. Forks an extra process. Needlessly slow. Cheers, -- Cameron Simpson, DoD#743[EMAIL PROTECTED]http://www.zip.com.a

Re: Bash and testing a variable

2003-02-11 Thread Cameron Simpson
On 16:37 11 Feb 2003, Robert Tinsley <[EMAIL PROTECTED]> wrote: | On Tue, 2003-02-11 at 16:31, Anthony E. Greene wrote: | > How about a case statement: | > case $var in | >1234567890) | | i think you mean | 1|2|3|4|5|6|7|8|9|0) Or just: case $var in [0-9]) This wor

Re: Bash and testing a variable

2003-02-11 Thread Todd A. Jacobs
On Tue, 11 Feb 2003, Gordon wrote: > It's just kind of wierd that the shell has builtin math functions, but > can't, by itself, tell you if a value is a valid number. Not really, since bash variables don't have type. Essentially, all variables are strings; the fact that you can apply integer logi

Re: Bash and testing a variable

2003-02-11 Thread Robert P. J. Day
On Tue, 11 Feb 2003, Gordon wrote: > Robert P. J. Day wrote: > > $ man expr > > > > the "expr" utility is, IMNSHO, sadly overlooked. > > > > rday > > > > > > > Thanks for the suggestion. I figured out that I can use > > $ expr match $var "[0-9]*$" > > and it will return >=1 for a valid i

Re: Bash and testing a variable

2003-02-11 Thread Gordon
Robert P. J. Day wrote: On Tue, 11 Feb 2003, Gordon wrote: I've searched all over for an answer to this and haven't found one yet! Is there a way in a bash script to test if a variable contains an integer? I want to create a script that uses some simple arithmetic but bash bombs out if you t

Re: Bash and testing a variable

2003-02-11 Thread Todd A. Jacobs
On Tue, 11 Feb 2003, Gordon wrote: > Is there a way in a bash script to test if a variable contains an > integer? I want to create a script that uses some simple arithmetic but Nope. You'll have to do something more arcane: function IsInt { if egrep -q '[[:digit:]]+' <(echo $1)

Re: Bash and testing a variable

2003-02-11 Thread Robert Tinsley
On Tue, 2003-02-11 at 16:31, Anthony E. Greene wrote: > How about a case statement: > > case $var in >1234567890) i think you mean 1|2|3|4|5|6|7|8|9|0) > do something;; >*) > echo $var is not a number > esac cheers, -- [EMAIL PROTECTED] [ do NOT use the following e-

Re: Bash and testing a variable

2003-02-11 Thread Robert P. J. Day
On Tue, 11 Feb 2003, Gordon wrote: > I've searched all over for an answer to this and haven't found one yet! > > Is there a way in a bash script to test if a variable contains an > integer? I want to create a script that uses some simple arithmetic but > bash bombs out if you try to do a compar

Re: Bash and testing a variable

2003-02-11 Thread Anthony E. Greene
Gordon wrote: I've searched all over for an answer to this and haven't found one yet! Is there a way in a bash script to test if a variable contains an integer? I want to create a script that uses some simple arithmetic but bash bombs out if you try to do a comparison or arithmetic with a non-

Re: Bash and testing a variable

2003-02-11 Thread Roland Roberts
-BEGIN PGP SIGNED MESSAGE- > "Gordon" == Gordon <[EMAIL PROTECTED]> writes: Gordon> I've searched all over for an answer to this and haven't Gordon> found one yet! Is there a way in a bash script to test if Gordon> a variable contains an integer? I want to create a scrip

Re: Bash and testing a variable

2003-02-11 Thread Robert Tinsley
On Tue, 2003-02-11 at 16:07, Gordon wrote: > I've searched all over for an answer to this and haven't found one yet! > > Is there a way in a bash script to test if a variable contains an > integer? I want to create a script that uses some simple arithmetic but > bash bombs out if you try to do a