Adding Help & Option in Bash Script : Basic Theory

There is a shell tool getopt. The GNU implementation of getopt(3) (used by the command-line getopt(1) on GNU/Linux) supports parsing long options. But the BSD implementation of getopt (e.g. on Mac OS X) does not support parsing long options.

getopt and getopts are different. getopts is a built-in command to bash to process command-line options in a loop and assign each found option and value in turn to built-in variables, so that we can further process them. getopt is an external utility program and it does not actually process our options for us the way that getopts does, as well as the Perl Getopt module or the Python optparse/argparse modules do. getopt canonicalize the options that are passed in -- that, is convert them to a more standard form, so that it becomes easier for a shell script to process them.

So for execution of commands in this way :

Vim

1

./shell.sh-cabc.pl/tmp/

it is easier and avoiding long words like --copyfile instead of -c shown above. Now, suppose this is our script’s main content :

Vim

1

2

3

4

5

#!/bin/bash

#main starts

echo-n"Hello! $USER, today is "

date+%A

#main ends

if we save it as bash-script.sh, to execute it, we will normally do these :

Vim

1

2

3

chmod+xbash-script.sh&&./bash-script.sh

#or

chmod+xbash-script.sh&&sh bash-script.sh

Adding Help & Option in Bash Script : Add Option

But, we want to supply the name as option, then print the date with this format of command :

Advertisement

---

Vim

1

./bash-script.sh-aAbhishek

so that, we will get this output :

Vim

1

Hello!Abhishek,today is Wednesday

Quite practical example. In this case, we need to write the script in this way (tested on OS X as working, report us if you face error) :

Vim

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#!/bin/bash

#Astring with command options

options=$@

#An array with all the arguments

arguments=($options)

#Loop index

index=0

forargument in$options

do

#Incrementing index

index=`expr$index+1`

#The conditions

case$argument in

-a)echo"Hello! ${arguments[index]}, today is `date +%A`";;

-abc)echo"Hello! ${arguments[index]}, today is $(date)";;

esac

done

exit;

very basic example.

Adding Help & Option in Bash Script : Add Help

Easiest example is :

Vim

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#!/bin/bash

if["$1"=="-h"];then

echo"Usage 1: ./ `basename $0` -a <anyname>"

echo"Usage 2: sh `basename $0` -a <anyname>"

echo"This Help File: sh `basename $0` -h"

exit0

fi

#Astring with command options

options=$@

#An array with all the arguments

arguments=($options)

#Loop index

index=0

forargument in$options

do

#Incrementing index

index=`expr$index+1`

#The conditions

case$argument in

-a)echo"Hello! ${arguments[index]}, today is `date +%A`";;

-abc)echo"Hello! ${arguments[index]}, today is $(date)";;

esac

done

exit;

If we run :

Vim

1

./bash-script.sh-h

then we will get the help :

Vim

1

2

3

Usage1:./bash-script.sh-a<anyname>

Usage2:sh bash-script.sh-a<anyname>

This Help File:sh bash-script.sh-h

But it has problem. It can not show anything if only ./bash-script.sh is executed. That is why we need to re-write in this way :

Vim

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#!/bin/bash

usage="$(basename "$0") -- Program to demonstrate the help to life or bash, she bang, she is Kate, she fck, she is sh. She indeed is GNU/Linux.

where:

-hshow this help text

-ssetthe seed value(default:42)"

seed=42

whilegetopts':hs:'option;do

case"$option"in

h)echo"$usage"

exit

;;

s)seed=$OPTARG

;;

:)printf"missing argument for -%s\n""$OPTARG">&2

echo"$usage">&2

exit1

;;

\?)printf"illegal option: -%s\n""$OPTARG">&2

echo"$usage">&2

exit1

;;

esac

done

shift$((OPTIND-1))

Real life example is not going to be easy. It is difficult because in real we need to authenticate, then define path and option , set errors, on error again the help will show. It is difficult as there will be minimum 2 arguments, logical flow for failure. If we do not set the flow rightly, we will not get any output! Easiest logical flow can be studied by saving this fully working example as supload.sh and executing it. We need to run command in this way :

Vim

1

/supload.sh-u<username>-k<password><dest_dir><src_path>

This is designed to work on OpenStack Swift or rather OpenStack Object Storage where API based authentication is allowed. With valid parameter on HP Cloud, it will print out the HTTP error. For dry run, it will throw the help options :