Article View: comp.unix.shell
Article #4036Re: Using an If Statement to Check One of Three Possible Values
From: Chris F.A. Johnson
Date: Tue, 11 Jul 2006 20:32
Date: Tue, 11 Jul 2006 20:32
58 lines
3156 bytes
3156 bytes
On 2006-07-11, kvnsmnsn@hotmail.com wrote:
> I'm trying to build a bash script that creates an "x.java" and a
> "y.java" file from three existing pairs of files, depending on whether
> an "a", a "b", or a "c" is entered on the command line. My bash code
> checks to see if the number of arguments entered on the command line
> is one, which check it handles fine, but if it passes that check and
> goes on to the next "if" statement, it gives me the complaints:
>
> Hmm.sh: line 4: [: missing `]'
> Hmm.sh: line 4: a: command not found
> Hmm.sh: line 4: a: command not found
> Incorrect operand "a".
>
> My actual code is:
>
> #! /bin/sh
> if [ $# == 1 ]
> then
> if [ $1 == "a" || $1 == "b" || $1 == "c" ]
if [ $1 == "a" ] || [ $1 == "b" ] || [ $1 == "c" ]
Or:
if [ $1 == "a" -o $1 == "b" -o $1 == "c" ]
> then
> cp x_$1.java x.java
> cp y_$1.java y.java
> else
> echo "Incorrect operand \"$1\"."
> fi
> else
> echo "Usage is"
> echo " sh Hmm.sh <suffix>"
> fi
>
> Can anyone see what I'm doing wrong here? Any pointers would be
> greatly appreciated.
Simpler is a case statement:
case $1 in
a|b|c) cp x_$1.java x.java
cp y_$1.java y.java
;;
*? ) echo "Incorrect operand \"$1\"."
;;
*) echo "Usage is"
echo " sh Hmm.sh <suffix>"
;;
esac
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
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
Technical Details
Message-ID:
<jfedo3-ga7.ln1@xword.teksavvy.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.teksavvy.com!news.teksavvy.com.POSTED!not-for-mailReferences:
<1152649211.167708.83330@m79g2000cwm.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1012a6,451bb3cca7eb9094 X-Google-Thread: 1054c0,451bb3cca7eb9094 X-Google-Thread: 114230,451bb3cca7eb9094 X-Google-Attributes: gid1012a6,gid1054c0,gid114230,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.teksavvy.com!news.teksavvy.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 11 Jul 2006 15:33:03 -0500 Newsgroups: comp.unix.shell,gnu.bash,alt.comp.lang.shell.unix.bourne-bash From: "Chris F.A. Johnson" <cfajohnson@gmail.com> Subject: Re: Using an If Statement to Check One of Three Possible Values References: <1152649211.167708.83330@m79g2000cwm.googlegroups.com> Reply-To: cfajohnson@gmail.com User-Agent: slrn/0.9.8.1 (Linux) Date: Tue, 11 Jul 2006 16:32:19 -0400 Message-ID: <jfedo3-ga7.ln1@xword.teksavvy.com> Lines: 58 NNTP-Posting-Host: 206.248.139.163 X-Trace: sv3-ueBkQExQbtkDBWnojYLJQhX19K8YvpFHKnvQrAspJQVlAAqrbEVTFgY9OMJqbGD+9y2W6DOYmsj3tgg!zTjdp/e6dZRKlC0plcLK6ej6U9ym89M+kb7tZRGmiKcnZvFf6XBJz8637MEhWdF0cf9ObzUv2dQS!bA== X-Complaints-To: abuse@teksavvy.com X-DMCA-Complaints-To: abuse@teksavvy.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32