Dika Dika - 5 months ago 10
Linux Question

Check whenever the output contain some string. And add it to logical if statement?

So basicly i need to scan my access point every 10 minutes. This job is done by using this command

ssid=iw wlan0 scan | grep -E 'MY SSID NAME'


The output like this

SSID: MY SSID NAME


But when i add some if-else using the output command above, it fails

if [[ $ssid == "SSID: MY SSID NAME" ]]
then
echo "SSID Detected"
else
echo "SSID NOT Detected"
fi


it will return false statement ("SSID NOT Detected"). The fact is, my SSID available and broadcasting.
Need help.

And Thank you :)

Answer

You can test the exit status of grep instead of the result, that is

if iw wlan0 scan | grep -q 'MY SSID NAME'; 
then echo "Detected"; 
else echo "Not Detected"; 
fi