I’m trying to write a bash script that will log the connection state of a wifi connection by mac address but I’m having some trouble with the syntax of the code.
#!/bin/sh
client="E1:FA:41:23:00:AC"
logfile="/var/logs/wificonn.log"
timestamp=$(date +"%Y%d%m %T")
assoclist=$(wl assoclist | grep $client)
loglast=$(tail -n 1 $logfile | grep $client Not Connected)
notconnmsg="$timestamp $client Not Connected"
connmsg="$timestamp $client Connected"
if [ ! -f $logfile ]; then
touch $logfile
fi
if [ -z $assoclist ]; then # Client is not connected
if [ -n $loglast ]; then # Last log is connected show not connected message
echo $notconnmsg
echo $notconnmsg >> $logfile
fi
else # Client is connected
if [ -z $loglast ]; then # Last log is not connected show connected message
echo $connmsg
echo $connmsg >> $logfile
fi
fi
This will be run as a cron job every 60seconds and only want it to show/log connected or not connected events only if the opposite is true. I am trying to achieve this by checking the last log entry. eg if last log was not connected and now connected, log to file. This is where the problem is.
thanks
Try wrapping
$loglastin double quotes:When
$loglastis empty, bash seesif [ -n ]. You are not evaluating an empty argument, you are evaluating nothing. You can try the following to see the difference: