Creating Script Parameters With getopts
Many programs for Linux have parameters that can be given at run time. These are also known as switches, arguments or options. These parameters make it easy to tell a program or script what to do and what options to use. In this Shell Script Sundays Blog post I will show you how to implement these in a script by using getopts.
For this example to work the following must be placed in a script:
while getopts ":e:" ARG; do case "${ARG}" in e) echo "${OPTARG}";; esac; done |
This code basically gets loops around the arguments. All this script does is take the value after the -e and echo’s it out. In the example below I named the script getopts.
owen@the-linux-blog:$ ./getopts -e "Hi There Linux Blog Viewers" Hi There Linux Blog Viewers |
For each extra parameter that is needed a new constant to the getopts and do loop need to be added. For example if the option -e and -q need to be in the script then the following getopts loop would be created:
while getopts ":e:q" ARG; do case "${ARG}" in e) echo "${OPTARG}";; q) echo "${OPTARG}";; esac; done |
Of course the above script for -q also only echo’s out the value for -q as seen below:
owen@the-linux-blog:$ ./getopts -e "Hi There Linux Blog Viewers" -q "Another Option" Hi There Linux Blog Viewers Another Option |
This is all very well, but documentation is always nice, even if you think you know how to use your script you may forget in the future. Its also nice if you have a script that other people can easily use. That being said its good to have a way to show users how to run the script.
usage () { echo -e "Usage: $0 -e \"What To Echo\" [ -q \"Output\" ]" echo -e "\t-e: specifies what to echo (required)" echo -e "\t-q: Where to write output to. If not specified the output is written to the console" } while getopts ":e:q:" ARG; do case "${ARG}" in e) ECHO="${OPTARG}";; q) OUTPUT="${OPTARG}";; esac; done [ -z "${ECHO}" ] && { usage && exit 1; } [ "${OUTPUT}" ] && { echo $ECHO > $OUTPUT; } || { echo $ECHO; } |
The code above takes the options and assigns a variable to each of the options $ECHO is what to echo and $OUTPUT is where to write the output to. The script calls the usage() function and exits whenever the required option ($ECHO) is not set. If $ECHO is set it checks to see if $OUTPUT is set, if so it echo’s the contents of $ECHO to the $OUTPUT variable (file or device). If $OUTPUT is not set then it just echo’s the $ECHO variable normally. This is the script running with its various different actions:
owen@the-linux-blog:$ ./getopts Usage: ./getopts -e "What To Echo" [ -q "Output" ] -e: specifies what to echo (required) -q: Where to write output to. If not specified the output is written to the console owen@the-linux-blog:$ ./getopts -e "The Linux Blog getopts Example" The Linux Blog getopts Example owen@the-linux-blog:$ ./getopts -e "The Linux Blog getopts Example. Output To Null" -q /dev/null owen@the-linux-blog:$ ./getopts -e "The Linux Blog getopts Example" -q Write_To_This_File owen@the-linux-blog:$ ls Write_To_This_File getopts owen@the-linux-blog:$ cat Write_To_This_File The Linux Blog getopts Example |
As there are many different variations and each implementation would be different I can not cover each individual getopts scenario but by assigning variables your option arguments you should be able to get switches working in your own shell scripts.