我需要有关如何改进以下脚本以提高效率(时间和资源,以及更好的错误检测)的建议。以下是脚本功能的简单描述:
功能:脚本在 crontab 中为“ins”用户运行,在“输入路径”中搜索 .DAT 文件。如果找到 .dat 文件,则将其移至“工作目录”,将其作为批处理执行,并在 .dat 文件执行完成后将其移至“输出文件夹”。.dat 文件包含一系列用于将数字插入数据库的类似命令。
#!/usr/bin/ksh
def_path="/apps/ins/"
env_path="/apps/ins/eir/bin"
input_path="/apps/ins/eir/batch/input/"
work_path="/apps/ins/eir/batch/working/"
output_path="/apps/ins/eir/batch/output/"
moved_path="/apps/ins/eir/batch/processed/"
log="/apps/ins/BLA/log/"
date=`date '+%d%b%y'`
cd $input_path
listcount=`ls -rt *.dat |wc -l`
list=`ls -rt *.dat`
echo "`date +%H:%M:%S`| Total No of DAT files available are # $listcount #\nName of the files are...\n $list " >> $log/$date.log 2>&1
if [[ -e $def_path/.bla_processing ]];
then
echo "`date +%H:%M:%S`| Script is already running" >> $log/$date.log 2>&1
exit
fi
for fname in `ls *.dat | awk -F. '{print $1}'`
do
touch $def_path/.bla_processing
mv $input_path/$fname.dat $work_path/$fname.dat
echo "##################################################" >> $log/$date.log 2>&1
echo "## Filename = $fname.dat ## Time = `date +%H:%M:%S` ##" >> $log/$date.log 2>&1
echo "##################################################" >> $log/$date.log 2>&1
cd $env_path
. /apps/ins/.profile >> $log/$date.log 2>&1
echo "Username is `whoami`" >> $log/$date.log 2>&1
$env_path/mip_cmd EXECUTE:$work_path/$fname.dat,$output_path/$fname.out,100; >> $log/$date.log 2>&1
sleep 2
echo "`date +%H:%M:%S`| Moving the file *** $fname.dat *** to path |$moved_path|" >> $log/$date.log 2>&1
mv $work_path/$fname.dat $moved_path/$fname.dat.moved
cmd_exec=`cat $output_path/$fname.out |grep ":" |wc -l`
echo "`date +%H:%M:%S`| Total commands executed are `expr $cmd_exec / 2`" >> $log/$date.log 2>&1
echo "`date +%H:%M:%S`| Total Sucessfully executed commands are `cat $output_path/$fname.out |grep "C1:00000;" |wc -l`" >> $log/$date.log 2>&1
echo "--------------------------------------------------" >> $log/$date.log 2>&1
echo "#### SCRIPT WILL PROCESS THE NEXT FILE ###########" >> $log/$date.log 2>&1
echo "--------------------------------------------------" >> $log/$date.log 2>&1
echo "" >> $log/$date.log 2>&1
rm $def_path/.bla_processing
exit
done