Article View: comp.unix.shell
Article #4037Re: Using an If Statement to Check One of Three Possible Values
From: Bill Marcum
Date: Tue, 11 Jul 2006 20:49
Date: Tue, 11 Jul 2006 20:49
37 lines
2390 bytes
2390 bytes
["Followup-To:" header set to comp.unix.shell.]
On 11 Jul 2006 13:20:11 -0700, kvnsmnsn@hotmail.com
<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" ]
The "[" command must end with "]". "||" separates two commands.
The "if" statement above can be rewritten in three ways:
if [ $1 == "a" ] || [ $1 == "b" ] || [ $1 == "c" ]
if [ $1 == "a" -o $1 == "b" -o $1 == "c" ]
case $1 of
a|b|c) ... ;;
*) ... ;;
esac
--
Maybe you can't buy happiness, but these days you can certainly charge it.
Technical Details
Message-ID:
<0gfdo3-r5f.ln1@don.localnet>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns14feed!worldnet.att.net!198.6.0.12!spool.news.uu.net!ash.uu.net!news.iglou.com!don.localnet!newsReferences:
<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 Newsgroups: comp.unix.shell,gnu.bash,alt.comp.lang.shell.unix.bourne-bash From: Bill Marcum <bmarcum@iglou.com> Subject: Re: Using an If Statement to Check One of Three Possible Values References: <1152649211.167708.83330@m79g2000cwm.googlegroups.com> Followup-To: comp.unix.shell User-Agent: slrn/0.9.8.1pl1 (Debian) Date: Tue, 11 Jul 2006 16:49:36 -0400 Message-ID: <0gfdo3-r5f.ln1@don.localnet> NNTP-Posting-Host: lou-ts9-50.iglou.com X-Original-NNTP-Posting-Host: lou-ts9-50.iglou.com X-Trace: news.iglou.com 1152653967 lou-ts9-50.iglou.com (11 Jul 2006 17:39:27 -0400) Lines: 37 X-Authenticated-User: bmarcum X-Original-NNTP-Posting-Host: 204.255.236.65 Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns14feed!worldnet.att.net!198.6.0.12!spool.news.uu.net!ash.uu.net!news.iglou.com!don.localnet!news