My end goal is to create a script that runs on startup that turns off wifi when an Ethernet cable is plugged in, and vice-versa. This is the script I've created so far:
#!/bin/bash
File=/sys/class/net/eth0/carrier
function toggle {
if grep -q 1 "$File"; then
echo "Yes"
else
echo "No"
fi
}
toggle # Once on startup
while inotifywait -q $File; do
toggle
done
I've confirmed that plugging in and unplugging the Ethernet cable does change /sys/class/net/eth0/carrier
, but for some reason, absolutely nothing happens when it's updated. I tested if the command was detecting changes at all by changing File to be a temporary file in my home directory called "test.txt". Whenever I accessed the file, changed its contents etc., the script continued as expected. I tried overwriting the file by doing echo '1' > test.txt
, which worked. Deleting the file resulted in an error and the script terminating, but at least it registered a response.
I could just set the script to execute every second, but I'd like to avoid that if possible.