user319862
user319862

Reputation: 1857

Construct command based on runtime information

I need to run a shell script in ash. How do I get around the lack of arrays for building a command dynamically without running into problems?

Here is the code block I am trying to fix:

CURL_ARGS=('--noproxy' '*' '--verbose' '--silent' '--include' '--write-out' '%{http_code}')
if [ "$CURL_HOST_HEADER" != "" ]; then CURL_ARGS+=(--header "$CURL_HOST_HEADER"); fi
CURL_ARGS+=("http://$CURL_HOST:$CURL_PORT$CURL_PATH")

echo Proceeding with curl cmd: curl "${CURL_ARGS[@]}"

n=0
until [ $n -ge 5 ]
do
    n=$[$n+1]
    CURL_RESPONSE=$(curl "${CURL_ARGS[@]}")
    PAGE=${CURL_RESPONSE:0:-3}
    HTTP_STATUS=${CURL_RESPONSE: -3}

    if [ "$HTTP_STATUS" != "200" ]
    then
        echo "Curl failed with http status $HTTP_STATUS. Retrying shortly if attempts remain"
        sleep 15
    else
        break
    fi
done

if [ "$HTTP_STATUS" != "200" ]
then
    LOGFILE=curlable-error-page-$(date +%s).html
    echo "CURL CMD:" > $LOGFILE
    echo "curl ${CURL_ARGS[@]}" >> $LOGFILE
    echo "STATUS:" >> $LOGFILE
    echo "$HTTP_STATUS" >> $LOGFILE
    echo "RESPONSE:" >> $LOGFILE
    echo "$PAGE" >> $LOGFILE
    exit 1
fi

Upvotes: 0

Views: 26

Answers (1)

chepner
chepner

Reputation: 532023

Without arrays, your next best option is to define a function so you can take advantage of its (unused) positional parameters to act as a pseudo-array.

As an example, I'll pretend the header is passed as an argument, rather than accessed as a global variable like the host, port, and path.

run_curl () {
  # save any arguments first
  curl_host_header=$1

  # Repurpose $@ for use when you call curl
  set -- '--noproxy' '*' '--verbose' '--silent' '--include' '--write-out' '%{http_code}'
  if [ -n "$curl_host_header" ]; then
      set -- "$@" --header "$curl_host_header"
  fi
  set -- "http://$CURL_HOST:$CURL_PORT$CURL_PATH" 


  n=0
  until [ "$n" -ge 5 ]
  do
    n=$(($n+1))
    curl_response=$(curl "$@")
    page=${curl_response:0:-3}
    http_status=${curl_response : -3}

    if [ "$http_status" != "200" ]
    then
      echo "Curl failed with http status $HTTP_STATUS. Retrying shortly if attempts remain"
      sleep 15
    else
      break
    fi
  done

  if [ "$http_status" != "200" ]
  then
    logfile=curlable-error-page-$(date +%s).html
    {
      echo "CURL CMD:"
      echo "curl $*"  # $* is fine for embedding in a larger string
      echo "STATUS:"
      echo "$http_status"
      echo "RESPONSE:"
      echo "$page"
    } > "$logfile"
    exit 1
  fi
}

run_curl                      # No extra headers
run_curl "$CURL_HOST_HEADER"  # Extra header 

Upvotes: 1

Related Questions