Issue
I am trying to use an IF statement to:
- Check if a server is UP
- Check if a file exists on the server
hostname="somehost.com"
file="somefile.txt"
URL="https://${hostname}/some/directory/${file}"
if [ "ping -c 1 -W 1 ${hostname} 2>/dev/null" ] & [ "wget --spider ${URL} 2>/dev/null" ];
then
echo -e "${hostname} is UP and ${file} is AVAILABLE"
else
echo -e "${hostname} is DOWN or ${file} is UNAVAILABLE"
fi
I have tried testing the IF statement by entering an incorrect hostname and an incorrect file but, the result is incorrect.
Current Output
somehost.com is UP and somefile.txt is AVAILABLE
Expected Output
somehost.com is DOWN and somefile.txt is DOWN
Solution
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.
From: https://www.gnu.org/software/bash/manual/bash.html#Compound-Commands
Answered By - Nic3500 Answer Checked By - Katrina (WPSolving Volunteer)