From: Janis Papanagnou on
Francis Moreau wrote:
> On Mar 13, 9:17 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> wrote:
>> Francis Moreau wrote:
>>> On Mar 13, 12:39 pm, Stephane CHAZELAS <stephane_chaze...(a)yahoo.fr>
>>> wrote:
>>>> 2010-03-13, 10:42(+00), SM:
>>>> [...]
>>>>> I came across this kind of hack:
>>>>> is_int() {
>>>>> printf "%d" $1 > /dev/null 2>&1
>>>> missing quotes above.
>>>>> return $?
>>>> redundant and missing quotes.
>>> why are quotes missing ?
>> In the first case (printf) any kind of argument may be passed and
>> create any amount of trouble or inaccuracy depending on the passed
>> argument; it's the word splitting issue. In the second case $? the
>> quotes are not necessary since $? will contain a numerical value.[*]
>
> I was asking about the second case, which doesn't seem to need any
> quotes (as you said).

Yes. And does not even need the whole return command. The discussion
on the quotes in this context is thus quite void. (For the academical
reason why to quote $? in such a context see pk's response. See also
my other response about the necessity of $? in general.)

Janis
From: Dominic Fandrey on
On 13/03/2010 08:48, Decare wrote:
>
> Is there any to determine whether a string
> is a integer or not? For example,
>
> read s
> if [ ... ]; then
> do something
> else
> do another
> fi


I'm surprised nobody suggested regular expressions.

I use the following functions under FreeBSD (Almquist Shell derivative):

#
# Checks whether the given parameter is an integer.
# Integers may be signed, but there must not be any spaces.
#
# @param 1
# The parameter to check.
# @return
# 0 for integers, 1 for everything else.
#
isInt() {
echo "$1" | egrep -qxe "(-|\+)?[0-9]+"
}

#
# Checks whether the given parameter is an unsigned integer.
#
# @param 1
# The parameter to check.
# @return
# 0 for unsigned integers, 1 for everything else.
#
isUInt() {
echo "$1" | egrep -qxe "\+?[0-9]+"
}

#
# Checks whether the given parameter is a floating point value.
# Floats may be signed, but there must not be any spaces.
# This function does not obey the locale.
#
# The following are examples for valid floats:
# 1
# 1.0
# -1.5
# 1000
# 1e3 = 1000
# 1e-3 = 0.001
# -1e-1 = -0.1
# +1e+2 = 100
#
# @param 1
# The parameter to check.
# @return
# 0 for floats, 1 for everything else.
#
isFloat() {
echo "$1" | egrep -qxe "(-|\+)?[0-9]+(\.[0-9]+)?(e(-|\+)?[0-9]+)?"
}

#
# Checks whether the given parameter is a simple floating point value.
# Simple floats may be signed, but there must not be any spaces.
# This function does not obey the locale.
#
# The following are examples for valid simple floats:
# 1
# 1.0
# -1.5
# 1000
#
# @param 1
# The parameter to check.
# @return
# 0 for simple floats, 1 for everything else.
#
isSimpleFloat() {
echo "$1" | egrep -qxe "(-|\+)?[0-9]+(\.[0-9]+)?"
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Janis Papanagnou on
Dominic Fandrey wrote:
> On 13/03/2010 08:48, Decare wrote:
>> Is there any to determine whether a string
>> is a integer or not? For example,
>>
>> read s
>> if [ ... ]; then
>> do something
>> else
>> do another
>> fi
>
>
> I'm surprised nobody suggested regular expressions.

Nobody? Ivan, the very first who replied, used a grep approach like you,
myself I used regular expressions as supported by extended globbing in
modern shells, and Stephane used regular expressions in a case construct.

Janis

>
> I use the following functions under FreeBSD (Almquist Shell derivative):
> [...]
From: Stephane CHAZELAS on
2010-03-14, 21:35(+01), Dominic Fandrey:
[...]
> # Checks whether the given parameter is an integer.
> # Integers may be signed, but there must not be any spaces.
> #
> # @param 1
> # The parameter to check.
> # @return
> # 0 for integers, 1 for everything else.
> #
> isInt() {
> echo "$1" | egrep -qxe "(-|\+)?[0-9]+"
> }
[...]

That will say that "foo<LF>1" is an integer, or that "\062" is
an integer (with an Unix echo), the result for "12\c" is
unspecified (unterminated line)...

you may want instead:

isInt() {
awk 'BEGIN { exit !(ARGV[1] ~ /^[-+]?[0-9]+$/) }' "$1"
}


--
Stéphane
From: pk on
Stephane CHAZELAS wrote:

> 2010-03-14, 21:35(+01), Dominic Fandrey:
> [...]
>> # Checks whether the given parameter is an integer.
>> # Integers may be signed, but there must not be any spaces.
>> #
>> # @param 1
>> #The parameter to check.
>> # @return
>> #0 for integers, 1 for everything else.
>> #
>> isInt() {
>> echo "$1" | egrep -qxe "(-|\+)?[0-9]+"
>> }
> [...]
>
> That will say that "foo<LF>1" is an integer, or that "\062" is
> an integer (with an Unix echo), the result for "12\c" is
> unspecified (unterminated line)...
>
> you may want instead:
>
> isInt() {
> awk 'BEGIN { exit !(ARGV[1] ~ /^[-+]?[0-9]+$/) }' "$1"
> }

I suppose this might work too:

isInt() {
awk 'BEGIN { exit (ARGV[1]+0 != ARGV[1]) }' "$1"
}

although it will recognize numbers in scientific format, which may or may
not be what one wants.