问题描述
- shell脚本执行异常,一直循环
-
脚本目的,分析脚本执行结果,将有问题的项目附加到结果后面。#!/bin/bash
function analyse_report()
{
abnormal_item=$1
IP=$2
case "$abnormal_item" in
3)
echo "hostname of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/hostname.log >>Linux_1410221605.txt
;;
4)
echo "system_version of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/uname.log | awk '{print $3}'>>Linux_1410221605.txt
;;
5)
echo "disk_usage of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/df.log>>Linux_1410221605.txt
;;
6)
echo "CPU_usage of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/iostat.log>>Linux_1410221605.txt
;;
7)
echo "IO_usage of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/iostat.log >>Linux_1410221605.txt
;;
8)
echo "MEM_usage of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/free.log>>Linux_1410221605.txt
;;
9)
echo "SWAP_usage of $IP">>Linux_1410221605.txt
cat ../result/1410231618/result_192.168.59.199/free.log| >>Linux_1410221605.txt
;;
esac
}
echo "nn" >>Linux_1410221605.txt
echo "****************************The following is the abnormal items****************************n" >>Linux_1410221605.txt
while read line
do
Result_line=echo $line |grep ^[[:digit:]]|grep "XX"
for((i=1;i<=7;i++))
doItem_num=$[$i+2] >/dev/null 2>&1
IP=echo $Result_line | awk '{print $1}'
Result=echo $Result_line|tr -s [" "] [":"]|cut -d ":" -f $Item_num
if [ "$Result" == "--" ]
then
continue
else
analyse_report $Item_num $IP
fi
donedone < "Linux_1410221605.txt"
Linux_1410221605.txt格式
192.168.1.1 test -- -- XX -- -- -- -- 1410231700脚本运行后,发现两个参数没有传递到function,而且程序一直循环,
没找到错误,求解!!! 不胜感激!
解决方案
Result_line=echo $line |grep ^[[:digit:]]|grep "XX"
IP=echo $Result_line | awk '{print $1}'
Result=echo $Result_line|tr -s [" "] [":"]|cut -d ":" -f $Item_num
赋值要加``或$()
即
Result_line=$(echo $line |grep ^[[:digit:]]|grep "XX")