Shell Script Argument Without Value

I am creating a very simple sh file to do something, but I want to pass an argument with no value, like:

./fuim -l

      

But I get the following message:

./fuim: option requires an argument -- l

      

If I pass in a random value, for example ./fuim -l 1

, it works fine. How can i do this?

Here's what I have so far:

while getopts e:f:l:h OPT
do
    case "$OPT" in
        h) print_help ;;
        e) EXT=$OPTARG ;;
        f) PROJECT_FOLDER=$OPTARG ;;
        l) LIST_FILES=1 ;;
        ?) print_help ;;
    esac
done

shift $((OPTIND-1))

if [ -z "$EXT" ] || [ -z "$PROJECT_FOLDER" ]; then
    print_help
fi

      

+3


source to share


1 answer


When used getopts

, placing :

after the option character means that it requires an argument. So change l:

to l

like in:

while getopts e:f:lh OPT

      



This makes it a boolean option, either there or not.

+10


source







All Articles