我while读文件,最后用remsh进行远端操作,操作实际完成,但执行完就退出循环?实际文件有多条记录。见下:
while read filename
do
echo $SrcFilePath/$filename
rcp root@$IP:$SrcFilePath/$filename $DesFilePath/$filename
echo $?
remsh $IP -l root rm $SrcFilePath/$filename
echo $?
done < 0701.FL
显示echo $? 均未0,但执行一行就退出。
如果吧remsh $IP -l root rm $SrcFilePath/$filename注掉就没问题了。
自己式了式如果在后面加一句其他的句子,也可以
while read filename
do
echo $SrcFilePath/$filename
rcp root@$IP:$SrcFilePath/$filename $DesFilePath/$filename
echo $?
remsh $IP -l root rm $SrcFilePath/$filename
echo $?
filehead="aaa"
done < 0701.FL
循环执行正常。我晕了:)
对不起,上面说错了,最后加一句,还是退出循环???????为什么阿
It's not a bug of remsh.
you can change your script in this way:
#add this line
exec 9<&0 0<0701.FL
#here are the lines from your old script
while read filename
...
done #notice no <
exec 0<&9
This does work. I have tested it .
you can also use read -r instead of read
like this:
while read -r filename
do
...
done <0701.FL
It also work.
我按照后一种做法
while read -r filename
do
echo $filename
rcp root@$IP:$SrcFilePath/$filename $DesFilePath/$filename
echo "bbbb"$?
if [ $? -eq 0 ]
then
#echo $SrcFilePath/$filename
remsh $IP -l root rm $SrcFilePath/$filename
echo "aaaa"$?
fi
done <0701.FL
还是一次成功退出
前一种
exec 9<&0 0<0701.FL
while read filename
do
echo $filename
rcp root@$IP:$SrcFilePath/$filename $DesFilePath/$filename
echo "bbbb"$?
if [ $? -eq 0 ]
then
#echo $SrcFilePath/$filename
remsh $IP -l root rm $SrcFilePath/$filename
echo "aaaa"$?
fi
done #notice no <
exec 0<&9
还是不行啊,不明白
change this line:
remsh $IP -l root rm $SrcFilePath/$filename
to :
remsh $IP -l root rm $SrcFilePath/$filename </dev/null
then test it.
谢谢啦,可以了,不过能不能解释一下阿,为什么这样就可以了?
因为,rsh,rcmd,remsh之类运行时拷贝当前输入作为自己的输入,把自己的标准输出转到当前的标准输出.</dev/null便可以避过这一点了。