bash脚本的偏见行为

时间:2012-04-25 12:35:19

标签: bash unix loops redirect

我有8个文件夹,即HCIVR5,HCIVR6,HCIVR7,HCIVR8,IVR5,IVR6,IVR7,IVR8。 我先写下这段代码

ls -d IVR* > temporary.txt

将此代码保存在名为run的文件中。然后使用./run执行它。当我尝试打开文件时(使用cat并通过点击正常打开),它说"无法访问temporary.txt。

这是我的整个代码

ls -d IVR* > temporary.txt
while read ivr_line;do echo $ivr_line;cd $ivr_line;./ivr_alarm;cd ..;done < temporary.txt
ls -d HCIVR* > temporary.txt
while read hcivr_line;do echo $hcivr_line;cd $hcivr_line;./hcivr_alarm;cd ..;done < temporary.txt

现在这个程序适用于IVR5,6,7,8。之后我得到了错误 ./run:第4行:temporary.txt:没有这样的文件或目录 即使我评论前两行我得到相同的错误

ivr_alarm的代码就是这个

mkdir TEMP
day=`date +%d-%m-%Y`
read ip<ip.txt
read ivr_number<ivr_number.txt
while read line;do scp -i ivr"$ivr_number"_key -r root@$ip:${line%?} ./TEMP/ ;done < files_needed.txt
mv ./TEMP/.bash_history ./LOGS/.bash_history_"$day"
cd TEMP
for file in *;do 
    mv $file ../LOGS/${file%.*}_"$day".${file##*.}
done
cd ..
rmdir TEMP

hcivr_alarm的代码就是这个

day=`date +%m-%d-%Y`
read ip<ip.txt
read ivr_number<ivr_number.txt
rm alarmtest
rm ./LOGS/HEALTH_CHECK_$day.log
while read line;do
echo "ssh -i hcivr"$ivr_number"_key root@$ip '${line%?}'>>./LOGS/HEALTH_CHECK_$day.log" >> alarmtest
done < commands.txt
chmod 700 alarmtest
./alarmtest

和alarmtest(对于HCIVR5,对HCIVR6的hcivr6_key的关键更改等)是

ssh -i hcivr5_key root@172.19.66.137 'ocstatus'>>./LOGS/HEALTH_CHECK_04-25-2012.log
ssh -i hcivr5_key root@172.19.66.137 'ocmp-bre status'>>./LOGS/HEALTH_CHECK_04-25-2012.log

现在这个程序适用于IVR5,6,7,8。之后我得到了错误 ./run:第4行:temporary.txt:没有这样的文件或目录 即使我评论前两行我得到相同的错误。我哪里错了。因为当我手动打开所有HCIVR文件夹并运行./hcivr_alarm时一切正常

1 个答案:

答案 0 :(得分:0)

试试这个:

ls -d IVR* > temporary.txt
while read ivr_line;do echo $ivr_line;pushd $ivr_line;./ivr_alarm;popd;done < temporary.txt
ls -d HCIVR* > temporary.txt
while read hcivr_line;do echo $hcivr_line;pushd $hcivr_line;./hcivr_alarm;popd;done < temporary.txt

我看不出怎么样,但我认为你搞砸了当前的目录,不知何故。