|
Home > Archive > Unix Shell > September 2007 > how to test two strings (which are numbers) for numeric equality?
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 |
how to test two strings (which are numbers) for numeric equality?
|
|
| terrence.x.13 2007-09-26, 7:20 pm |
| 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
| |
| Chris F.A. Johnson 2007-09-26, 7:20 pm |
| 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 <http://cfaj.freeshell.org/shell/>
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
| |
| Chris F.A. Johnson 2007-09-28, 1:30 am |
| On 2007-09-27, Loki Harfagr wrote:
> Wed, 26 Sep 2007 17:51:56 -0400, Chris F.A. Johnson did cat_:
>
>
> 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 <http://cfaj.freeshell.org/shell/>
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
| |
| Michael Tosch 2007-09-28, 7:21 pm |
| 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
| |
| Spiros Bousbouras 2007-09-29, 1:19 pm |
| On 28 Sep, 23:16, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:
> terrence.x.13 wrote:
>
>
>
> 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.
| |
| Stephane CHAZELAS 2007-09-29, 1:19 pm |
| 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
|
|
|
|
|