csh code to ksh code

I have this code using csh and want to convert it to ksh to include this thinking into my ksh scripts.

     while ( $iarg < $narg )
    MATH iarg = $iarg + 1
    set arg = $argv[$iarg]
    set opt=` echo $arg | awk 'BEGIN { FS="=" } { print $1 }' `
    set par=` echo $arg | awk 'BEGIN { FS="=" } { print $2 }' `
    switch ($opt)
        case "data":
            set Data=$par
            echo "-Rrms " $Data
            set optdata=1
            breaksw
        case "-Rrms":
            set Rrms=$par
            echo "-Rrms " $Rrms
            set optrms=1
            breaksw
        case "-Rbst":
            set Rbst = $par
            echo "-Rbst " $Rbst
            set optbst=1
            breaksw
        default:
            echo "Unrecognised Option"
            set ierr=1
            breaksw
     endsw
  end
  

Hi.

Some automated conversion may be done with:

# cshtobash - convert csh aliases, environment variables, and variables to
#	      bash equivalents

http://sysinf0.klabs.be/usr/doc/bash/examples/misc/cshtobash.gz?dist=;arch=

A perl code that claims to do the conversion can be found at:
http://www.linux-kheops.com/doc/perl/perl-aubert/csh2sh-rca.pl

But you will probably need to do some manual work as well. See the first few columns of Rosetta Stone for Scripting Languages ... cheers, drl

1 Like
while [ $iarg -lt $narg ]
do
  iarg=$(( $iarg + 1 ))
  arg=$argv[$iarg]

  opt=${arg%%=*}
  par=${arg#*=} ## if there's more than one '=', this is a little more complex

  case $opt in
     "data")
         Data=$par
         echo "-Rrms $Data"
         optdata=1
         ;;
   "-Rrms")
        Rrms=$par
        echo "-Rrms $Rrms"
        optrms=1
        ;;
    "-Rbst")
        Rbst = $par
        echo "-Rbst " $Rbst
        optbst=1
        ;;
      *)
        echo "Unrecognised Option"
        ierr=1
        ;;
  esac
done

If I knew a little more about the script, I'd probably recommend using getopts to parse command-line options.