0

Consider I have a curl command:

'curl' '--noproxy' 'check' '-H' 'Authorization: token ${TOKEN}' '-H', 'Accept: application/vnd.github.v3+json' '--create-dirs' '-o' successFile.toString()  "Url"

Below script is to test the working of curl command and Here I want to add long option --noproxy to the below script:

#!/bin/sh

string_matches() {
    eval "
    case \$1 in
        $2) return 0 ;;
        *) return 1 ;;
    esac
    "
}

FULL_COMMAND="$@"
while getopts "H:o:H:X:d:-:" OPT; do
    if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
        OPT="${OPTARG%%=*}"    
        OPTARG="${OPTARG#$OPT}" 
        OPTARG="${OPTARG#=}"  
    fi
    case "${OPT}" in
      o) OUTPUT_FILE="${OPTARG}" ;;
      create-dirs) CREATE_DIRS=1 ;;
    ?) ;;
    esac
done

shift $(($OPTIND - 1))
CURL_URL=$1

if [ -n "${CREATE_DIRS:-}" ]; then
    mkdir -p $(dirname ${OUTPUT_FILE})
fi

if string_matches "${CURL_URL}" '*content.properties*'; then
    echo 'success' >"${OUTPUT_FILE}"
else
    exit 97 #WAT
fi

I tried adding noproxy in case

case "${OPT}" in
n|noproxy) NOPROXY='check';;

but it doesn't seem to work, but if I remove the noproxy option, everything else is working perfectly. Tried using this answer but no luck https://stackoverflow.com/a/28466267/21758758

  • 1
    To get some useful hints paste your script at http://www.shellcheck.net/. – Cyrus May 11 '23 at 19:27
  • 1
    `eval` is a serious code smell, and I don't see why you need it here. Anyone who can control `$2` can make your script run arbitrary code. – Charles Duffy May 11 '23 at 19:28
  • `string_matches() { case $1 in $2) return 0;; *) return 1;; esac }; string_matches foo "*oo*"`, f/e, works fine. – Charles Duffy May 11 '23 at 19:30
  • Mind, `FULL_COMMAND="$@"` is irreparably buggy. You can't store an array in a string without losing data, so your arguments with quotes and spaces will never work as long as you depend on it. (I don't see you ever _using_ `FULL_COMMAND`, though; a variable you never use can't cause bugs, though of course it's also pointless). – Charles Duffy May 11 '23 at 19:31
  • 1
    That said, for long options, I recommend against using `getopts` at all. See [BashFAQ #35](https://mywiki.wooledge.org/BashFAQ/035) for an introduction to a simpler, saner and still sh-compatible argument parsing approach. – Charles Duffy May 11 '23 at 19:32
  • Ok sounds good I will make these changes and also to include long option --noproxy should I just add like create-dirs- case "${OPT}" in o) OUTPUT_FILE="${OPTARG}" ;; create-dirs) CREATE_DIRS=1 ;; ?) ;; esac – check check May 11 '23 at 19:52
  • 2
    I was specifically recommending the "manual loop" section of BashFAQ #35, in which one would be using `case $1 in` ... `--create-dirs) create_dirs=1;;` – Charles Duffy May 11 '23 at 19:56
  • 1
    The question has a "bash" tag but the code has an "sh" shebang. See [Difference between sh and Bash](https://stackoverflow.com/q/5725296/4154375). – pjh May 11 '23 at 20:15

0 Answers0