Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:51:09+00:00 2026-06-13T09:51:09+00:00

Below script is to push file to remote location through sftp,i faced lot of

  • 0

Below script is to push file to remote location through sftp,i faced lot of issues to write below code.But still i am facing some issue,Please guid me to resolve the issues.It’s not working with sh.it is only working with 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

Getting below Errors:

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

Regards,

Chai

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:51:10+00:00Added an answer on June 13, 2026 at 9:51 am

    This line is wrong:

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

    It should use double quotes, so that the variables will be expanded:

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

    Another error:

    if [flag_file_found_TESTDF == 0 ] then
    

    needs a space after [. [ is a command (it’s a synonym for test), and all commands are separated from their arguments by spaces.

    The whole section labeled “Create sftp script” is failing because /var/tmp/SFTP_BATCH_FILE already exists and is a directory; rm -f won’t delete a directory, you need to use rm -rf.

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

    is missing the $ before the variable names.

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

    Get rid of the space after $.

    UPDATE 2:

    In all your if statements, you’re missing the ; (or newline) before then.

    I’m not sure what’s causing all the “B: not found” errors. But after you fix all the other errors, maybe it will go away or be easier to find.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried the below script but I am getting an error: dim cn, rs
How would I change the below script to edit the original file instead of
I've gone as far as I can with the below script, but I can't
The script below works fine if 3 and 3.2 are not links but i
I have implemented below function in a separate .js script file which I load
Code is working fine but i want to add my own location . for
Consider this code below: <a href="javascript:void(-1)" id="a1">a1</a> <a href="javascript:void(-1)" id="a2">a2</a> <script type="text/javascript"> var buttons
I was using the below code provided by Google for Google Analytics. <script type=text/javascript>
I want the below code to execute when I push the button I have
New Google Analytics code looks like one below: <script type=text/javascript> var _gaq = _gaq

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.