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?

1
  • 2
    bash -x yourscript arg1 ... will tell you reasonably well, once you get used to deciphering the output. Commented Oct 13, 2013 at 21:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.