bourne shell query

Chet Ramey chet at cwns1.CWRU.EDU
Fri Sep 7 00:11:33 AEST 1990


In article <SCOTT.90Sep5124415 at tab29.larc.nasa.gov> scott at tab29.larc.nasa.gov (Scott Yelich) writes:

>1) if [ "$1" = "$2" ]; then
>     echo "Yeas!"
>   else
>     echo "No-way!"
>   fi

What if $1 = '=' or '-t'?

>2) test "$1" = "$2" && echo "Yeas!" || echo "No-way!"

cwns1$ foo()
> {
> /bin/test "$1" = "$2" && echo "Yeas" || echo "No-way"
> }
cwns1$ foo -r -r
test: too many arguments
No-way

>What about?
>
>3) if [ "$1" ]; then
>     echo "Yup!"
>   fi

What if $1 = `-f' or `-r'?

cwns1$ foo()
> {
> if [ "$1" ] ; then
> echo yup
> fi
> }
cwns1$ foo a
yup
cwns1$ foo -r
[: argument expected

>4) eval ${1:+'echo "Yup!"'}

This *is* a nice way to test whether or not a variable is set.

Nobody seems to consider the case statement, which avoids these problems:

equal()
{
	case "$1" in
		"$2")	return 0
			;;
		*)	return 1
			;;
	esac
}

cwns1$ equal a b
cwns1$ echo $?
1
cwns1$ equal a a
cwns1$ echo $?
0
cwns1$ equal -r -r
cwns1$ echo $?
0

>Now, does anyone have and BOURNE SHELL (ie: /bin/sh) routines to do math?

Well, the shell running as /bin/sh on my machine is bash, and it has
$[] to do arithmetic substitution.

Chet
-- 
Chet Ramey				``Levi Stubbs' tears run down
Network Services Group			  his face...''
Case Western Reserve University	
chet at ins.CWRU.Edu		



More information about the Comp.unix.shell mailing list