It is currently Mon, 27 Jun 2022 12:12:46 GMT



 
Author Message
 Is there switch and case in sh script?

Hello,
This sh script doesn't work yet.  When I try to run it, it keeps saying that
'switch is unexpected'.  Does that mean 'switch' can be used in sh script?
Any suggestion is very appreciated.

#! /bin/sh
############################################################################
#  Purpose:using the UNIX utililies into a Cshell script to print the word
#          backward or forward depend on the flag on the commend line
#  There are 3 possible options:
#         -r  print the line reverse order
#         -f  print the line in the original order
#         -o  use the next argument as the output file
############################################################################
set inputfile = \(\)
set outputfile = ''
set flag = ''
set file = ''
@ counto = 0

switch ($#argv)
   case 0 :
      echo " Warning :No file name or option on argument list "
      exit (1)
      breaksw
   default :
      foreach arg ( $argv)
         switch ($arg)
            case -r:
               set flag = r
               breaksw
            case -f:
               set flag = f
               set foundf
               breaksw
            case -o:
               set flag = o
               set foundo
               set file = nooutfile
               @ counto ++
               if ( $counto >1) then
                  echo command $argv illegal.
                  exit (1)
               endif
               breaksw
            default :
               if ($?foundo ) then
                  if ( $flag == o) then
                     set outputfile = "$arg"
                     set file = outfile
                     unset foundo
                  else
                     echo command $argv illegal
                     exit (1)
                  endif
               else if ( -r $arg ) then
                       set inputfile = " $inputfile $arg "
                       set fileexit
                    else
                       echo " $arg : file not readable"
                    endif
               endif
               breaksw
            endsw
         end
      switch ( $flag )
         case r :
            if ($file == outfile ) then
               if ( -e $outputfile ) then
                  echo The file $outputfile already exits.
                  exit ( 1 )
               else
                  cat $inputfile| awk 'BEGIN{} NF!=0 {i=NF; while (i!=0) { if (i==NF) printf ("%s",$i)\
 else printf (" %s",$i); i--; } printf("\n");} END{}'|cat > $outputfile
               endif
            else
               cat $inputfile| awk 'BEGIN{} NF!=0 {i=NF;while (i!=0) { if (i==NF) printf ("%s",$i)\
else printf (" %s",$i); i--; } printf("\n");} END{}'
            endif

            breaksw
         case f :
            if ($file == outfile ) then
               if ( -e $outputfile ) then
                  echo The file $outputfile already exits.
                  exit ( 1 )
               else
                  cat $inputfile| awk 'BEGIN{} NF!=0 {i=1; while (i<=NF) { if (i ==NF) printf ("%s",$i)\
 else printf ("%s ",$i); i++; } printf("\n");} END{}'|cat > $outputfile
               endif
            else
               cat $inputfile| awk 'BEGIN{} NF!=0 {i=1;while (i<=NF) { if (i==NF) printf ("%s",$i)\
else printf ("%s ",$i); i++; } printf("\n");} END{}'
            endif
            breaksw
        case o :
           switch ($file)
              case outfile:
                 if ( -e $outputfile ) then
                    echo the file $outputfile already exits.
                    exit ( 1 )
                 else if ($?foundf) then
                         cat $inputfile| awk 'BEGIN{} NF!=0 {i=1; while (i<=NF) { if (i==NF) printf ("%s",$i)\
 else printf ("%s ",$i); i++; } printf("\n");}END{}'|cat > $outputfile
                      else
                         cat $inputfile| awk 'BEGIN{} NF!=0 {i=NF; while (i!=0) { if (i==NF) printf ("%s",$i)\
 else printf (" %s",$i); i--; } printf("\n");} END{}'|cat > $outputfile
                      endif
                 endif
                 breaksw
              case nooutfile :
                 echo there is an -o option but no outfile indicated
                 if ($?foundf) then
                    cat $inputfile| awk 'BEGIN{} NF!=0 {i=1; while (i<=NF) { if (i==NF) printf ("%s",$i)\
 else printf ("%s ",$i); i++; } printf("\n");}END{}'
                 else if ( $?fileexit ) then
                         cat $inputfile| awk 'BEGIN{} NF!=0 {i=NF; while (i!=0) { if (i==NF) printf ("%s",$i)\
 else printf (" %s",$i); i--; } printf("\n");} END{}'
                      else
                         echo also no input file indicated
                         exit (1)
                      endif
                 endif
                 breaksw
              endsw
            breaksw
        default :
           cat $inputfile| awk 'BEGIN{} NF!=0 {i=NF; while (i!=0) { if (i==NF) printf ("%s",$i)\
 else printf (" %s",$i); i--; } printf("\n");} END{}'
           breaksw
        endsw
   breaksw
endsw



 Tue, 08 Jun 1999 03:00:00 GMT   
 Is there switch and case in sh script?

In the beginning, or at least on 20 Dec 1996 04:20:01 GMT,  MASC0102
<masc0...@rohan.sdsu.edu?> proclaimed ...

case "$I" in
        -f) echo F flag;;
        -c) echo C flag;;
        -q) echo Q flag;;
        -*) echo unknown flag;;
        *) echo everything else;
esac

--
 Don Read                      sy...@calcasieu.com
 EDP Manager                       dr...@texas.net
 Calcasieu Lumber Co.                    Austin TX
  ---- "Ya jus' mash that butt'n, righ' jere"  ---



 Wed, 09 Jun 1999 03:00:00 GMT   
 Is there switch and case in sh script?

In <59d45h$...@gondor.sdsu.edu> masc0...@rohan.sdsu.edu (MASC0102) writes:

Yup. 'switch' is csh syntax. In fact a lot of your script is in csh and will
not work under sh.

I am not going to rewrite the entire script for you, but the same
functionality can be achieved quite easily in sh. Read the friendly manpage
or a book for more details on sh syntax.

The following sh script parses the arguments and sets various flags. I have
omitted the various checks on the filename argument. You can take it from
there.

#!/bin/sh
case $# in
   0) echo "Warning :No file name or option on argument list " 1>&2; exit 1;;
esac

while true
do
   case $1 in
     -r)
         flag=r
         shift;;

     -f)
         flag=f
         shift;;

     -o)
         shift
         file=$1
         flag=o
         shift;

     "")
         break;;

      *)
         echo "Cannot understand argument:" $1 ". Goodbye."
         exit 1;;
   esac
done

Regards,
Gora



 Wed, 09 Jun 1999 03:00:00 GMT   
 Is there switch and case in sh script?

In article <59d45h$...@gondor.sdsu.edu>, masc0...@rohan.sdsu.edu

Switch is a Csh concept. The Bourne shell uses the Case statement, as
others have posted that solution to you already. However, for what you
want, you might consider Kornshell and it's getopts function. Getopts is
Kornshell's way of parsing command line flags. You usually see it within a
while loop and case statement. The Getopts function allows you to combine
the flags (-rfo would be legal in your case, and allows you to. It's a
little hard to explain, but here it is:

USAGE="Here's how to use my program...

        blah, blah, blah
"

while getopts :rfo: option
do
    case $option in

        r) rflag=true;;
        f) fflag=true;;
        +f) plusFFlag=true;;
        o) oflag=true
           file=$OPTARG
           ;;
        *) print $USAGE
           exit 2
           ;;
    esac
done

shift OPTIND - 1

The :rfo: tells getopts what are valid flags. The colon in front tells
getopts not to print an error message if an invalid flag is found. Each
valid flag is listed, and if a flag requires an option, that flag is
follow by a colon.

The flags are "set" in the case statement. Notice I added a "+f" line.
This was NOT part of your requirements, I just put that in to show you how
to check for command line parameters that start with a "+" sign.

Standard Kornshell procedure says you set the flags you want, then check
them further in your code. The idea is to keep the parsing of the flags
clean as possible. It also tends to greatly simplify your code.

Hope this helps.

--
David Weintraub                    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Deutsche Bank, NA                 _/                                      _/
dw...@dbna.com                   _/    I AM THE GREAT AND POWERFUL OZ*   _/
dav...@cnj.digex.net            _/                                      _/
                               _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
                          *(Pay no attention to the man behind the curtains)



 Wed, 09 Jun 1999 03:00:00 GMT   
 
   [ 4 post ] 

Similar Threads

1. sh newb: case menus w/scripts

2. Case and Switch in csh script

3. Script to Convert Upper Case Filenames to Lower Case

4. script to change lower case to upper case

5. Execute a sh script under perl and sh: sh script; perl script?

6. make sh-script compatible between SUN-sh and bash

7. I AM GONNA PUKE - I AM GONNA PUKE - I AM GONNA PUKE - I AM GONNA PUKE - I AM GONNA PUKE - I AM GONNA PUKE - I AM GONNA PUKE -

8. sh - case query

9. case insensitive compare in sh??

10. sh question regarding case


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