我希望bash脚本运行以下命令
body = str("The following items are in the list: ")
items = ["a", "b", "c", "d"] # list of strings
for i in items:
body = body.__add__(i + ",")
print(body)
如果没有使用
找到它的输出iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 10000
如何使用If-Else查找输出。我可以使用“猫”吗? ?
答案 0 :(得分:2)
使用 $()
来捕获命令的输出,并使用 -z
来确定它是否为空:
output=$(iptables -t nat --list)
if [ -z $output ] # returns true if the length of $output is 0
then
output=$(iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 10000)
fi
答案 1 :(得分:2)
您可以将grep与iptables列表一起使用,具体取决于您尝试匹配它的方式。
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
这将查看是否存在关于--destination-port 80和--to-port 10000的PREROUTING条目。如果输出字符串更可预测,你可以使用单个grep,但我不知道知道iptables足以提供解决方案的一部分