Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getopts: detect when OPTARG is used without colon in optstring #1989

Open
2 tasks done
wwuck opened this issue Jun 17, 2020 · 0 comments
Open
2 tasks done

getopts: detect when OPTARG is used without colon in optstring #1989

wwuck opened this issue Jun 17, 2020 · 0 comments

Comments

@wwuck
Copy link

wwuck commented Jun 17, 2020

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/sh

usage() {
        echo "$0 [-a text] [-b]"
        exit 1
}

while getopts "ab" opt; do
        case "$opt" in
                a) foo=$OPTARG ;;
                b) alias bar=true ;;
                *) usage ;;
        esac
 done

echo "$foo"

Here's what shellcheck currently says:

No issues detected!

Here's what I wanted or expected to see:

Something similar to OPTARG used in option case a but no colon specified after a in optstring.

Shellcheck should warn that foo is being assigned to $OPTARG when there is no colon after a in the optstring. This could lead to the user expecting foo to get a value but no value being passed to the script command line arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant