我正在编写一个脚本来自动执行工作中的一些检查,并且我在只有 1 个长期存在的问题上遇到了麻烦,即脚本永远不会结束。
它一遍又一遍地完成所有任务。有 2 个 while 循环,一旦完成第一个循环,它就逻辑上开始第二个循环。一旦第二个完成,它就会从第一个开始。
另外,我想让它在两个循环相继完成后结束。我不经常写脚本,答案可能很简单,但到目前为止我找不到适合我的脚本的答案。
感谢您的帮助。
脚本下方:
#!/bin/ksh
#
#
#
#
day=`date +%Y%m%d`
BASEDIR=$(dirname $(readlink -f $0))
cd $BASEDIR
cd /export/home/prodwww/ftp_backup/conf
grep "mds_lin_m" *|cut -d " " -f1 > /export/home/prodwww/Production/Check/tmp/mono_list_"$day".log
cat /export/home/prodwww/Production/Check/tmp/mono_list_"$day".log |cut -d " " -f1 > /export/home/prodwww/Production/Check/log/mono_list_"$day"_.log
sed -i -e "s/:/\//g" /export/home/prodwww/Production/Check/log/mono_list_"$day"_.log
cat /export/home/prodwww/Production/Check/log/mono_list_'date'_.log |while read lines
do
grep -HL 'NbChannel' /export/home/prodwww/ftp_backup/data/"$lines"/mds.ini >> /export/home/prodwww/Production/Check/log/result_"$day"_.log;
done
cat /export/home/prodwww/Production/Check/log/result_"$day"_.log|cut -d"/" -f 7-8> /export/home/prodwww/Production/Check/log/autoswitch-list.log
sed -i -e "s/\// @ /g" /export/home/prodwww/Production/Check/log/autoswitch-list.log
awk -F" " '{print $3 $2 $1}' /export/home/prodwww/Production/Check/log/autoswitch-list.log > /export/home/prodwww/Production/Check/log/check-list.log
cat /export/home/prodwww/Production/Check/log/check-list.log |while read target
do
echo "$target">/export/home/prodwww/Production/Check/log/account.txt
cat /export/home/prodwww/Production/Check/log/account.txt|cut -d"@" -f1>/export/home/prodwww/Production/Check/log/login.txt
logon=$(cat /export/home/prodwww/Production/Check/log/login.txt)
echo $target>>/export/home/prodwww/Production/Check/data/autoswitch_check_"$day".log ; /usr/local/bin/rww CMD "$target" "ls -lrt /export/home/"$logon"/mds/conf/autoswitch.ini">> /export/home/prodwww/Production/Check/data/autoswitch_check_"$day".log
done
exit
答案1
任何循环都没有明显的理由变得“无限”。然而,重写完全相同的脚本,但将每个文件名提取到一个变量中,我们得到了一个更清晰的版本,IMO。
day=`date +%Y%m%d`
BASEDIR=$(dirname $(readlink -f $0))
cd $BASEDIR
dir1="/export/home/prodwww/ftp_backup/conf"
log1="/export/home/prodwww/Production/Check/tmp/mono_list_$day.log"
log2="/export/home/prodwww/Production/Check/log/mono_list_$day_.log"
log3="/export/home/prodwww/Production/Check/log/mono_list_date_.log"
ini1="/export/home/prodwww/ftp_backup/data/$lines/mds.ini"
log4="/export/home/prodwww/Production/Check/log/result_$day_.log"
log5="/export/home/prodwww/Production/Check/log/autoswitch-list.log"
log6="/export/home/prodwww/Production/Check/log/check-list.log"
txt1="/export/home/prodwww/Production/Check/log/account.txt"
txt2="/export/home/prodwww/Production/Check/log/login.txt"
log7="/export/home/prodwww/Production/Check/data/autoswitch_check_$day.log"
ini2="/export/home/$logon/mds/conf/autoswitch.ini"
log8="/export/home/prodwww/Production/Check/data/autoswitch_check_$day.log"
cd "$dir1"
grep "mds_lin_m" * | cut -d " " -f1 >"$log1"
cat "$log1" | cut -d " " -f1 >"$log2"
sed -i -e "s/:/\//g" "$log2"
cat "$log3"| while read lines
do
grep -HL 'NbChannel' "$ini1" >> "$log4"
done
cat "$log4" | cut -d"/" -f 7-8 > "$log5"
sed -i -e "s/\// @ /g" "$log5"
awk -F" " '{print $3 $2 $1}' "$log5"> "$log6"
cat "$log6" | while read target
do
echo "$target" > "$txt1"
cat "$txt1" | cut -d"@" -f1 > "$txt2"
logon=$( cat "$txt2" )
echo $target >> "$log7";
/usr/local/bin/rww CMD "$target" "ls -lrt $ini2" >> "$log7"
done
exit
grep -HL 'NbChannel' "$ini1" >> "$log4"
然后,很明显,对 中的每一行重复完全相同的命令log3
将会在log4
.
也许这不是你想要的。这就是以下所有命令在 中的相同值上一遍又一遍重复的原因log4
。
请注意,log2
应始终为空,因为log1
在第一个空格上被剪切。那么log1
就没有空格了。不可能cut -d " " -f1
匹配任何东西。