Making a Script with switch as option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making a Script with switch as option
# 1  
Old 09-14-2009
Error Making a Script with switch as option

Hi all,

I want to make a script which should be able to use switch,
eg. If i want to perform a operation on a single file , i should give file path as argument
i.e. SCRIPT <PATH>

Now if i want to perform the same operation on a bunch of files, i should write as
SCRIPT -f filename
where filename contains path of files where operation is to be performed.
Now my question is how should i make it possible to use switch as argument.
I want to make this script in cshell. So is it possible that i can create such a script which should automatically understand that if i am using -f as switch it has to perform operation on group of files & should execute that code.

Similarly if i type, SCRIPT -h, it should execute the usage code.

Thanks in advance

Sarbjit

Last edited by sarbjit; 09-14-2009 at 07:13 AM..
# 2  
Old 09-14-2009
you can do something like:

Code:
while true
do
  case $1 in
        "")
            break
            ;;
        -h)
            ## help
            usage
	    exit
            ;;
        -f)
            filemane=$2
            if [ -s "$2" ]
            then
              echo "$Usage"
              exit 
            fi
            shift
            ;;
           
  esac
  shift
done

# 3  
Old 09-14-2009
in bash u can do this by 'getopts' command:
the above code would like:
Code:
while getopts fh choice
do
  case $choice in
        -h)
            ## help
            usage
	    exit
            ;;
        -f)
            #do ur action with ,$2-$9 as the filepaths
             ;;
    ?) echo "wrong choice"
           
  esac
  
done

i don't know whether it is in csh or not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to switch user in shell script?

HI in a server we can't login with root user directly but i can login with different user and then i can switch to root user by su command Requirement is there anyway where i can write a script without mentioning password in file as mentioning the root password is not the... (3 Replies)
Discussion started by: scriptor
3 Replies

2. Shell Programming and Scripting

Script to execute with switch

Hello I want to create a script which will require a mandatory value and optional values which can be supplied using switch. If optional values are not supplied, the script will use the default values mentioned in the script. For example, how we create user in linux systems. Please... (3 Replies)
Discussion started by: atanubanerji
3 Replies

3. Shell Programming and Scripting

Switch between root and user in the same script.

I am writing a korn shell script where i need to switch to root in between and again exit from root to normal user and continue other commands. Is that possible to switch between these two in the same script? (1 Reply)
Discussion started by: santosh2626
1 Replies

4. UNIX for Advanced & Expert Users

Help with server switch shell script

I need a script to change server automatically after performing some operations. The command for changing server is ssh username@servername . Then a prompt comes to enter a password. Then i need to perform some opertaions on the other server. How can i do this in a script? (1 Reply)
Discussion started by: pratikm23
1 Replies

5. Shell Programming and Scripting

How to switch user using shell script ?

Hi, script1.sh script2.sh script3.sh From above, script1.sh is the main script which is executed from root user, creates installation directory, changing ownership and execution rights etc..etc.. and finally calls scripot2.sh and script3.sh to create the database as well as for post... (1 Reply)
Discussion started by: milink
1 Replies

6. Shell Programming and Scripting

Switch User in within a Shell Script

Hi Experts, I'm trying to write a shell script to stop few things where i have to use another user to execute a command. Otherwise it will not work. Your help is really appreciated Thanks, (16 Replies)
Discussion started by: Afi_Linux
16 Replies

7. Shell Programming and Scripting

Switch from one database to other using shell script

Hi all, This is my first ever post to any forum so, dont let this go in vain...........:) Here is the scenario........ I have logged into the unix where oracle_sid is initialized for some X database in the .profile. I have a unix script where some sql query which fetches data from X... (3 Replies)
Discussion started by: sachinkl
3 Replies

8. Shell Programming and Scripting

switch user inside a script

Hi Is there any way to switch user inside a shell script? (4 Replies)
Discussion started by: ./hari.sh
4 Replies

9. Shell Programming and Scripting

script with more then one switch

Hi, have managed to code a script that has a simple menu so for instance if I run: this will call a help function that displays the programs help, I have coded this in using a case statement so if: case is h) call the help function The problem is I don't know how to code in the... (3 Replies)
Discussion started by: Del33t
3 Replies

10. Shell Programming and Scripting

switch user inside a script

Hi, I wrote a unix script that will perform differnt tasks on bahalf of number of users. I use "sudo" to run the script. The problem is when I execute the command: su - user -c "xxx " > output_file, I get the system output header frm the su command. Is there a way to get rid of it instdead of... (2 Replies)
Discussion started by: nimo
2 Replies
Login or Register to Ask a Question