Linux Blog

Getting a Hand With Bash

Filed under: General Linux — at 8:27 am on Wednesday, October 1, 2008

If you use the bash shell, or as many know it the terminal on a regular basis then there is a package that you can install that will make your life a whole lot easier. Firstly I’ll tell you a little bit more about it.
When I first started using Linux a friend told me to install this package. I never really understood what it exactly did, but using the terminal was a whole lot easier then using the command prompt in Windows. I couldn’t explain it but things just worked. Features that were missing from Dos were there. There was awesome auto completion that knew what I wanted to type. Over the years I became more accustomed to using Linux and the Bash shell. I upgraded hardware, changed distributions and never really noticed what I was missing out on. The other day I was thinking, wouldn’t it be nice if I could press tab and this would auto complete. Then it hit me, the package my friend had told me to install when I first was starting out. The package happened to be exactly for this purpose. Since I was a novice and he was helping me out I didn’t really have the time to ask about the details of every single package, so here I am now letting you in on a little secret. (Read on …)

Creating Script Parameters With getopts

Filed under: Shell Script Sundays — at 9:06 am on Sunday, October 14, 2007

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}";;

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}";;

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}";;
[ -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.