Bootstrap FreeKB - Bash (Scripting) - Command line options flags arguments (case statement)
Bash (Scripting) - Command line options flags arguments (case statement)

Updated:   |  Bash (Scripting) articles

There are various ways to pass data into a Bash shell script.

  • Pass in arguments (also known as positional arguments)
  • Pass in options (also known as key value pairs)
  • Pass in flags

 


Pass in arguments

In this example, "foo" and "bar" are passed into the example.sh script.

example.sh foo bar

 

In the example.sh script, foo will be accessed using $1 and bar will be accessed using $2, like this.

#!/bin/bash
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"

 

Invoking this script will return the following output.

$0 = example.sh
$1 = foo
$2 = bar

 

Likewise, the argument will also be stored in the $@ variable.

echo "$@"

 

And in the $* variable.

echo "$*"

 

Both $@ and $* should return the following.

foo bar

 

And here is a very simple case statement that will echo "got foo" or "got bar" if $1 is foo or bar, else "opps" will be returned.

case "$1" in
  foo) echo "got foo";;
  bar) echo "got bar";;
    *) echo "opps";;
esac

 


Pass in options

A case statement can be used to create both command line options and flags. An option must be followed by a value. In this example, the --name option is followed by a value of Jeremy.

~]# example.sh --name Jeremy

 

A flag is not followed by a value on the command line. In this example, the --help flag is used. A flag will be associated with a value in the case statement.

~]# example.sh --help

 

One of the most practical uses of a case statement is to be able to create command line options. For example, lets say you have a bash script and you want options such as -n or --name and -o or --occupation.

example.sh -n Jeremy -o Engineer

 

First, you will create a variable that will contain the short and long verision of the command line options. In this example n:o: creates the -n and -o options, and name:,occupation: creates the --name and --occupation options.

ARGS=$(getopt -a --options n:o: --long "name:,occupation:" -- "$@")

 

Next you evaluate the $ARGS variable.

eval set -- "$ARGS"

 

Then a while loop is used to associate each command line option with it's value. For example, if -n Jeremy or --name Jeremy is used on the command line, then a variable called name will contain a value of Jeremy.

while true; do
  case "$1" in
    -n|--name)
      name="$2"
      shift 2;;
    -o|--occupation)
      occupation="$2"
      shift 2;;
    --)
      break;;
     *)
      printf "Unknown option %s\n" "$1"
      exit 1;;
  esac
done

 

You can then do something based on the  value associated with the command line options.

echo "Your name is $name"

 


Pass in flags

You can also create flags. For example, let's say you want to have flags for -v or --verbose and -h or --help.

~]# example.sh -v -h

 

First, you will create a variable that will contain the short and long verision of the command line flags. In this example, vh creates the -v and -h options, and verbose,help creates the --verbose and --help options. The difference between an option and a flag is that an option has the : character, and a flag does not.

ARGS=$(getopt -a --options vh --long "verbose,help" -- "$@")

 

Next you evaluate the $ARGS variable.

eval set -- "$ARGS"

 

You will also want to set default values for each flag. In this example, the default value is "false".

verbose="false"
help="false"

 

Then a while loop is used to update the verbose and help value to "true" if the flags are used.

while true; do
  case "$1" in
    -v|--verbose)
      verbose="true"
      shift;;
    -h|--help)
      help="true"
      shift;;
    --)
      break;;
     *)
      printf "Unknown option %s\n" "$1"
      exit 1;;
  esac
done

 

You can then use an if statement to do something with the verbose and help flags.

if [ $help == true ]; then
  echo "Here is some helpful information"
fi

 


Arguments and Options

Let's say you want to include an argument (foo) followed by an option (--name john.doe).

example.sh foo --name john.doe

 

In this scenario, you may just need a simple if statement to get the job done.

if [ ! -z "$1" ]; then
  argument="$1"
fi

if [ ! -z "$2" ]; then
  name="$3"
fi

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e55181 in the box below so that we can be sure you are a human.