-1

下面的脚本是通过 sftp 将文件推送到远程位置,我在编写下面的代码时遇到了很多问题。但我仍然面临一些问题,请指导我解决问题。它不适用于 sh。它仅适用于 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

问候,

4

1 回答 1

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陈述中,您都缺少;before (或换行符)then

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

于 2012-10-26T22:38:02.283 回答