Getops usage in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getops usage in UNIX
# 1  
Old 09-08-2014
Getops usage in UNIX

I have a case where the script has to run in two modes as options and based on the mode, script excepts optional and mandatory arguments.
Code:
script.sh -a  -f  <value1> -d <value2> -h <value3>
script.sh -b  -i <value4>

a and b are the modes of the script execution.
value2, value3 are optional.

I just know the basic usage of getopts. But can someone help me implementing the case in getops?

Thanks in advance!

Last edited by vbe; 09-08-2014 at 11:15 AM.. Reason: code tags please
# 2  
Old 09-08-2014
Maybe should you explain us how you see your getopts stanza...
what have you done so far?
# 3  
Old 09-08-2014
This is what i tried. Mainly I'm facing issue while applying mutually exclusive conditions of the options. Like either a or b should be passed, but not both and likewise optional arguments as well.

Code:
while getopts :abf:d:h:i: mode
do
        case $mode in
        a)adhoc=1;;
        b)batch=1;;
        f)fn=$OPTARG;;
        d)del=$OPTARG;;
        h)hd=$OPTARG;;
        i)id=$OPTARG;;
        *)echo "Invalid arg";;
esac done

if [[ ! -z $adhoc ]]
then
    echo "Adhoc processing"
    echo "fn: " $fn
    echo "del: " $del
    echo "hd: " $hd
fi

if [[ ! -z $batch ]]
then
    echo "Batch processing"
    echo "id: " $id
fi


Last edited by ksailesh1; 09-08-2014 at 01:33 PM.. Reason: added fn argument
# 4  
Old 09-08-2014
Quote:
Originally Posted by ksailesh1
This is what i tried. Mainly I'm facing issue while applying mutually exclusive conditions of the options. Like either a or b should be passed, but not both and likewise optional arguments as well.

Code:
while getopts :abf:d:h:i: mode
do
        case $mode in
        a)adhoc=1;;
        b)batch=1;;
        f)fn=$OPTARG;;
        d)del=$OPTARG;;
        h)hd=$OPTARG;;
        i)id=$OPTARG;;
        *)echo "Invalid arg";;
esac done
 
if [[ ! -z $adhoc ]]
then
    echo "Adhoc processing"
    echo "fn: " $fn
    echo "del: " $del
    echo "hd: " $hd
fi
 
if [[ ! -z $batch ]]
then
    echo "Batch processing"
    echo "id: " $id
fi

Personally, I'd probably just do validation on all the arguments after you finish grabbing them with getopts:

So just add this after your existing getopts case (and prior to your other ifs):

Code:
if [[ $adhoc -eq 1 && $batch -eq 1 ]]
then
  echo "Cannot specify both adhoc and batch."
  exit 1
fi

(for example) ...
# 5  
Old 09-08-2014
Quote:
Originally Posted by Ditto
Personally, I'd probably just do validation on all the arguments after you finish grabbing them with getopts:

So just add this after your existing getopts case (and prior to your other ifs):

Code:
if [[ $adhoc -eq 1 && $batch -eq 1 ]]
then
  echo "Cannot specify both adhoc and batch."
  exit 1
fi

(for example) ...
That covers the case where both of the exclusive options are present, but doesn't notice if neither -a nor -b was given. Perhaps adding:
Code:
adhoc=0
batch=0

before the getopts loop and:
Code:
if [ $((adhoc + batch)) -ne 1 ]
then    echo "Exactly one of -a and -b must be specified." >&2
        exit 1
fi

after the loop would be better.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of Sleep in UNIX

Hi Guys, In my script i need to sleep for some seconds till the previous command exeuction is successful #!/bin/bash cp test.txt /direct/sub_dir if ; then echo "file copied" else echo "file not copied" sleep 5 exit 1 fi i need to sleep for 5 seconds if the copy command... (7 Replies)
Discussion started by: Master_Mind
7 Replies

2. Shell Programming and Scripting

Help with UNIX READ command usage..

Tested on : bash Will be implementing on : ksh I dont know if this is weird , or my idea of Unix stdin or stdout is completely messed up , but if I use the following command, I am getting a proper output. ls -l | head -1 | while read a ; do echo $a ;done and the output is soemthing like... (5 Replies)
Discussion started by: kumarjt
5 Replies

3. UNIX for Dummies Questions & Answers

Cpu usage of UNIX server

I want to know the total cpu usage(in %ge)of unix server like windows (NOTE :-not each process cpu usage) Its urgent...plzzzz help me.... (1 Reply)
Discussion started by: rohit kataria
1 Replies

4. Shell Programming and Scripting

Various usage of FTP in Unix

My project is to write a script to get/put data to another server, be able to log input and output files, have some sort of security for the password, and any errors connecting send a email to a group of people. I've seen examples of perl, java being called by Unix scripts, etc. I've even seen... (1 Reply)
Discussion started by: gavineq
1 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

7. Shell Programming and Scripting

explanation of getops

hi all, i am trying to figure out what exactly does this chunk of code which sits in a shell script does. Am not very good at scripting so could someone explain what the below is/does ?? its a ksh (i.e #!/bin/ksh) and sits on a solaris 9 box. REFRESH=FALSE ((C=0)) while getopts... (2 Replies)
Discussion started by: cesarNZ
2 Replies

8. Shell Programming and Scripting

Disk Usage in Unix

Hi, Please let me know how do we write a piece of code to find the disk usage in UNIX (3 Replies)
Discussion started by: Shilpi
3 Replies

9. UNIX for Dummies Questions & Answers

How can I see the processor usage in Unix??

Could anybody tell me which command I should use to see how many percentage or something from the processor is used by various programs? Thanx in Advance! Erik:D (4 Replies)
Discussion started by: Erik Rooijmans
4 Replies
Login or Register to Ask a Question