0

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

4
  • 3
    You have an extra space before data-fim Commented Sep 23, 2022 at 17:44
  • 4
    The best way to debug a shell script is to put set -x at the beginning. Then you'll see every statement as it's executed. Commented Sep 23, 2022 at 17:44
  • 1
    @CarlNorum Thank you so much! I'm in shock that I was able to type so many times making the same mistake. Kind embarassing but glad it is solved Commented Sep 23, 2022 at 17:51
  • 1
    >&2 redirects the output of the command to file descriptor 2 (the error stream) Commented Sep 23, 2022 at 18:48

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! As pointed by Carl on the comment above, I added a space before "data-fim". But in any case I'll change the script to make the error message clearer in the future. Thanks once more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.