使用 tee 列出目录内容重定向到文件

使用 tee 列出目录内容重定向到文件

我很难理解为什么某个特定过程会失败。基本上,我的脚本中有一行列出目录内容,用于tee将输出重定向到两个文件:

retval=`ls -1 $SOURCE_DIR/${2}/${3} | tee ${COPY_LIST} ${MOVE_LIST}`

该命令在命令行上运行良好,并且两个文件都已填充(大约 8K 行)。奇怪的是,通过 cron,脚本只是“挂起”并检查文件,仅创建相同目录的部分列表(大约 3k 记录)。最后一行也是部分的,没有写完整。不确定问题是否出在tee自身内部。

    #!/bin/ksh
#DAILY RUN

#DEBUG_SCREEN=ON
LOG_FILE=/daily_archive_v2_`date +%d-%m-20%y`.log
. /stdlib.ksh

##the datetimestamp for the file that contains the list of files to be archived
LIST_TIMESTAMP=`perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d_%H%M",localtime(time());'`

Month_value=`date +%m`
typeset -i Prev_Month_value_tmp=`date +%m`
Prev_Month_value_tmp=${Prev_Month_value_tmp}-1
Year_value=`date +%y`

if [[ "${Month_value}" -eq 01 ]] then
  echo "Month is January"
  typeset -i Prev_Year_value=${Year_value}-1
Prev_Month_value_tmp=12
else
  Prev_Year_value="${Year_value}"
fi

if [[ "${Prev_Month_value_tmp}" -le 10 ]] then
  echo "Prev month is less than 10"
  Prev_Month_value="0${Prev_Month_value_tmp}"
else
  Prev_Month_value="${Prev_Month_value_tmp}"
fi
echo Previous Month Value = $Prev_Month_value

echo LOGFILE NAME SET TO $LOG_FILE
echo "DAILY REPORT [Archiving Month ${Month_value}]"
log "DAILY REPORT [Archiving Month ${Month_value}]"
log_line
SOURCE_DIR=/test
DEST_DIR=/Archives/month_"${Month_value}"_20"${Year_value}"
BACKUP_DIR=/vault/month_"${Month_value}"_20"${Year_value}"
mkdir -p $DEST_DIR
mkdir -p $BACKUP_DIR

COPY_LIST="${LIST_TIMESTAMP}_output_archiving.copy.lst"
MOVE_LIST="${LIST_TIMESTAMP}_output_archiving.move.lst"

#PARAMS : NAME, DIR, MASK
function move_files
{
  DEST_DIR=/month_"${Month_value}"_20"${Year_value}"
  FILES_COUNT=`ls -1 $SOURCE_DIR/$2/$3 | wc -l`
  if [ "${FILES_COUNT}" != "0" ]; then
    log "MOVE $FILES_COUNT $1 FILES $SOURCE_DIR/$2/$3 to $DEST_DIR/$2"
    log "MOVE $FILES_COUNT $1 FILES $SOURCE_DIR/$2/$3 to $BACKUP_DIR/$2"
    mkdir -p $DEST_DIR/$2
    mkdir -p $BACKUP_DIR/$2
retval=`ls -1 $SOURCE_DIR/${2}/${3} | tee ${COPY_LIST} ${MOVE_LIST}`
#echo retval=`ls -1 $SOURCE_DIR/${2}/${3} | tee ${COPY_LIST} ${MOVE_LIST}`
#       while read line
#         do
#         CURRENT_FILE=`echo $line`
#         #echo ${CURRENT_FILE}
#         cp -p ${CURRENT_FILE} $BACKUP_DIR/$2
#         mv ${CURRENT_FILE} $DEST_DIR/$2
#         log "File ${CURRENT_FILE} copied to $BACKUP_DIR/$4/"
#         log "File ${CURRENT_FILE} moved to $DEST_DIR/$4/"
#         done < ${COPY_LIST}
  else
    echo Nothing to move
  fi
#        gzip  $DEST_DIR/$2/*
#         gzip $BACKUP_DIR/$2/*
}

move_files  "TESTDIR" "dir2/test" "*.txt"

\rm -f $RESULTS_FILE
\rm -f $FILE_LIST
#rm -f $LOG_FILE

相关内容