Question

Below is the snippet of my shell script

#!/bin/sh -e
ROOT=""
while getopts "r" optionName;
do case $optionName in
r)
    ROOT="$OPTARG"
    ;;
*)
    echo "Bad argument"
    exit
    ;;
esac
done
echo "ROOT=$ROOT"

it doesn't print the ROOT correctly, where is the problem?

$myscript.sh -r FOO
ROOT=
Était-ce utile?

La solution

You need a colon after r if you want it to take an argument:

while getopts "r:" optionName
                ^
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top