passing either of argument, not both in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing either of argument, not both in shell
# 1  
Old 02-24-2012
passing either of argument, not both in shell

Hi,

I have a requirement to work on script, it should take either of arguments.
wrote it as below.
Code:
#!/bin/bash
usage() {
  echo "$0: missing argument OR invalid option !
        Usage : $0 [-v][-h] -m|-r|-d
}

while getopts mrdvh opt; do
   case "$opt" in
      m)     monitor_flag=monitor;;
      r)     report_flag=report;;
      d)     delete_flag=delete;;
      v)     verbose=True;;
      h)     usage
             exit 0;;
     [?])   usage
   esac
done


if [ "$monitor_flag" = "monitor" -o "$delete_flag" = "delete" -o "$report_flag" = "report" ]; then
   echo "correct usage"
else
   echo "Usage:"
   usage
   exit 1;
fi
....
..

Here my issue is, script should through usage when they run it with both -m -r or -m -d or -r -d or -m -r -d...

I couldn't use XOR , otherwise it would have work.

please suggest.

Thanks,
Rams

Last edited by Franklin52; 02-24-2012 at 03:31 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-24-2012
You may try something like this:

Code:
#!/bin/bash


usage() {
  (( h )) || 
    printf >&2 '%s: missing argument OR invalid option!\n' "${0##*/}"
  printf >&2 'Usage : %s [-v][-h] -m|-r|-d\n' "${0##*/}"
  }

(( $# )) || usage 

while getopts :mrdvh opt; do
   case $opt in

     ( h )     
        h=1 
        usage 
        exit 0   
        ;;

     ( v )     
        verbose=True 
        ;;

     ( m )     
        monitor_flag=monitor 
        (( ++c ))
        ;;

     ( r )     
        report_flag=report   
        (( ++c ))
        ;;
      
     ( d )     
        delete_flag=delete
        (( ++c ))
        ;;

     ( ? )   
      usage
   esac
done

(( c > 1 )) &&
  usage

Note that the script uses non standard syntax. Let me know if it doesn't work with the version of bash you're using.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

3. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

4. Shell Programming and Scripting

Passing argument from Java to Shell script

Hi All, I want to pass array of argument from Java to a shell script.I can use process builder api and its exec() method to call the script,but the question is how to receive the parameter in the script. Thanks in advance ---------- Post updated at 10:00 PM ---------- Previous update was... (1 Reply)
Discussion started by: Abhijeet_Atti
1 Replies

5. Shell Programming and Scripting

Passing argument to a script while executing it within current shell

Hi Gurus, I have written a script set_env.ksh to which I pass an argument and set the oracle login credentials based on the argument I pass. The script has code as below. ORACLE_SID=$1 DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID; export DB_SCHEMA_LOGON; echo... (3 Replies)
Discussion started by: Sabari Nath S
3 Replies

6. UNIX for Dummies Questions & Answers

Passing command output as an argument to a shell script

Hi, I have a very small requirement where i need to pass command output as an argument while invoking the shell script.. I need to call like this sh testscript.sh ' ls -t Appl*and*abc* | head -n 1' This will list one file name as ana argument.. I will be using "$1" in the shell... (2 Replies)
Discussion started by: pssandeep
2 Replies

7. UNIX for Dummies Questions & Answers

Passing command line argument between shell's

Hi, I am facing a problem to pass command line arguments that looks like <script name> aa bb "cc" dd "ee" I want to pass all 5 elements include the " (brackets). when I print the @ARGV the " disappear. I hope I explain myself Regards, Ziv (4 Replies)
Discussion started by: zivsegal
4 Replies

8. Shell Programming and Scripting

passing argument to shell script that reads user inputs

Hi, Lets say I have a script "ss" which does this read abc echo $abc read pqr echo $pqr Now if I want to pass and argument to only "abc" how do I do it. If I do echo "somevalue" | ss, it does not prompt for pqr and its value comes out as blank. Any help is appreciated Thanks P (6 Replies)
Discussion started by: patjones
6 Replies

9. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

10. Shell Programming and Scripting

Problem when passing argument to a shell script

Hi all, I'm new to Shell scripting. In my shell script for Bourne shell, the script accepts a date parameter which is optional. If the value is supplied, the supplied value should be assigned to a variable. If not, the current date will be assigned to the variable. My script is like this. #!... (9 Replies)
Discussion started by: sumesh.abraham
9 Replies
Login or Register to Ask a Question