Getopts loop not working

1.3k views Asked by At

I am working on a bash script, and I decided to use getopts to get the options, but the loop I used doesn't work! Could someone please help me?

while getopts "u:p:k:s:t:c:l:" flag
do
    echo $flag
    case "$flag" in
        k)  APIKEY="$OPTARG"
            ;;
        s)  APISECRET="$OPTARG"
            ;;
        u)  USERNAME="$OPTARG"
            ;;
        p)  PASSWORD="$OPTARG"
            ;;
        t)  TITLE="$OPTARG"
            ;;
        c)  CATEGORY="$OPTARG"
            ;;
        l)  LANGUAGE="$OPTARG"
            ;;
 esac
done
shift $((OPTIND-1))

None of the above variables are being set.

1

There are 1 answers

1
choroba On BEST ANSWER

Don't use = with getopts and short options. Also, if you want to supply "non-options", e.g. file names (video.mp4 in your case), they should come last, not before the options (and you have to change the code accordingly); or you can process them (and shift) before you start the options loop.

dmUpload.sh -u USERNAME -p PASS -k KEY -s SECRET -c CAT -t TILE -l LANG video.mp4