Unix Shell - integer value of variable

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > integer value of variable





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author integer value of variable
nibbio

2006-10-24, 1:17 pm

Hi to all
i'm writing a bash script that accepts 3 arguments. the second one has
to be an integer number else the script
stops. so i did :

case $2 in
*[!0-9]*) exit 1 ;;
esac

but it works only for number in the range [0-9]. How can I set the case
statement for accepting a generic integer value.Thanks

Icarus Sparry

2006-10-24, 1:17 pm

On Tue, 24 Oct 2006 08:02:08 -0700, nibbio wrote:

> Hi to all
> i'm writing a bash script that accepts 3 arguments. the second one has
> to be an integer number else the script
> stops. so i did :
>
> case $2 in
> *[!0-9]*) exit 1 ;;
> esac
>
> but it works only for number in the range [0-9]. How can I set the case
> statement for accepting a generic integer value.Thanks


It should work for any integer, although it will accept the empty string.
The pattern matches anything that has a non-digit in it.

case $2 in
("") echo "Empty value not allowed" >&2 ; exit 1 ;;
(*[!0-9]*) echo "Non digit not allowed" >&2 ; exit 1 ;;
(*) echo "OK" ;;
esac
Daniel Rock

2006-10-24, 1:17 pm

nibbio <paolo.mozzati@gmail.com> wrote:
> Hi to all
> i'm writing a bash script that accepts 3 arguments. the second one has
> to be an integer number else the script
> stops. so i did :
>
> case $2 in
> *[!0-9]*) exit 1 ;;
> esac
>
> but it works only for number in the range [0-9]. How can I set the case
> statement for accepting a generic integer value.Thanks


A generic (not bash specific) would be by using expr:

expr "$2" : '^[0-9][0-9]*$' > /dev/null || exit 1


--
Daniel
Dundonald

2006-10-24, 1:17 pm


Daniel Rock wrote:

> nibbio <paolo.mozzati@gmail.com> wrote:
>
> A generic (not bash specific) would be by using expr:
>
> expr "$2" : '^[0-9][0-9]*$' > /dev/null || exit 1


Is that restricted to 2 digit integers?

Icarus Sparry

2006-10-24, 1:17 pm

On Tue, 24 Oct 2006 08:40:07 -0700, Dundonald wrote:

> Daniel Rock wrote:
>
>
> Is that restricted to 2 digit integers?


No, but neither was your original solution. Why do you belive that your
original solution does not work?
William

2006-10-24, 1:17 pm

"nibbio" <paolo.mozzati@gmail.com> wrote in message
news:1161702128.468323.86030@e3g2000cwe.googlegroups.com...
> Hi to all
> i'm writing a bash script that accepts 3 arguments. the second one has
> to be an integer number else the script
> stops. so i did :
>
> case $2 in
> *[!0-9]*) exit 1 ;;
> esac
>
> but it works only for number in the range [0-9]. How can I set the case
> statement for accepting a generic integer value.Thanks


I think you're misinterpreting what it does.

This would work for only a single digit:
case $2 in
[0-9]) echo "Single digit" ;;
esac

But yours works for a string of any number of digits, because
it's a negative test - it's looking for any single character
that IS NOT a digit.

By the way, you probably want to modify it slightly unless you
know for certain $2 will never be empty:

case $2 in
"" | *[!0-9]*) exit 1 ;;
esac

-Wm


Bill Marcum

2006-10-25, 1:32 am

On 24 Oct 2006 08:40:07 -0700, Dundonald
<mark.dundon@gmail.com> wrote:
>
> Is that restricted to 2 digit integers?
>

A single [0-9]* would match an empty string or any number of digits.
[0-9][0-9]* matches one or more digits.


--
Perfect day for scrubbing the floor and other exciting things.
nibbio

2006-10-25, 1:27 pm

Thanks for all yours advices. I decided to use the following and it
works perfectly.

> case $2 in
> ("") echo "Empty value not allowed" >&2 ; exit 1 ;;
> (*[!0-9]*) echo "Non digit not allowed" >&2 ; exit 1 ;;
> (*) echo "OK" ;;
> esac


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com