Usage of optarg


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Usage of optarg
# 1  
Old 07-19-2005
Power Usage of optarg

Hello Friends,

I need to pass arguments to a shell scripts. but for this i need to take the arguments only if they are supplied to the script as arguments.

Like :

Prompt > scriptname -d device_cd -s message

so how do i capture these arguments ?> i think we have to use optarg.but i dont know exact usage.

Can any one please help?

Thanks in advance
Veera
# 2  
Old 07-19-2005
try getopts in some kind of a loop
example:
Code:
function Parse
{
   while getopts :r:l:p:o:q:t: name
   do
     case $name in
        l) LOG="$OPTARG" ;;        # LOG FILE
        p) PROC="$OPTARG" ;;       # Process name
        o) ONE_UP="$OPTARG" ;;     # One_Up Number
        q) PRNT="$OPTARG" ;;       # q Print queue 'FTP' for now
        r) LIS="$OPTARG" ;;        # lis file name
        t) FTPDEST="$OPTARG" ;;     # D = Disk, T = Tape
        *) Usage ;;                     # display usage and exit
     esac
   done

# 3  
Old 07-19-2005
Computer getopts usage

Hi Jim mcnamara,

This helps. Thanks for the response.
bye
Veera
# 4  
Old 07-19-2005
Power getopts

Hi Jim mcnamara and friends,
I used getopts in the way you suggested. But what if i have to send two mandatory arguments to the script and the rest two as optional parameters ?
i have having problem in giving both Mandatory and optional fields at the same time to the script.
Please help.
Thanks in advance
Veera
# 5  
Old 07-19-2005
In Jim's example, he's setting variables; therefore, he could test the variables with [ -z "${VAR...}" ] and create appropriate exceptions if they aren't defined.
Code:
function Parse
{
   while getopts :r:l:p:o:q:t: name
   do
     case $name in
        l) LOG="$OPTARG" ;;        # LOG FILE
        p) PROC="$OPTARG" ;;       # Process name
        o) ONE_UP="$OPTARG" ;;     # One_Up Number
        q) PRNT="$OPTARG" ;;       # q Print queue 'FTP' for now
        r) LIS="$OPTARG" ;;        # lis file name
        t) FTPDEST="$OPTARG" ;;     # D = Disk, T = Tape
        *) Usage ;;                     # display usage and exit
     esac
   done
   [ -z "${PROC}" -o -z "${PRNT}" ] && Usage

# 6  
Old 07-19-2005
You can take this a step further by using a check sum. Say you have 5 or 10 required flags, you could accomplish this by keeping track of those arguments you have already processed by using a counter. Check the value of the counter at the end to verify all flags were received.


Code:
USAGE="Usage: `basename $0` [-s schemaname] [-d databasename] [-u username] -f -t -h"
while getopts :s:d:u:fth params
do
   case $params in
     s) SOURCESCHEMA="$OPTARG"
        let COUNT= $COUNT+10000
         ;;
     f) FULL='y'
         ;;

     t) TABLE='y'
         ;;

     d) DBNAME="$OPTARG"
         let COUNT= $COUNT+1
        ;;

     u) USERNAME="$OPTARG"
         let COUNT= $COUNT+1000
         ;;
     h) 
         help_doc
         exit 0 
         ;;
     ?) 
         echo "Invalid Option Specified"
         echo "$USAGE" 1>&2 ; exit 1 
         ;;
   esac

if [ "$COUNT" -ne 10101 ]
  then
    help_doc
    exit 0
elif [ ! -z $FULL ]
   then
      echo "Paramater FULL is Set"
elif [ ! -z $TABLE ]
   then
      echo "Paramater TABLE is Set"
else
    echo "Neither TABLE or FULL Are Set"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts - space in argument (OPTARG)

Hi, I want to capture space as well from the argument eg: script.ksh -m "Message from xyz" -e "email@xyz.com" script.ksh -m 'Message from xyz' -e 'email@xyz.com' I am parsing using getopts, but for option "m" OPTARG is returning only "Message". Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies

2. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

3. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

4. Shell Programming and Scripting

Pass date range into optarg

here is the code echo begin while getopts 1:2:3:4: mode do case $mode in 1)echo "You have chosen mode1" case $OPTARG in a) echo "User Specified Date Range" rangelist.sh ;; b) echo "user specified month and year";; ?) echo "Default, Current... (4 Replies)
Discussion started by: pravsripad
4 Replies

5. Shell Programming and Scripting

$OPTARG changes commandline input

Hi there, I hope, that I do not open an thread, that is already existing, but I didn't found something matching with my problem while searching for problems with "getopts" My problem ist, that I'm taking arguments from commandline into my script with getopts, I've an flag -s, after that there... (2 Replies)
Discussion started by: thommes_pommes
2 Replies

6. Shell Programming and Scripting

OPTARG is not taking SPACE

Hi I have below code in one of my shell script: if ; then fail $USAGE; fi while getopts hz:r:t:dz: o do case "$o" in h) echo $USAGE ; exit 0;; r) export REQ_ID="$OPTARG";; t) TIMESPAN="$OPTARG";; d) detail="true";; ) ... (0 Replies)
Discussion started by: mohsin.quazi
0 Replies

7. 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

8. Programming

command line option: optarg

Hi I'm learning how to add in programm another option by command line. For example in the "my_prog" i want to add a " -k " option and then write a number. I mean: my_prog -k 50 and the i should use the number 50. I'm reading about getopt_long and optarg, and what i have done in the code... (0 Replies)
Discussion started by: Dedalus
0 Replies

9. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

10. 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
Login or Register to Ask a Question