它本来应该是一个简单的脚本,但是...在for循环中的所有内容都根本没有执行。如果IF语句为true,它将检查for语句“ oc get pods | grep -i '^pat*' | awk '{print $1}'
中的for pod”,并在while循环的开头无限次地返回。如果IF语句为false,则该脚本存在。在这种情况下,如果if语句始终为true,则应在列表上打印pod的名称。使用2>将FOR语句的stderr重定向到控制台后,我得到"command substitution: line 5: syntax error near unexpected token
newline'“`,考虑到for循环仅包含echo命令
while true; do
check=$(curl --insecure https:...... | python -mjson.tool |head -2 |grep -i status|awk -F '"' '{print $4}')
if [ $check == "OK" ]; then
for pod in `oc get pods | grep -i '^pat*' | awk '{print $1}'`; do
echo $pod
done
else
exit 0
fi
done
调试输出如下:
+ true
++ python -mjson.tool
++ head -2
++ grep -i status
++ curl --insecure https:...
++ awk -F '"' '{print $4}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 127 100 127 0 0 353 0 --:--:-- --:--:-- --:--:-- 354
+ check=OK
+ '[' OK == OK ']'
++ grep -i '^pat*'
++ awk '{print $1}'
++ oc get pods
+ true
++ head -2
++ python -mjson.tool
++ awk -F '"' '{print $4}'
++ curl --insecure https:...
% Total % Received % Xferd Average Speed Time Time Time
Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 127 100 127 0 0 415 0 --:--:-- --:--:-- --:--:-- 416
+ check=OK
+ '[' OK == OK ']'
++ awk '{print $1}'
++ grep -i '^pat*'
++ oc get pods
+ true
++ python -mjson.tool
++ head -2
++ grep -i status
++ awk -F '"' '{print $4}'
++ curl --insecure https:...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 127 100 127 0 0 423 0 --:--:-- --:--:-- --:--:-- 423
100 127 100 127 0 0 423 0 --:--:-- --:--:-- --:--:-- 423
+ check=OK
+ '[' OK == OK ']'
++ grep -i '^pat*'
++ awk '{print $1}'
++ oc get pods
+ true
....
答案 0 :(得分:0)
经过更多故障排除后,发现问题是FOR语句中的grep grep -i '^pat*'
没有返回任何值,从而跳过了执行。将模式“ ^ pat *”替换为正确的模式后,一切正常