LiveFire Labs - Online UNIX Training - Company Logo


Online UNIX Training with Hands-on Internet Lab


"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."

 


Home
Internet Lab
Console Access
Sample Course

Student Login


LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View


Questions? Call
1.888.843.1637 or send us email

May 26, 2003 - Processing Shell Script Options with the getopts Command

The getopts command simplifies the task of validating and parsing command line options and arguments for your shell scripts.  Without this built-in command, writing shell script code to perform these common tasks could quickly become a complex and lengthy undertaking.

The syntax for the getopts command is as follows:

getopts option-list variable
option-list contains valid options for the script, and variable is set to the option if it is found in option-list.  If the option is not in option-list, variable is set to the "?" character.

The following snippet of code illustrates a basic implementation of the command:

#!/bin/ksh

PROG_NAME=$(basename $0)
A_FLAG=FALSE
B_FLAG=FALSE

while getopts abl: OPTION
do
    case ${OPTION} in
        a) A_FLAG=TRUE;;
        b) B_FLAG=TRUE;;
        l) LOGFILE=${OPTARG};;
      \?) print -u2 "Usage: ${PROG_NAME} [ -a -b -l logfile_name ]"
           exit 2;;
    esac
done

print ${A_FLAG}
print ${B_FLAG}
print ${LOGFILE}

There are a few key points you should understand about getopts and this example:

- getopts is designed to be executed inside of a loop, processing one option each iteration.  Once getopts processes the last option, it returns a non-zero value and the loop terminates.

- options requiring an argument ("l" in this example) need to be followed by a ":" (colon) on the getopts command line.

- option arguments are stored in the special variable OPTARG for script processing.

- if an invalid option is used, correct usage will be displayed and the script will exit with a return code of 2.

Here is a sample of the output for the above code (the script is named getopts_sample):

# ./getopts_sample -a -b -l testlog.txt
TRUE
TRUE
testlog.txt

You may want to run the above code on your system to see what happens when an invalid option is used.

The previous has been a basic introduction to the getopts command.  A more detailed understanding can be obtained by reviewing your shell's man page.


Learn more...
  

If you are new to the UNIX or Linux operating system and would like to learn more about other frequently-used operating system commands, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.


More Tips...

· 
Popular UNIX Tips from the Past

spacer Box Border
 

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email


First Name:


Email Address:






   1.888.843.1637

Home - Contact us - Company info - Privacy Statement   

 
©2002-2003 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.