I have a large set of iptables rules that I control with my own bash script. Most of the commands in the script are simple iptables with the same status. I am trying to improve the script by adding a success / failure result when the script is executed.
I have a script divided into different sections. One example is the FORWARD section, where all rules apply to the FORWARD chain. At the beginning of the section, I deduced that the script started to apply FORWARD rules, and at the end I want to indicate whether all the rules were applied successfully or if any of them did not work. Here is the basic idea:
#Start FORWARD section echo -ne "Applying FORWARD rules..."
What I want to do is to catch any output or errors that may occur as a result of each iptables command and store them in an array or something like that. Then at the end of the block use the if statement to evaluate the array to see if there were any errors. If not, display the status [OK]; if they are, display the status [FAILED] and display the corresponding error.
Is there a way to do this for the entire rule block without wrapping each iptables rule in an if if [$?! = 0] expression?
source share