0

I'm trying to build a script in bash and building a getopts loop. I have two options(x and h) that don't have any arguments and the other two have it. But my while loop fails by taking only one option and doesn't take any other arguments, how can I make this work? m, p and s can have arguments whereas x and h will not have arguments

while getopts ":m:p:s:x:h:"  option; do
case "${option}" in
m)
HOME_DIR=${OPTARG}
echo "The home dir is $HOME_DIR"
;;
p) URL=${OPTARG}
echo "The url is $URL"
;;
s) ID=${OPTARG}
echo "The server id is $ID"
;;
x) SKIP_SERVICE=${OPTARG}
echo "The skip service is $SKIP_SERVICE"
;;
h)
    echo "Usage: $0 [options]"
    echo " "
    echo "options:"
    echo "-h,       show help"
    echo "-m,       set home directory - defaults to HOME"
    echo "-p,       enter URL"
    echo " "
    exit 0
    ;;
esac
done
bat_singer
  • 11
  • 2
  • You might be just missing `shift $((OPTIND-1))` see https://unix.stackexchange.com/questions/214141/explain-the-shell-command-shift-optind-1/214151 and https://unix.stackexchange.com/questions/236010/use-of-shift-optind-1 – Jetchisel Apr 07 '21 at 04:54
  • If `-x` and `-h` don't have arguments, then don't put colons after them in the `getopts` string (i.e. use `getopts ":m:p:s:xh"`). But if `-x` doesn't have an argument, what is `SKIP_SERVICE`? – Gordon Davisson Apr 07 '21 at 05:05

0 Answers0