Domanda

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=
È stato utile?

Soluzione

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

while getopts "r:" optionName
                ^
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top