Complete the code to ping a host once and check if it is reachable.
ping -c [1] 8.8.8.8
The -c option specifies the number of ping requests to send. Using 1 sends a single ping.
Complete the code to store the ping command's exit status in a variable.
ping -c 1 8.8.8.8 status=[1]
The special variable $? holds the exit status of the last command run.
Fix the error in the if statement to check if the ping was successful.
if [ [1] -eq 0 ]; then echo "Host is reachable" else echo "Host is unreachable" fi
Variables in bash must be prefixed with $ to get their value inside conditions.
Fill both blanks to create a loop that pings a host 3 times and prints the result each time.
for i in [1]; do ping -c 1 8.8.8.8 result=$? if [ $result [2] 0 ]; then echo "Ping $i: Success" else echo "Ping $i: Fail" fi done
The loop iterates over the numbers 1 2 3 (without quotes). The numeric comparison operator for equality in bash is -eq.
Fill all three blanks to create a script that logs ping results with timestamps to a file.
for i in [1]; do ping -c 1 8.8.8.8 > /dev/null status=$? echo "$(date [2]) Ping $i: $[3]" >> ping_log.txt done
The loop runs 5 times. The date command uses the format string "+%Y-%m-%d %H:%M:%S" for timestamps. The variable holding ping success is status.