There are various ways to pass data into a Bash shell script.
Passing 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 $1
echo $2
Invoking this script will return the following output.
foo
bar
Or, like this (array)
#!/bin/bash
echo "$@"
Invoking this script will return the following output.
foo bar
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