beginner script errors 2
Hi guys,
my scripts is not giving me the desired output and i can't seem to figure out why. all its doing is pinging a host and based on the exit staus print a few lines. here's the code: Code:
#!/bin/bash the issue i'm having is in the following: Code:
ping -c 1 192.168.0.45 Code:
if [[ $RETURNVAL -eq 0 ]] Code:
PING 192.168.0.45 (192.168.0.45) 56(84) bytes of data. |
The ``then'' statement must immediately follow the ``if'' statement.
That's why I usually use the following style: Code:
if [[ $RETURNVAL -eq 0 ]]; then |
Try moving the lines
echo "entering loop if return value is 0" echo "entering loop if return value is 1" AFTER the then statement. |
Thanks a lot guys, that was a fundamental error on my part. cheers!
|
The bracket tests are really designed for use with string values. In bash, when testing numerical values such as exit codes, the arithmetic operator is recommended.
Code:
if (( RETURNVAL == 0 )); then Code:
if (( RETURNVAL )); then There's also no need to do use elif here, since the result is just the inverse of the first test. "else" only will do. elif is only needed when you have two or more independent tests. Code:
if (( RETURNVAL )); then Code:
echo "entering loop if return value is 1" Edit: Come to think of it, a case construct would also be a viable option here. Code:
case "$RETURNVAL" in |
All times are GMT -5. The time now is 12:32 AM. |