Unix shell脚本错误

时间:2012-10-26 22:25:39

标签: linux shell unix

下面的脚本是通过sftp将文件推送到远程位置,我在代码下面写了很多问题。但是我仍然面临一些问题,请指导我解决问题。它不能用sh.it只是与ksh合作。

#test script 
#-------------------------------------------------------------------
#!/bin/sh
#------------------------------------------------------------------------
# sftp_file_uploads.sh 
#------------------------------------------------------------------------
export REMOTE_SERVER_PROD='192.168.0.1'
export REMOTE_SERVER_FAILOVER='192.168.0.2'
export SFTP_PORT='0001'
export SOURCE_FUNCTIONAL_ID='testusr'
export SOURCE_FILE_DIRECTORY='/var/temp/files/'
export SOURCE_ARCHIVE_DIRECTORY='/var/temp/files/archive'
export DATE_FORMAT=`date "+%Y%m%d"`
export LOG_DIRECTORY='/var/temp/logs'
export DESTINATION_FILE_DIRECTORY='/dest'
export LOG_FILE='$LOG_DIRECTORY/test_$DATE_FORMAT.log'
export SFTP_BATCH_FILE='/var/tmp/SFTP_BATCH_FILE'
#------------------------------------------------------------------------
# Find if the files are available at the source directory.
#------------------------------------------------------------------------
cd $SOURCE_FILE_DIRECTORY 
export FILE_TO_UPLOAD_TESTD=`ls -lrt TESTD$DATE_FORMAT.csv  | awk '/TESTD/{ f=$NF };END{ print f }'`
export FILE_TO_UPLOAD_TESTDF=`ls -lrt TESTDF$DATE_FORMAT.csv  | awk '/TESTDF/{ f=$NF };END{ print f }'`
#------------------------------------------------------------------------
# Try 2 times and Sleep for 5 mins if either of the files is not present
#------------------------------------------------------------------------
counter=0
flag_file_found_TESTD=0
flag_file_found_TESTDF=0
while [ $counter –lt 2 ]
do
#---------------------------
# Check TESTD file arrived
#---------------------------
if [ -z $FILE_TO_UPLOAD_TESTD ] then
      echo “No TESTD file to transfer. Sleeping for 5 mins” >> $LOG_FILE
 sleep 300
else
 echo “TESTD file found to transfer.” >> $LOG_FILE
 flag_file_found_TESTD=1
fi
#---------------------------
# Check TESTDF file arrived
#---------------------------
if [ -z $FILE_TO_UPLOAD_TESTDF ] then
      echo “No TESTDF file to transfer. Sleeping for 5 mins” >> $LOG_FILE
 sleep 300
else
 echo “TESTDF file found to transfer.” >> $LOG_FILE
flag_file_found_TESTDF =1
fi
if [[ flag_file_found_TESTD == 1 &&
      flag_file_found_TESTDF == 1 ]] then
 echo “Both files are found.” >> $LOG_FILE
 break
else

 echo “At least one of the files is not found. Retrying now.” >> $LOG_FILE
fi
counter=`expr $counter + 1`
done
if [[ flag_file_found_TESTD == 1 &&
      flag_file_found_TESTDF == 1 ]] then
 echo “Both files are found.”
 break
else
 if [ flag_file_found_TESTD == 0 ] then
    echo “test file is not found and two attempts completed. Cannot transfer the file for today.” >> $LOG_FILE
 fi

if [flag_file_found_TESTDF == 0 ] then
 echo “test1 file is not found and two attempts completed. Cannot transfer the file for today.” >> $LOG_FILE
 fi
fi
#------------------------------------------------------------------------
# Create sftp script
#------------------------------------------------------------------------
rm -f $SFTP_BATCH_FILE
echo "lcd $SOURCE_FILE_DIRECTORY " > $SFTP_BATCH_FILE
echo "cd $DESTINATION_FILE_DIRECTORY " >> $SFTP_BATCH_FILE
if [ -z $FILE_TO_UPLOAD_TESTD ] then
echo "put $FILE_TO_UPLOAD_TESTD " >> $SFTP_BATCH_FILE
fi
if [ -z $FILE_TO_UPLOAD_TESTDF ] then
echo "put $FILE_TO_UPLOAD_TESTDF " >> $SFTP_BATCH_FILE
fi
echo "bye" >> $SFTP_BATCH_FILE

#------------------------------------------------------------------------
# Do sftp
#------------------------------------------------------------------------
echo " Before SFTP " >> $LOG_FILE
if [[ -z $ FILE_TO_UPLOAD && -z $ FILE_TO_UPLOAD1 ]] then
 echo “No files to transfer” >> $LOG_FILE
       mv $LOG_FILE $LOG_DIRECTORY
 exit 1
else 
echo “Attempting to connect to  Remote Server $REMOTE_SERVER_PROD” >> $LOG_FILE
/usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID@$REMOTE_SERVER_PROD >> $LOG_FILE 2 >> $LOG_FILE
fi
result=$?
errorConnectToProd=0
if [ $result -eq 0 ]
then
  echo "SFTP completed successfully to Prod Remote Server" >> $LOG_FILE
else
    errorConnectToProd=1
   if [[ $result -eq 4 || $result -eq 5 ]]
  echo "FAILED to connect to Server. " >> $LOG_FILE

   else
 echo "FAILED to SFTP to  Remote Server. " >> $LOG_FILE
   fi
fi
if [ errorConnectToProd == 1 ] then
echo “Attempting to connect to FAILOVER Remote Server $REMOTE_SERVER_FAILOVER” >> $LOG_FILE
/usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID@$REMOTE_SERVER_FAILOVER >> $LOG_FILE 2 >> $LOG_FILE
fi
result=$?
if [ $result -eq 0 ]
then
  echo "SFTP completed successfully to Failover Remote Server" >> $LOG_FILE
else
   echo "FAILED to SFTP to Failover Remote Server. " >> $LOG_FILE
   mv $LOG_FILE $LOG_DIRECTORY
   exit 1
   fi
fi
cd $SOURCE_FILE_DIRECTORY
mv $FILE_TO_UPLOAD_TESTD $SOURCE_ARCHIVE_DIRECTORY
echo “Moved $FILE_TO_UPLOAD_TESTD to archive direcotry.” >> $LOG_FILE
mv $FILE_TO_UPLOAD_TESTDF $SOURCE_ARCHIVE_DIRECTORY
echo “Moved $FILE_TO_UPLOAD_TESTDF to archive direcotry.” >> $LOG_FILE

rm -f $SFTP_BATCH_FILE
echo “Deleted the SFTP Batch file.” >> $LOG_FILE
echo “Upload completed.” >> $LOG_FILE
mv $LOG_FILE $LOG_DIRECTORY
exit 0

获得以下错误:

test.ksh[41]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[55]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[56]: flag_file_found_TESTDF:  not found

test.ksh[65]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[41]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[55]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[56]: flag_file_found_TESTNDF:  not found

test.ksh[65]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[79]: [flag_file_found_TESTDF:  not found

rm: /var/tmp/SFTP_BATCH_FILE is a directory

test.ksh[89]: /var/tmp/SFTP_BATCH_FILE: cannot create

test.ksh[90]: /var/tmp/SFTP_BATCH_FILE: cannot create

test.ksh[97]: B:  not found

test.ksh[98]: B:  not found

test.ksh[99]: B:  not found

test.ksh[100]: B:  not found

test.ksh[101]: B:  not found

test.ksh[102]: B:  not found

test.ksh[106]: /var/tmp/SFTP_BATCH_FILE: cannot create

test.ksh[113]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create

test.ksh[114]: syntax error at line 114 : `FILE_TO_UPLOAD' unexpected

此致

1 个答案:

答案 0 :(得分:4)

这一行错了:

export LOG_FILE='$LOG_DIRECTORY/test_$DATE_FORMAT.log'

它应该使用双引号,以便扩展变量:

export LOG_FILE="$LOG_DIRECTORY/test_$DATE_FORMAT.log"

另一个错误:

if [flag_file_found_TESTDF == 0 ] then

[之后需要一个空格。 [是一个命令(它是test的同义词),并且所有命令都通过空格与它们的参数分开。

标记为“创建sftp脚本”的整个部分失败,因为/ var / tmp / SFTP_BATCH_FILE已经存在且是一个目录; rm -f不会删除目录,您需要使用rm -rf

if [[ flag_file_found_TESTD == 1 &&
  flag_file_found_TESTDF == 1 ]] then

在变量名称之前缺少$

if [[ -z $ FILE_TO_UPLOAD && -z $ FILE_TO_UPLOAD1 ]] then

摆脱$后的空格。

更新2:

在您的所有if语句中,您在;之前错过了then(或换行符)。

我不确定导致所有“B:未找到”错误的原因是什么。但是在你修复了所有其他错误后,它可能会消失或更容易找到。