how to test two strings (which are numbers) for numeric equality?

how to test two strings (which are numbers) for numeric equality?

am 26.09.2007 23:42:41 von terrence.x.brannon

LINES=`wc -l $1 | cut --delimiter ' ' --fields 1`
LINES=`expr $LINES - 1`

LAST_LINE=`tail -1 $1 | cut --fields 4`


expr `$LINES - $LAST_LINE` # expr: non-numeric argument
# I really want to test if
$LINES == $LAST_LINE

Re: how to test two strings (which are numbers) for numeric equality?

am 26.09.2007 23:51:56 von cfajohnson

On 2007-09-26, terrence.x.13 wrote:
> LINES=`wc -l $1 | cut --delimiter ' ' --fields 1`
> LINES=`expr $LINES - 1`

You don't need cut or expr to do that in a POSIX shell:

LINES=$(( $( wc -l < "$1" ) - 1 ))

> LAST_LINE=`tail -1 $1 | cut --fields 4`
>
>
> expr `$LINES - $LAST_LINE` # expr: non-numeric argument
> # I really want to test if
> $LINES == $LAST_LINE

if [ $LINES -eq $LAST_LINE ]
then
echo same
else
echo different
fi


--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: how to test two strings (which are numbers) for numericequality?

am 27.09.2007 08:30:03 von Loki Harfagr

Wed, 26 Sep 2007 17:51:56 -0400, Chris F.A. Johnson did cat :

> On 2007-09-26, terrence.x.13 wrote:
>> LINES=`wc -l $1 | cut --delimiter ' ' --fields 1` LINES=`expr $LINES -
>> 1`
>
> You don't need cut or expr to do that in a POSIX shell:
>
> LINES=$(( $( wc -l < "$1" ) - 1 ))
>
>> LAST_LINE=`tail -1 $1 | cut --fields 4`
>>
>>
>> expr `$LINES - $LAST_LINE` # expr: non-numeric argument
>> # I really want to test if
>> $LINES == $LAST_LINE
>
> if [ $LINES -eq $LAST_LINE ]
> then
> echo same
> else
> echo different
> fi

Or the risky way:
# (( $LINES == $LAST_LINE )) && echo same || echo different

Re: how to test two strings (which are numbers) for numeric equality?

am 28.09.2007 05:07:05 von cfajohnson

On 2007-09-27, Loki Harfagr wrote:
> Wed, 26 Sep 2007 17:51:56 -0400, Chris F.A. Johnson did cat :
>
>> On 2007-09-26, terrence.x.13 wrote:
>>> LINES=`wc -l $1 | cut --delimiter ' ' --fields 1` LINES=`expr $LINES -
>>> 1`
>>
>> You don't need cut or expr to do that in a POSIX shell:
>>
>> LINES=$(( $( wc -l < "$1" ) - 1 ))
>>
>>> LAST_LINE=`tail -1 $1 | cut --fields 4`
>>>
>>>
>>> expr `$LINES - $LAST_LINE` # expr: non-numeric argument
>>> # I really want to test if
>>> $LINES == $LAST_LINE
>>
>> if [ $LINES -eq $LAST_LINE ]
>> then
>> echo same
>> else
>> echo different
>> fi
>
> Or the risky way:
> # (( $LINES == $LAST_LINE )) && echo same || echo different

That's not risky; it's just not portable.

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: how to test two strings (which are numbers) for numericequality?

am 28.09.2007 10:22:27 von Loki Harfagr

On Thu, 27 Sep 2007 23:07:05 -0400, Chris F.A. Johnson wrote:

> On 2007-09-27, Loki Harfagr wrote:
>> Wed, 26 Sep 2007 17:51:56 -0400, Chris F.A. Johnson did cat :
>>
>>> On 2007-09-26, terrence.x.13 wrote:
>>>> LINES=`wc -l $1 | cut --delimiter ' ' --fields 1` LINES=`expr $LINES
>>>> - 1`
>>>
>>> You don't need cut or expr to do that in a POSIX shell:
>>>
>>> LINES=$(( $( wc -l < "$1" ) - 1 ))
>>>
>>>> LAST_LINE=`tail -1 $1 | cut --fields 4`
>>>>
>>>>
>>>> expr `$LINES - $LAST_LINE` # expr: non-numeric argument
>>>> # I really want to test
>>>> if
>>>> $LINES == $LAST_LINE
>>>
>>> if [ $LINES -eq $LAST_LINE ]
>>> then
>>> echo same
>>> else
>>> echo different
>>> fi
>>
>> Or the risky way:
>> # (( $LINES == $LAST_LINE )) && echo same || echo different
>
> That's not risky; it's just not portable.

Which is the very reason why in my admin life I quickly
had to find out I'd better consider it risky :-)
Now, in my normal user life I consider it has an easier
look for my eyes but at home I'm not continuousky jumping
from one OS style to another.

Anyway you were right to make the precision, as some users would've
been curious about how a syntax could be "risky"!

Re: how to test two strings (which are numbers) for numeric equality?

am 29.09.2007 00:16:45 von Michael Tosch

terrence.x.13 wrote:
> LINES=`wc -l $1 | cut --delimiter ' ' --fields 1`
> LINES=`expr $LINES - 1`
>
> LAST_LINE=`tail -1 $1 | cut --fields 4`
>
>
> expr `$LINES - $LAST_LINE` # expr: non-numeric argument
> # I really want to test if
> $LINES == $LAST_LINE
>

Shouldn't it be
LINES=`wc -l $1`
?

LAST_LINE=`tail -1 $1 | cut --fields 4`
seems not to get a number?
Maybe you want
LAST_LINE=`tail -1 $1 | awk '{print $4}`

if [ "$LINES" -eq "$LAST_LINE" ]
then
echo same
else
echo different
fi


--
Michael Tosch @ hp : com

Re: how to test two strings (which are numbers) for numeric equality?

am 29.09.2007 15:17:08 von Spiros Bousbouras

On 28 Sep, 23:16, Michael Tosch
wrote:
> terrence.x.13 wrote:
> > LINES=`wc -l $1 | cut --delimiter ' ' --fields 1`
> > LINES=`expr $LINES - 1`
>
> > LAST_LINE=`tail -1 $1 | cut --fields 4`
>
> > expr `$LINES - $LAST_LINE` # expr: non-numeric argument
> > # I really want to test if
> > $LINES == $LAST_LINE
>
> Shouldn't it be
> LINES=`wc -l $1`
> ?

I'm not using a Unix system right now but I believe this will also
put the file name inside LINES. So the code by Terrence uses cut
to get just the number but what Chris Johnson did was rather clever:
write wc -l < $1 in which case there is no file name.

Re: how to test two strings (which are numbers) for numeric equality?

am 29.09.2007 16:26:04 von Michael Tosch

Spiros Bousbouras wrote:
> On 28 Sep, 23:16, Michael Tosch
> wrote:
>> terrence.x.13 wrote:
>>> LINES=`wc -l $1 | cut --delimiter ' ' --fields 1`
>>> LINES=`expr $LINES - 1`
>>> LAST_LINE=`tail -1 $1 | cut --fields 4`
>>> expr `$LINES - $LAST_LINE` # expr: non-numeric argument
>>> # I really want to test if
>>> $LINES == $LAST_LINE
>> Shouldn't it be
>> LINES=`wc -l $1`
>> ?
>
> I'm not using a Unix system right now but I believe this will also
> put the file name inside LINES. So the code by Terrence uses cut
> to get just the number but what Chris Johnson did was rather clever:
> write wc -l < $1 in which case there is no file name.
>
>

Oh yes, of course

LINES=`wc -l < $1`


--
Michael Tosch @ hp : com

Re: how to test two strings (which are numbers) for numeric equality?

am 29.09.2007 16:35:58 von Stephane CHAZELAS

2007-09-29, 16:26(+02), Michael Tosch:
[...]
> Oh yes, of course
>
> LINES=`wc -l < $1`
[...]

LINES=`wc -l < "$1"`

as some shells like bash or POSIX ones in interactive mode may
perform globbing upon the expansion of $1 otherwise.

--
Stéphane