【发布时间】:2022-10-17 20:46:27
【问题描述】:
我将端口保存在要检查的数组中,然后运行 for 循环来检查 iptables 规则列表中的端口。我想用未找到 msg 来回显不在 iptables 列表中的端口。试图在循环内添加一个 if 条件但不起作用。 这是代码:[非工作;)]
#!/bin/bash
array=( 3306 1403 8080 443 22 )
for i in "${array[@]}"
pc=(iptables --list | grep $i | cut -d " " -f1)
do
if [ "${pc}" = "ACCEPT" ]
then
echo "ok"
else
echo "Port not found"
fi
done
错误:
array.sh: line 4: syntax error near unexpected token `|'
array.sh: line 4: `pc=(iptables --list | grep $i | cut -d " " -f1)'
array.sh: line 5: syntax error near unexpected token `do'
array.sh: line 5: `do'
【问题讨论】:
-
什么不工作?
-
@BenjaminW。整个脚本。一些语法问题?
-
确实......你把它放在ShellCheck吗?:)
-
pc=... 行当然应该在“do”关键字之后。你的意思可能是 pc=$(...) 而不是 pc=(...)
-
“整个脚本不起作用”可能意味着很多事情。你有错误吗?出乎意料的输出?你期望什么输出?