2 harrison520 harrison520 于 2014.10.24 10:52 提问

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 "\n\n" >>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++))
do

Item_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
done

done < "Linux_1410221605.txt"

Linux_1410221605.txt格式
192.168.1.1 test -- -- XX -- -- -- -- 1410231700

脚本运行后,发现两个参数没有传递到function,而且程序一直循环,
没找到错误,求解!!! 不胜感激!

1个回答

dncszp
dncszp   2014.11.08 11:28

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")

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!