Give shell script options
Requirements
Place this in a file called options.sh, and make it executable.
1#!/bin/sh
2
3echo "This script is called $0"
4
5while getopts oe: choice ; do
6 case "$choice" in
7 o)
8 echo "This is option number '${OPTIND}'."
9 ;;
10 e) echo "Option number '${OPTIND}' uses this variable: '${OPTARG}'."
11 ;;
12 \?) echo "That's not an option."
13 exit 1
14 ;;
15 esac
16done
17shift "$(($OPTIND -1))"
18
19echo "The standard arguments are: $@"
Try the options:
1./options.sh -o
2./options.sh -e elephant
3./options.sh Some random words
4./options.sh -efox Some other words
Try the script again, without the shift statement.
NB: You can change choice to any variable, but you must use OPTARG to show an option's argument and OPTIND to show the index of that argument.