Custom exit message according to flag

Hello everyone,

I'm currently working on a script that uses curl's to check internet connectivity with and without a proxy being specified.
Basically what I'd like to achieve is having the script output fail or success according to the "method" used (direct or via proxy).

A very ugly way of doing it:

 # "curl_args" contains "-x $2" and gets set if $2 exists
curl ${curl_args} "$1" &> /dev/null

if [ $? == 0 ]; then
   if [ "$#" == 1 ]; then
      printf "OK - able to connect to $1\n"
      exit 0
   elif [ "$#" == 2 ]; then
      printf "OK - able to connect to $1 from $2\n"
      exit 0
   fi
else
   if [ "$#" == 1 ]; then
      printf "CRITICAL - unable to connect to $1\n"
      exit 1
   elif [ "$#" == 2 ]; then
      printf "CRITICAL - unable to connect to $1 from $2\n"
      exit 1
   fi

The script expects 2x arguments. The first one is the URL and the second one is the proxy address.

Whats a more dynamic way of writing that ugly if statement?

Id like to see the code for the commented line, because the code would be shorter.

Other than that sounds more lile homework to me.
Is it?

no it's not homework.

I've made some changes to it (whole code as requested):


# if we specify a proxy on $2, add the -x flag to curl
if [ "$proxy" ]; then
  # the whitespace is important
  curl_args+=" -x $2"
  exit_msg+=" via $proxy"
fi

usage() {
  printf "$(basename $0) URL [proxy]\n"
  exit 0
}

exit_ok() {
  printf "OK - $1"
  exit 0
}

exit_fail() {
  printf "FAILED - $1"
  exit 1
}

curl_cmd="curl ${curl_args} $url"
case $# in
  1) $curl_cmd &>/dev/null && exit_ok "$exit_msg" || exit_fail "$exit_msg"
     ;;
  2) $curl_cmd &>/dev/null && exit_ok "$exit_msg" || exit_fail "$exit_msg"
     ;;
  *) usage
     ;;
esac

I'm currently trying to get rid of exit_ok() and exit_fail()

I didnt request the whole code, just the first commented line.
Anyway, thanks.

First and foremost, both "${2:-}" are useless, as that would fill an empty variable with... emptyness.
Second, you dont want something in a variable that defines an extra option - if it hasnt been set by the user.

I'll keep the short approach you've had in the first post:

curl_args="--fail --max-time 3"


case ${#@} in
1)	url="$1"
	exit_msg="connecting to $url"
	;;
2)	url="$1"
	curl_args="$curl_args -x \"$2\""	# The 'inner' quotes needs escape so they are there when the variable is reused
	exit_msg="connecting to $url via $2"
	;;
esac

curl $curl_args "$url"
ret=$?

[[ 0 = $ret ]] && \
	exit_msg="SUCCESS - $exit_msg" || \
	exit_msg="FAILURE - $exit_msg"

echo "$exit_msg"
exit $ret

hth & hf

hmm, very interesting construct. thank you for that.

One question though regarding the following piece of code:

[[ 0 = $ret ]] && \
	exit_msg="SUCCESS - $exit_msg" || \
	exit_msg="FAILURE - $exit_msg"

Why not do a proper if/else statement here?

I did that because of the "set -u". What would be the right approach?

Ok, can you elabortate on that? Why is it wrong (?) to append a string to a variable?

You're welcome.

It's shorter and i'm lazy.
Also it only 'contains' 1 task per situation, so this is enough.
However, if there are more lines to be made for each situation, i'd go with a proper if/else statement as well.

If you want to check wether a variable exists (or has content) or not (empty) and DECIDE what to execute according to that, it's a bad choice to fill the variables anway.

One usualy uses this to make sure a REQUIRED variable gets it's DEFAULT or FALLBACK values, as in:

var=${1:-default-value}

I have.. uhm.. misspelled.. my... uhm.. concern... in the previous post, because:
${1:-} doesnt actualy do something, and is basicly the same as ${1} , but it (the first one) indicates that you wanted to put some variable there, which then might interfere.
Also, by setting a string to an empty variable, you'd say the script to ALWAYS use '$proxy', because '$proxy' would contain something. (well, not in your actual code, that's just bad syntax)

The set -u only requires you to declare the variable before invoking it.
So any of these are valid options for that case/command:

hth

--- Post updated at 21:54 ---

On a further note, your exit codes in those functions just exit the fuction, therefor return could be used there.

For example, try this:

my_msg(){ echo "${1:-some string i dont care} ; return 1 ; }
my_msg
ret=$?
echo $ret
exit $ret

And then change the return number.

Hth

1 Like