1

New to bash here. Trying to write a while loop that checks for 3 conditions, 2 user input and if a ping command failed. How to combine all 3 into 1 big statement? Would like to check that user didn't enter Y or y or ping to google.com failed

attempt:

# Ask user to confirm ethernet cable connected or quit with Yy or Qq in continous loop.
echo -e "Connect ethernet cable to Pi Slave. Type (Y) when done, (Q) to quit:" 
read confirm
# If confirm is Y or y, continue to rest of script
while [[ "$confirm" != "Y" && "$confirm" != "y" && ping -q -c 1 -w 1 google.com  ]]
do
    if [[ "$confirm" == "Q" || "$confirm" == "q" ]]; then
        exit 1
    fi
    echo -e "Connect ethernet cable to Pi Master. Type (Y) when done, (Q) to quit:" 
    read confirm
done

1 Answer 1

3

[[ ... ]] only accepts simple tests. Full commands like ping stand on their own.

while [[ "$confirm" != "Y" && "$confirm" != "y" ]] && ! ping -q -c 1 -w 1 google.com

To get rid of the duplicate prompt code you could rewrite the loop as:

while true; do
    read -p "Connect ethernet cable to Pi Slave. Type (Y) when done, (Q) to quit: " confirm

    case $confirm in
        [yY]) ping -q -c 1 -w 1 google.com && break;;
        [qQ]) exit 1;;
        *)    continue;;
    esac
done
Sign up to request clarification or add additional context in comments.

1 Comment

I like that explaination that [[ ]] is a command. How come you edit it out?

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.