Help on command line argument in csh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on command line argument in csh
# 1  
Old 09-27-2011
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?
Code:
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.

Last edited by Scott; 09-27-2011 at 09:36 AM.. Reason: Code tags, please...
# 2  
Old 09-27-2011
Try this...
Code:
#!/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
# 3  
Old 09-27-2011
Thanks ahamed but I need in csh not bash.
# 4  
Old 09-27-2011
Ok, I just saw the "csh" part. I am not sure if this will work in "csh" shell.

--ahamed
# 5  
Old 09-27-2011
No, it wont work. I tried that already.
# 6  
Old 09-27-2011
Are you not missing the "case" keyword?

Code:
#!/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?
This User Gave Thanks to Scott For This Post:
# 7  
Old 09-27-2011
CSH

Code:
#!/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
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add command line argument

I would like to add the ability to change the message that is displayed when timer is finished. At present it just asks for the time I want for the alarm. I think what I need is another command line argument. soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"... (5 Replies)
Discussion started by: drew77
5 Replies

2. UNIX for Beginners Questions & Answers

Command line argument

Hi Guys, I'm trying to work out how to add a command line argument inside single quotes. Would anyone be able to help please as I'm going mad :) I want to be able to place the filename on command line and it then be used in a script but it needs to have quotes surrounding it. Thanks in... (4 Replies)
Discussion started by: mutley2202
4 Replies

3. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

4. Homework & Coursework Questions

(t)csh command line arguments issues

1. The problem statement, all variables and given/known data: create a shell script, chExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names of files to be renamed. For each file in the command line, this script should rename the file, as... (6 Replies)
Discussion started by: kcoll025
6 Replies

5. Shell Programming and Scripting

command-line line 0: Missing yes/no argument

Hi Guys When I run the below command ssh -o 'PasswordAuthentication yes' -o 'PreferredAuthentications publickey' -i $HOME/.ssh/id_dsa Server_Name I found the below error ommand-line line 0: Missing yes/no argument Kindly help me to sort out Double post, continued... (0 Replies)
Discussion started by: Pratik4891
0 Replies

6. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

7. Programming

Command Line Argument

Hi, I have a very simple C program which will run in UNIX. When i am passing * as the command line argument, i am gettig the below output. Program: #include <stdio.h> #include "mylibrary.h" int **environ; int main(int argc,char *argv) { int i; printf("\nHello... (2 Replies)
Discussion started by: dsudipta
2 Replies

8. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

9. Shell Programming and Scripting

How to get the value in last command line argument???

Say I want to get the value of last command line argument using the value in $# (or some other way if u can suggest) how do I do it?? $"$#" `$"$#"` These don't work :( (4 Replies)
Discussion started by: amit_oddey21
4 Replies

10. UNIX for Dummies Questions & Answers

array as command line argument !!!!

hello, can any help me how to can pass array as command line argument in korn shell. also how to read a array from command line. thanks spandu (2 Replies)
Discussion started by: spandu
2 Replies
Login or Register to Ask a Question