0

I am trying to write a terminal script; upon success I want to trigger a function and when it fails I want to send an alert. The script works except when it is successful it continues to run the commands after the 'done; line. Not sure of the proper syntax or correct way to write this.

I only want the commands after done to process upon failure, not success.

check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
   ping -c 1 $remoteIdrac &> /dev/null && echo 'Success' && break
   n=$((n+1))
   echo "Connection Failed Attempt" $n
   sleep 15
done

   ssh $localIp wg-quick down $wireguardInterface
   ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
}

So I solved with below code, just added an if statement but wonder if there is a more elegant solution than below.

check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
  ping -c 1 10.10.20.100 &> /dev/null && echo 'Success' && break
  n=$((n+1))
  echo "Connection Failed Attempt" $n
  sleep 15

    if [[ $n > 1 ]]; then
    ssh $localIp wg-quick down $wireguardInterface
    ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
    fi
done


}

1 Answer 1

1

break only breaks out of the loop. You want return to break out of the function on success:

check_connection_wireguard(){
n=0
until [ "$n" -ge 1 ]
do
   ping -c 1 $remoteIdrac &> /dev/null && echo 'Success' && return
   n=$((n+1))
   echo "Connection Failed Attempt" $n
   sleep 15
done

   ssh $localIp wg-quick down $wireguardInterface
   ssh $localIp /usr/local/emhttp/webGui/scripts/notify -i alert -s \"Backup Remote Connection\" -d \"Backup Operation failed to connect\"
}
Sign up to request clarification or add additional context in comments.

Comments

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.