0

For example, I'm writing a bunch of iptables rules in a bash script. When I run the script, shell says iptables: No chain/target/match by that name. I don't know what's going on so I copy and paste every line into shell and run them separately to figure out which line is causing trouble. BTW. it turns out that I put "OUTUT" instead of "OUTPUT" in one rule.

Is there anyway that shell can tell me like [line 53]: iptables: No chain/target/match by that name., so I know where the problem is?

harpun
  • 4,022
  • 1
  • 36
  • 40
user1537085
  • 404
  • 5
  • 18

2 Answers2

0

I'm not bash expert but what I was doing is adding echo with information regarding the progress and read (wait until keypressed) that will let me do the process step by step.

yossico
  • 3,421
  • 5
  • 41
  • 76
0

Nearly all programs return success and error conditions. You can include error checking for each program your script calls, and take appropriate action on error (like undoing previous work, exiting out, etc). This is particularly useful if line 4 should never execute if line 3 fails.

The exit status of the program you just called is stored in $? .

Example (pseudocode - you'll need to modify the syntax to be correct)

 Iptables foo bar baz; if ($? != 0) echo 'failed to update iptables' && exit 1; fi

additionally, you can turn on various levels of tracing with set -f , set -v , and set -x . See the links below for full details.

http://tldp.org/LDP/abs/html/exit-status.html http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html

atk
  • 9,244
  • 3
  • 32
  • 32