simple bash script to check if tunnel exists [catching stdout and stderr]
i'm trying to write a simple bash script for a cronjob to check if some interfaces (tunnels) exsisting. if not i'd like to start another script that (re)starts them. my problem is: i don't get the "IF" working:
#!bin/bash
for i in tun1 tun2 tun3 tun3 tun4 tun5
do OUT="$(ip a show $i up)";
if [[ $OUT == *"does not exist."* ]]; then
echo "$i is down"
else
echo "$i is up"
fi
done
current output is:
./check_tunnel.sh
tun1 is up
tun2 is up
tun3 is up
tun3 is up
tun4 is up
Device "tun5" does not exist.
tun5 is up
Solution 1:
Device "tun5" does not exist.
is printed to stderr (standard error).
Your script catches only stdout (standard output).
You may redirect stderr to stdout to fix the problem (trailing 2>&1
).
OUT="$(ip a show $i up 2>&1)"