I'm relatively 'beginner' in the world of Linux and bash and I cannot figure this out.
Among other things I want to modify iptables if 'string' doesn't exist in there, but it doesn't seem to work. Not sure if this is because of the if statements, exit code, syntax or sudo permissions or other.
When the script is being run automatically via cron, it performs the
if clause even if
iptables does contain the string I'm looking for. This is verified by
- printing iptables in terminal both seconds before and after a scheduled run
- by adding an
echo "this" >>/log/file.logto the if clause.
#!/bin/bash iptables -L -n -v | grep 126.96.36.199 if [ $? != 0 ]; then command-to-perform fi
#!/bin/bash iptablesvar=$(iptables -L -v -n) if [[ $iptablesvar != *"188.8.131.52"* ]]; then command-to-perform fi
I've tried both, triggered by (sudo) crontab with the following line:
*/1 * * * * /bin/bash /home/username/path/to/script-file.sh
What baffles me, is that both the options above seems to work when entered directly into the terminal like so:
sudo iptables -L -n -v | grep 184.108.40.206 if [ $? != 0 ]; then echo "not found" ;fi if [ $? == 0 ]; then echo "found" ;fi var=$(sudo iptables -L -n -v) if [[ $var != *"220.127.116.11"* ]]; then echo "n" ;fi if [[ $var == *"18.104.22.168"* ]]; then echo "y" ;fi
If relevant, my system is a fresh Linux Mint 19.3 Tricia