Path: 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> Sender: news@usenet.ins.cwru.edu Reply-To: chet@po.CWRU.Edu Organization: Case Western Reserve Univ. Cleveland, Ohio, (USA) Lines: 77 In article 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