Article View: comp.unix.shell
Article #1077Re: bourne shell query
From: Chet Ramey
Date: Thu, 06 Sep 1990 14:11
Date: Thu, 06 Sep 1990 14:11
76 lines
1846 bytes
1846 bytes
In article <SCOTT.90Sep5124415@tab29.larc.nasa.gov> scott@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@ins.CWRU.Edu
Technical Details
Message-ID:
<1990Sep6.141133.3391@usenet.ins.cwru.edu>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!tut.cis.ohio-state.edu!usenet.ins.cwru.edu!cwns1!chetReferences:
<75@iconsys> <925@hls0.hls.oz> <SCOTT.90Sep5124415@tab29.larc.nasa.gov>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!tut.cis.ohio-state.edu!usenet.ins.cwru.edu!cwns1!chet From: chet@cwns1.CWRU.EDU (Chet Ramey) Newsgroups: comp.unix.shell Subject: Re: bourne shell query Message-ID: <1990Sep6.141133.3391@usenet.ins.cwru.edu> Date: 6 Sep 90 14:11:33 GMT References: <75@iconsys> <925@hls0.hls.oz> <SCOTT.90Sep5124415@tab29.larc.nasa.gov> Sender: news@usenet.ins.cwru.edu Reply-To: chet@po.CWRU.Edu Organization: Case Western Reserve Univ. Cleveland, Ohio, (USA) Lines: 77