It is currently Mon, 27 Jun 2022 12:43:24 GMT



 
Author Message
 sh - case query

I have a query about the behaviour of `case' in the Bourne shell.
In the following example you of course get one, and only one,
of `A', `B' or `Other' echo'd.

case $var in
  a)  echo "A" ;;
  b)  echo "B" ;;
  *)  echo "Other"
esac

In C-shell, you can omit the equivalent of the `;;' (which is
`breaksw') so that if var is `a', then `A', `B' and `Other' is echo'd,
if var is `b', then `B' and `Other' is echo'd, etc. (Has this been written
clearly enough?).

Is there any way in (k)sh to emulate this behaviour of (csh) case?

In sh, a `;;' must precede the next `REG-EXP)', which leaves me thinking
that the answer to the above question is no.

Can anyone help me in this?

TIA

Adam



 Sat, 28 Aug 1999 03:00:00 GMT   
 sh - case query

|> In C-shell, you can omit the equivalent of the `;;' (which is
|> `breaksw') so that if var is `a', then `A', `B' and `Other' is echo'd,
|> if var is `b', then `B' and `Other' is echo'd, etc. (Has this been written
|> clearly enough?).

|> Is there any way in (k)sh to emulate this behaviour of (csh) case?

No.

|> In sh, a `;;' must precede the next `REG-EXP)', which leaves me thinking
|> that the answer to the above question is no.

Yes.  It's a syntactic requirement, otherwise the parser would not be able
to tell where the next pattern (this is *not* a regexp) starts.
--
Andreas Schwab                                      "And now for something
sch...@issan.informatik.uni-dortmund.de              completely different"



 Sun, 29 Aug 1999 03:00:00 GMT   
 sh - case query

: I have a query about the behaviour of `case' in the Bourne shell.
: In the following example you of course get one, and only one,
: of `A', `B' or `Other' echo'd.
: case $var in
:   a)  echo "A" ;;
:   b)  echo "B" ;;
:   *)  echo "Other"
: esac
: --
: In C-shell, you can omit the equivalent of the `;;' (which is
: `breaksw') so that if var is `a', then `A', `B' and `Other' is echo'd,
: if var is `b', then `B' and `Other' is echo'd, etc. (Has this been written
: clearly enough?).
: Is there any way in (k)sh to emulate this behaviour of (csh) case?
: In sh, a `;;' must precede the next `REG-EXP)', which leaves me thinking
: that the answer to the above question is no.

For sh and ksh88, the answer is indeed no. One may rewrite the
flow-of-control to insert case statements inside case statements, etc,
so simulate this behavior.

Ksh93 has a new operator ";&" which delimits the case statement without
performing a default "break", in the C coding sense.

Thus:

switch ($var)
case a:
        echo "A"
case b:
        echo "B"
default:
        echo "Other"
endsw
--

is equivalent to, in ksh93:

case $var in
a)  echo "A" ;&
b)  echo "B" ;&
*)  echo "Other"
esac
--

-Brian
--
   ,---.     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.  
  /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \  
.'  / \  `.'  / mailto:bsh20...@challenger.atc.fhda.edu \  `.'  / \  `.
__,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.__



 Sun, 29 Aug 1999 03:00:00 GMT   
 sh - case query

  Adam Blair (a...@ltsun7.star.le.ac.uk) wrote:
  : I have a query about the behaviour of `case' in the Bourne shell.
  : In the following example you of course get one, and only one,
  : of `A', `B' or `Other' echo'd.
  : case $var in
  :   a)  echo "A" ;;
  :   b)  echo "B" ;;
  :   *)  echo "Other"
  : esac
  : --
  : In C-shell, you can omit the equivalent of the `;;' (which is
  : `breaksw') so that if var is `a', then `A', `B' and `Other' is
  echo'd,
  : if var is `b', then `B' and `Other' is echo'd, etc. (Has this been
  written
  : clearly enough?).
  : Is there any way in (k)sh to emulate this behaviour of (csh) case?

  : In sh, a `;;' must precede the next `REG-EXP)', which leaves me
  thinking
  : that the answer to the above question is no.

  For sh and ksh88, the answer is indeed no. One may rewrite the
  flow-of-control to insert case statements inside case statements,
  etc,
  so simulate this behavior.

  Ksh93 has a new operator ";&" which delimits the case statement
  without
  performing a default "break", in the C coding sense.

  Thus:

  switch ($var)
  case a:
          echo "A"
  case b:
          echo "B"
  default:
          echo "Other"
  endsw
  --

  is equivalent to, in ksh93:

  case $var in
  a)  echo "A" ;&
  b)  echo "B" ;&
  *)  echo "Other"
  esac
  --

  -Brian
  --
     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.

    /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _
  \
  .'  / \  `.'  / mailto:bsh20...@challenger.atc.fhda.edu \  `.'  / \
  `.
  __,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'
  `.__

 One hesitates to correct the undisputed champion of all things ksh, but
according to both my experience and my reading of Bolsky/Korn the ;&
operator is indeed valid in ksh88. Also, the (original) example contains
a syntax error: the "Other" case should have a ;;.

David



 Thu, 02 Sep 1999 03:00:00 GMT   
 sh - case query

: Brian S Hiles wrote:
: : Adam Blair (a...@ltsun7.star.le.ac.uk) wrote:
: : ...
: case $var in
: a)  echo "A" ;&
: b)  echo "B" ;&
: *)  echo "Other"
: esac

: One hesitates to correct the undisputed champion of all things ksh, but
: according to both my experience and my reading of Bolsky/Korn the ;&
: operator is indeed valid in ksh88.

Early versions of Ksh88 did not implement the ";&" operator, (indeed,
like many features that were implemented in k/sh, but only documented
in the ksh93 revised edition of "The New Kornshell: ..."). My
preference is to give answers compatible with the documented standard,
as I am all too aware of how many subtle distinctions there exist
between ksh versions -- indeed between ksh _ports_ -- there are. You
would be indeed surprised at all the sundry undocumented features/syntax
of sh and ksh I have discovered.

: Also, the (original) example contains a syntax error: the "Other"
: case should have a ;;.

This is exactly why I try to give "vanilla" answers. All versions of
ksh and sh will execute the above code without complaint; the ";;" is
optional before the terminating "esac". Did you try out the code?

-Brian
--
   ,---.     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.  
  /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \  
.'  / \  `.'  / mailto:bsh20...@challenger.atc.fhda.edu \  `.'  / \  `.
__,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.__



 Sat, 04 Sep 1999 03:00:00 GMT   
 
   [ 5 post ] 

Similar Threads

1. Case recommendation -- off topic query

2. Case statement range query

3. sh newb: case menus w/scripts

4. Is there switch and case in sh script?

5. case insensitive compare in sh??

6. sh question regarding case

7. bourne sh, problems with var setting in case/esac

8. simple query in sh --anyone

9. upper case vs lower case account names

10. Upper case to lower case


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software