【发布时间】:2018-07-13 08:49:04
【问题描述】:
我有下一个代码:
process_mem() {
used=`sed -n -e '/^Cpu(s):/p' $temp_data_file | awk '{print $2}' | sed 's/\%us,//'`
idle=`sed -n -e '/^Cpu(s):/p' $temp_data_file | awk '{print $5}' | sed 's/\%id,//'`
awk -v used=$used \
-v custom_cpu_thres=$custom_cpu_thres \
'{
if(used>custom_cpu_thres){
exit 1
}else{
exit 0
}
}'
return=$?
echo $return
if [[ $return -eq 1 ]]; then
echo $server_name"- High CPU Usage (Used:"$used".Idle:"$idle"). "
out=1
else
echo $server_name"- Normal CPU Usage (Used:"$used".Idle:"$idle"). "
fi
}
while IFS='' read -r line || [[ -n "$line" ]]; do
server_name=`echo $line | awk '{print $1}'`
custom_cpu_thres=`echo $line | awk '{print $3}'`
if [ "$custom_cpu_thres" = "-" ]; then
custom_cpu_thres=$def_cpu_thres
fi
expect -f "$EXPECT_SCRIPT" "$command" >/dev/null 2>&1
result=$?
if [[ $result -eq 0 ]]; then
process_mem
else
echo $server_name"- Error in Expect Script. "
out=1
fi
echo $server_name
done < $conf_file
exit $out
问题是读取 bash 循环应该执行 4 次(每行读取一次)。但是,如果我编写带有退出的 awk 代码,则在第一个循环之后读取 bash 循环退出。
为什么会这样?在我看来,awk 代码中的退出代码不应该影响 bash 脚本。
问候。
【问题讨论】:
-
您好,欢迎来到 Stack Overflow。你能创建一个我们可以用来解决这个问题的小脚本吗?在当前状态下,我们无法验证您的发现并帮助您解决问题或解释罪魁祸首。 (minimal reproducible example)。
标签: bash shell awk scripting exit