Jn Coe
Jn Coe

Reputation: 135

Shell script keeps reading arguments in wrong way

I have a shell script file written by someone else and for some reason it is having a hard time reading the arguments to which it is being executed. The chunk where i'm assuming the error is happening is the following:

# Add input parameters
while [ $# -gt 0 ]; do
  case "$1" in
  --tipo* | -t*)
    if [[ "$1" != *=* ]]; then shift; fi
    TIPO_APLICACAO="${1#*=}"
    ;;
  --contexto* | -c*)
    if [[ "$1" != *=* ]]; then shift; fi
    CONTEXTO="${1#*=}"
    ;;
  --ano-inicio* | -i*)
    if [[ "$1" != *=* ]]; then shift; fi
    ANO_INICIO="${1#*=}"
    ;;
  --ano-fim* | -f*)
    if [[ "$1" != *=* ]]; then shift; fi
    ANO_FIM="${1#*=}"
    ;;
  --data-inicio* | -f*)
    if [[ "$1" != *=* ]]; then shift; fi
    DATA_INICIO="${1#*=}"
    ;;
  --data-fim* | -f*)
    if [[ "$1" != *=* ]]; then shift; fi
    DATA_FIM="${1#*=}"
    ;;
  --estados* | -f*)
    if [[ "$1" != *=* ]]; then shift; fi
    ESTADOS="${1#*=}"
    ;;
  --help | -h)
    usage
    exit 0
    ;;
  *)
    printf >&2 "Error: invalid format to one or more arguments\n"
    usage
    exit 1
    ;;
  esac
  shift
done

I keep getting the error "Error: invalid format to one or more arguments\n" no matter how I pass the arguments. According to the documentation, this is (one example of) how the script should be run:

./update-data.sh --tipo covid --contexto development --ano-inicio 2020 --ano-fim 2022 --data-inicio 2022-03-31 -- data-fim 2022-09-01 --estados BR

I have tried some variations but to no success. It is still not 100% clear to me what the ">&2" is doing here, but I'm assuming that it is checking if any of the arguments got more than one value, is that right? Is there any modifications I could do on this code so it at least tell me what argument it thinks is invalid?

Thanks

Upvotes: 0

Views: 49

Answers (1)

glitch
glitch

Reputation: 101

Change your error message to "Error: Invalid option '$1'" and you'll find that it now says Error: Invalid option '--'. Is there a -- by itself on the command line?

>&2 redirects the message to standard error so it will appear on the console even if you run ./update-data.sh | grep something for instance.

Upvotes: 3

Related Questions