Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Script Argument without value

I'm creating a very simple sh file to do something, but I want to pass an argument without a value, for example:

./fuim -l

But I receive the following message:

./fuim: option requires an argument -- l

If I pass a random value, like ./fuim -l 1, it works perfectly. How can I do that?

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
like image 261
Juliano A. Felipe Avatar asked Aug 12 '14 18:08

Juliano A. Felipe


1 Answers

When using getopts, putting : after an option character means that it requires an argument. So change l: to l, as in:

while getopts e:f:lh OPT

This makes it a boolean option, either it's there or it isn't.

like image 193
Barmar Avatar answered Nov 12 '22 18:11

Barmar