0

I'm trying to understand this script thus have tried to comment each line. Could someone check my comments are accurate, and help me with my gaps in understanding

#!/bin/bash                                  

TESTIP=192.168.1.1   :: router ip address                        

ping -c4 ${TESTIP} > /dev/null 
:: -c4 = Stop after sending (and receiving) 4 count ECHO_RESPONSE packets
:: ${} = not sure what the curly brackets are?
:: /dev/null = throw away any error messages  



if [ $? != 0 ]  ::$?= return value is not equal to zero, i.e. good wifi                            
then                                         
    logger -t $0 "WiFi seems down, restarting" 
    ::logger = log to file, can not find any messages in the syslog or messages files 
    :: -t = tag
    :: $0 = DO NOT UNDERSTAND!! how can there be a variable called "0"?


    ifdown --force wlan0   :: disables the wifi interface                 
    ifup wlan0   ::enable it again                            
else                                        
    logger -t $0 "WiFi seems up."           
fi  

Iv'e tested this script on my pi, by running it every min, but cannot see any messages in the logs from the script. I can however see it being called by the cron.

  • 1
    I think $0 is the first argument. So I think you should call it with ./script-file.sh tag-name. – Gerben Jul 1 '14 at 18:38
2

Let's say that you have named this script wifitest.sh. Below is a valid shell-script with comments.

#!/bin/bash                                  
# set TESTIP variable to the IP address of the gateway
TESTIP=192.168.1.1

# Send four (-c4) ICMP echo-request ("ping") packets to the the
# gateway address ($TESTIP), discarding the standard output (by
# redirecting to the file /dev/null, which is a blackhole)
# ${TESTIP} is equivalent to $TESTIP
ping -c4 ${TESTIP} > /dev/null

# the ping command returns an exit code:
# - 1 if no ICMP echo-reply packets were received at all
# - 2 on other errors
# - 0 otherwise (that is, on success)
# The exit code of the last command is stored in the special variable
# $?. The '[' command tests for a condition, in the following case
# it tests whether the exit code is non-zero (an error occurred)
if [ $? != 0 ]
then
    # No ping replies were received. Assume that the Wi-Fi connection
    # is dead, and try to restart it.

    # 'logger' is a command that registers a message to the system
    # logger ("syslog"). It uses the script name (stored in $0) as
    # tag ('-t'). In the syslog you will then probably find:
    # wifitest.sh: WiFi seems down, restarting
    logger -t $0 "WiFi seems down, restarting"

    # Here follows a "restart" of the network interface.
    # Bring the network interface "wlan0" down...
    ifdown --force wlan0
    # ...and bring it up again, hopefully it gets configured automatically.
    ifup wlan0
else
    # At least one ICMP reply was received, register this to the syslog,
    # again using the tag equal to the script name ($0)
    logger -t $0 "WiFi seems up."           
fi

If you would like to learn more about a command, execute the shell command man <command>, for example man ping to learn more about the -c option. For information about the constructs $0 and $?, read the manual page of bash.

The logger command sends messages to the syslog, so you should have a syslog daemon running. On Debian-like systems, you can probably find your logs in /var/log/syslog.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.