linux - Check for errors in a script command -
how check error in command before run it, example, if wanted run iptables command, input wrong variable of script made, example "asdiojaosdi" $port variable, , when script tries plug iptables command, returns error, want echo "error"
i want syntax check huge command
iptables -t nat -a prerouting -p tcp -d $filip --dport $port -j dnat --to-destination $cusip && iptables -a forward -p tcp -m state --state new,established,related -j accept && iptables -t nat -a postrouting -d $cusip -j snat --to-source $secip && iptables -t nat -a postrouting -j snat --to-source $filip
in ixish environments, programs commonly return value different 0
in case of failure. error messages expected logged standard error, can captured via redirecting file descriptor 2
.
you can test so:
#!/bin/bash program option1 option2 2>error.log.$$ result=$? if [ $result -ne 0 ]; echo program failed result=$result: $(cat error.log.$$) rm error.log.$$ fi
Comments
Post a Comment