#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    32
    Rep Power
    7

    Unhappy FTP then Move file issue


    I have a script where I ftp some files and am trying to move them to a new directory. I've tried to move them while in the for loop just after FTP. I've also tried to move the whole batch after the for loop when the FTP connection has closed. In either case, the move command is executing before the all the files are FTP'd. I put in a sleep command to allow the FTP to finish, but that did not help.

    Here's my code. Any ideas?

    #! /bin/ksh

    HOST=$1
    USER=$2
    PASSWD=$3
    REQID=$4
    PROC_TYPE=$5
    BI_TOP=$6


    cd $BI_TOP/ftp/out

    exec 4>&1
    ftp -niv >&4 2>&4 |&
    print -p open $HOST
    print -p user $USER $PASSWD
    print -p ascii

    if
    [ $PROC_TYPE = "INVOICE" ] ; then
    for file in `ls $REQID*.idx`
    do
    new_name=`echo $file | sed "s/^$REQID//;"`
    print -p put $file $new_name

    #tried this. It only FTPs a couple files before all are moved
    #mv $file $BI_TOP/outdata/archive

    done

    # This works ok, but does not FTP the last file
    #mv $file $BI_TOP/outdata/archive
    else
    echo "What?"
    fi

    print -p bye

    #This only ftps a couple of files before all are moved

    #sleep 15s
    #if [ $PROC_TYPE = "INVOICE" ] ; then
    # mv $REQID*.idx $BI_TOP/outdata/archive
    #fi

    exit 0
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,484
    Rep Power
    1752
    A couple of ways you may be able to do it. Either in the 'ftp script' on a file-by-file basis by using the ftp ! command to execute a local command, print -p "!mv $file $BI_TOP/outdata/archive" or have two loops, one dealing with just the ftp commands, then one that moves the files once the ftp session has been closed.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    859
    Rep Power
    388

    Cool


    SimonJM is correct, the file(s) may be in the process of transferring while you suddenly move them.
    You must perform the script in two parts: 1) ftp and 2) move files.
    Also you may want to close the ftp session "print -p bye" after the ftp loop.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    32
    Rep Power
    7

    Smile


    Originally Posted by SimonJM
    A couple of ways you may be able to do it. Either in the 'ftp script' on a file-by-file basis by using the ftp ! command to execute a local command, print -p "!mv $file $BI_TOP/outdata/archive" or have two loops, one dealing with just the ftp commands, then one that moves the files once the ftp session has been closed.
    Thanks, I had tried putting teh bang in front of mv before and it failed. The piece I was missing was was "print -p". Thanks, it is working now
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    32
    Rep Power
    7
    I do have "print -p bye" after the ftp loop
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,484
    Rep Power
    1752
    Originally Posted by cbashyb
    Thanks, I had tried putting teh bang in front of mv before and it failed. The piece I was missing was was "print -p". Thanks, it is working now
    Good to hear it's working.

    Whenever I have done things like this I have used the loop to create a temporary ftp script and piped that into an ftp -ni command. That way I could always go back and see what was being tried.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo