【发布时间】:2011-08-07 06:55:49
【问题描述】:
我有下面的代码。基本上这段代码将ls -tr $FROM_DIRECTORY 然后将输出重定向到/tmp/msc.load.tmp。在此之后,for 循环将执行并将前 3000 个文件移动到另一个目录。代码运行良好,但有时会挂起。我不知道它为什么挂起。有谁知道脚本的问题是什么?
ls -tr $FROM_DIRECTORY > /tmp/msc.load.tmp
echo "$sysdate -- Listing Diretory " >>$LOGFILE
# From the file list, get the 3000 from the top to move. Skip the remaining files in the list
# in this iteration.
# Version 1.1 - List the files from the temporary file.
for file in $(cat /tmp/msc.load.tmp | grep 'MSCERC.*' | head -3000 )
do
mv $FROM_DIRECTORY/$file $DESTINATION_DIRECTORY
done
echo "$sysdate -- End of Script " >>$LOGFILE
exit 0
# End of script.
【问题讨论】:
-
最好只使用
find命令,省去中间文件。 -
使用“bash -x”运行它,这样您就可以看到它挂起的位置和原因
-
我是 unix 新手。我知道如何在脚本中添加 -x 吗?可以举个例子吗?
-
cat、head和正则表达式通配符都是不必要的。另外,don't read lines withfor.grep -m 3000 MSCERC /tmp/msc.load.tmp | while read -r file; do...最后,quote your variables.