getoptsを使用したbashでのオプションパラメータの処理

getoptsを使用したbashでのオプションパラメータの処理

オプションのパラメーターを使用して入力ファイルを処理するbashスクリプトがあります。スクリプトは次のとおりです。

#!/bin/bash
while getopts a:b:i: option
do
case "${option}"
in
a) arg1=${OPTARG};;
b) arg2=${OPTARG};;
i) file=${OPTARG};;
esac
done

[ -z "$file" ] && { echo "No input file specified" ; exit; }

carry out some stuff

スクリプトは正常に実行されますが、入力ファイルを次のように指定する必要があります。

sh script.sh -a arg1 -b arg2 -i filename

-i次のようにオプションなしでスクリプトを呼び出すことができるようにしたいと思います。

sh script.sh -a arg1 -b arg2 filename

入力ファイルを指定しないと、エラーメッセージが表示されます。これを行う方法はありますか?

ベストアンサー1

#!/bin/sh -

# Beware variables can be inherited from the environment. So
# it's important to start with a clean slate if you're going to
# dereference variables while not being guaranteed that they'll
# be assigned to:
unset -v file arg1 arg2

# no need to initialise OPTIND here as it's the first and only
# use of getopts in this script and sh should already guarantee it's
# initialised.
while getopts a:b:i: option
do
  case "${option}" in
    (a) arg1=${OPTARG};;
    (b) arg2=${OPTARG};;
    (i) file=${OPTARG};;
    (*) exit 1;;
  esac
done

shift "$((OPTIND - 1))"
# now "$@" contains the rest of the arguments

if [ -z "${file+set}" ]; then
  if [ "$#" -eq 0 ]; then
    echo >&2 "No input file specified"
    exit 1
  else
    file=$1 # first non-option argument
    shift
  fi
fi

if [ "$#" -gt 0 ]; then
  echo There are more arguments:
  printf ' - "%s"\n' "$@"
fi

そのコードには具体的な内容がないので、にbash変更させていただきます。shbash

おすすめ記事