ftp script in bash

13,115

Some improvements:

#!/usr/bin/bash
HOST1='a.b.c.d'
USER1='load'
PASSWD1='load'
read Y m d H M <<<$(date "+%Y %m %d %H %M")    # only one call to date
DATE='$Y$m$d$H$M'
DATE2='$Y$m$d$H'
DATE3='$Y$m$d'
FTPLOGFILE=/logs/Done.$DATE2.log
D_FOLDER='/dir/load01/input'

PUTFILE='file*un'
for file in $PUTFILE    # no need for ls
do
  echo "${file} transfered at $DATE"
done >> /logs/$DATE3.log    # output can be done all at once at the end of the loop.

ftp -n -v $HOST1 <<SCRIPT >> ${FTPLOGFILE} 2>&1
quote USER $USER1
quote PASS $PASSWD1
cd $D_FOLDER
ascii
prompt off
mput /data/file*un 
quit
SCRIPT

mv *un test/

for f in test/*un    # no need for ls and awk
do
  mv "$f" "${f/%.un/.processed}"
done

rm *unl

I recommend using lower case or mixed case variables to reduce the chance of name collisions with shell variables.

Are all those directories really directly off the root directory?

Share:
13,115
bernie
Author by

bernie

Updated on June 29, 2022

Comments

  • bernie
    bernie about 2 years

    I have the following script that pushes files to remote location:

    #!/usr/bin/bash
    HOST1='a.b.c.d'
    USER1='load'
    PASSWD1='load'
    DATE=`date +%Y%m%d%H%M`
    DATE2=`date +%Y%m%d%H`
    DATE3=`date +%Y%m%d`
    FTPLOGFILE=/logs/Done.$DATE2.log
    D_FOLDER='/dir/load01/input'
    
    PUTFILE='file*un'
    ls $PUTFILE | while read file
    do
      echo "${file} transfered at $DATE" >> /logs/$DATE3.log
    done
    
    ftp -n -v $HOST1 <<SCRIPT >> ${FTPLOGFILE} 2>&1
    quote USER $USER1
    quote PASS $PASSWD1
    cd $D_FOLDER
    ascii
    prompt off
    mput /data/file*un 
    quit
    SCRIPT
    
    mv *un test/
    
    ls test/*un | awk '{print("mv "$1" "$1)}' | sed 's/\.un/\.processed/2' |sh
    rm *unl
    

    I am getting this error output:

    200 PORT command successful.
    553 /data/file1.un: A file or directory in the path name does not exist.
    200 PORT command successful.