当ls找不到结果时,bash ls输出到文件

时间:2016-10-06 07:33:15

标签: linux bash shell logging ls

我对bash中的脚本相对较新,并制作了以下脚本:

# Set variables
logfile=/tmp/peter/logfile.log
indir=/mls/indir/IG/
tempdir=/mls/indir/IG/IGTEMP/
MLSready=/mls/indir/IG/MLSready

#Log current date/time
date >>$logfile

#Check whether processing files are present and one MLSready file exists. If not, exit the script.
if ls ${tempdir}R* 1> /dev/null 2>&1; then 
   mv $MLSready $tempdir
   if [ $? -eq 0 ] ; then
      echo MLSready move succeeded, copy processing files from $tempdir to $indir >>$logfile
      ls -l ${tempdir}MLSready >>$logfile
      find $tempdir -type f -name 'RPUO*' -mmin +1 -exec mv {} $indir \; 
      find $tempdir -type f -name 'RFRO*' -mmin +3 -exec mv {} $indir \; 
   else
      echo MLSready does not exist, exit script >>$logfile
      exit 1
   fi 
else
   echo no processing files in $tempdir, exit script >>$logfile
   exit 1
fi

echo set new MLSready file >>$logfile
# date >$MLSready
ls -l $MLSready 1>&2 >>$logfile

exit

在脚本结束时,我没有制作新的MLSready文件。 当我执行脚本时,我希望在我的日志文件中看到最新的'ls -l'命令的结果,但是(因为我没有创建新的MLSready文件?)输出被重定向到我的提示而不是我的日志文件:

DC3_igsy3ft3.sonic:/tmp/peter> ./move_bmp_files.sh
ls: 0653-341 The file /mls/indir/IG/MLSready does not exist.

如何安排最新的'ls -l'命令的结果始终重定向到我的日志文件?

我也尝试过:

ls -l $MLSready 2>&1 >>$logfile
ls -l $MLSready >>$logfile

在我的日志文件下面:

do okt  6 09:22:39 2016
MLSready move succeeded, copy processing files from /mls/indir/IG/IGTEMP/ to /mls/indir/IG/
-rw-rw-rw-    1 oracle   505               0 okt 06 09:22 /mls/indir/IG/IGTEMP/MLSready
set new MLSready file

提前致谢!!

1 个答案:

答案 0 :(得分:0)

这是订单:

$ ls -l $MLSready >>$logfile 2>&1
$