使用if-else语句来检查在bash脚本编程输出 [英] Using If-Else statement to check for output on Bash scripting
问题描述
我想在bash脚本来运行以下命令
I would want the bash scripting to run the following command
iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 10000
,如果没有它的输出发现使用
if there is no output of it found using
iptables -t nat --list
我如何使用的if-else去找输出。我可以使用猫?
How can I use the If-Else to look for the output. Can i use 'cat' ?
推荐答案
您可以使用grep与iptables的名单,取决于你如何试图与之匹敌。
You could use grep with the iptables list, depending on how you're trying to match it.
if iptables -t nat --list PREROUTING | grep -- '--destintation-port 80' | grep -q -- '--to-port 10000'
iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 10000
fi
这看起来是否有preROUTING条目,涉及两个--destination-80端口和--to端口10000。如果输出字符串比较predictable你可以使用一个单一的grep吧,但我不知道iptables的不够好,提供,作为解决方案的一部分。
This will look if there is a PREROUTING entry that concerns both --destination-port 80 and --to-port 10000. If the output string is more predictable you could use a single grep for it, but I don't know iptables well enough to offer that as part of the solution
这篇关于使用if-else语句来检查在bash脚本编程输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!