下記の例で、変数A, B, Cにbashからオプション引数を割り当てる方法示します。
#!/bin/bash
set -e
set -o pipefail
OPT=`getopt a:b:c: $*`
set -- $OPT
for i; do
case $i in
--) shift; break ;;
-a) A=$2; shift; shift;;
-b) B=$2; shift; shift;;
-c) C=$2; shift; shift;;
esac
done
# check if A, B and C are properly given from option.
if [ -z "$A" ]; then echo "A is not specified"; exit; fi
if [ -z "$B" ]; then echo "B is not specified"; exit; fi
if [ -z "$C" ]; then echo "C is not specified"; exit; fi
コメント