Ftp script in bash

I have the following script that pops files to a 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 get 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.
+3
source share
2 answers

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 string or mixed variables to reduce the likelihood of name collisions with shell variables.

Are all these directories really located directly in the root directory?

+5
source

Ftp to the remote site and execute ftp commands manually. When an error occurs, look around to find out what is the reason. (Use the "help" if you do not know the ftp command line.)

, /data . - , , ftp-?

0

Source: https://habr.com/ru/post/1784494/


All Articles