Help on command line argument in csh

HI ,
I am new to csh. I need to pass some command line arguments like
./abc.sh -os Linux -path abc -tl aa -PILX 1
I have defined the loop as shown below. But its taking "-os" switches as arguments. Its treating them as arguments.
How to resolve it?

while ( $#argv != 0 )
   switch ($argv[1])
     "-kit":
       KIT=$2;
       echo "Proc arg missing"
       breaksw
     "-type":
       TYPE=$1;
       echo "TYPE is $1"
       breaksw
     "-tl":
       TL=$1;
       echo "TL is $1"
       breaksw
     "-PILX":
       PILX=$1;
       echo "PIL is $1"
       breaksw
     default:
       echo "Invalid arg"
       breaksw
   endsw

Should i use getopt? if yes then how? Please help me.

Try this...

#!/bin/bash
while [ $# -gt 0 ]
do
 arg=$1; shift
 val=$1; shift
 case $arg in
   "-os")OS=$val
   ;;
   "-path")VPATH=$val
   ;;
   "-tl")TL=$val
   ;;
   "-PILX")PILX=$val
   ;;
 esac
done
 
echo "OS : $OS"
echo "VPATH : $VPATH"
echo "TL : $TL"
echo "PILX : $PILX"

AFAIK, I don't think we can use getopt for options spanning more than one char i.e. -path or -os etc.
Can we?

--ahamed

Thanks ahamed but I need in csh not bash.

Ok, I just saw the "csh" part. I am not sure if this will work in "csh" shell.

--ahamed

No, it wont work. I tried that already.

Are you not missing the "case" keyword?

#!/usr/bin/csh
while ( $#argv != 0 )
   switch ( $argv[1] )
     case "-kit":
       KIT=$2;
       echo "Proc arg missing"
       shift
       breaksw
     case "-type":
       TYPE=$1;
       echo "TYPE is $1"
       breaksw
     case "-tl":
       TL=$1;
       echo "TL is $1"
       breaksw
     case "-PILX":
       PILX=$1;
       echo "PIL is $1"
       breaksw
     default:
       echo "Invalid arg"
       breaksw
   endsw
   shift
end

And why does it have to be C-Shell? Why not use a proper shell?!

Why do some options in the example command you posted not appear in the switch statement?

1 Like

CSH

#!/bin/csh
while ( $#argv != 0 )
  switch ($argv[1])
    case "-kit":
       set KIT=$2;
        echo "KIT is $KIT"
        breaksw
    case "-type":
      set TYPE=$2;
      echo "TYPE is $TYPE"
      breaksw
    case "-tl":
      set TL=$2;
      echo "TL is $TL"
      breaksw
    case "-PLIX":
      set PLIX=$2;
      echo "PLIX is $PLIX"
      breaksw
    default:
      echo "Invalid arg"
      breaksw
  endsw
  shift
  shift
end

--ahamed

1 Like

Sory for missing cases. Its not necessary to have all the options in a command.