Search code examples
bashftpftp-clientgrads

Reading Through A List of Files, then Sending those Files via FTP


I am making weather model charts with the Grads scripting language, and I am using a bash script so I can use a while loop to download model data (in grib2 format) and call the grads scripts for each frame on the model run. Right now, I have a loop that runs through all the scripts for a given forecast hour and uploads the image output via FTP. After this for loop completes, the grib2 data for the next hour is downloaded, and the loop runs again.

for ((i=0;i<${#SCRIPTS[@]};i++)); do
    #define filename
    FILENAME="${FILENAMES[i]}${FORECASTHOUR}hrfcst.png" 
    #run grads script
    /home/mint/opengrads/Contents/opengrads -lbc "run /home/mint/opengrads/Contents/plotscripts/${SCRIPTS[i]} $CTLFILE $INIT_STRINGDATE $INIT_INTDATE $INITHOUR $FILENAME $h $MODEL $MODELFORTITLE 500"
    #run ftp script
    #sh /home/mint/opengrads/Contents/bashscripts/ftpsample.sh $INIT_INTDATE $INITHOUR $FILENAME $MODEL
done

This is inelegant because I open and close an FTP session each time I send a single image. I would much rather write the names of the filenames for a given forecast hour to a .txt file (ex: have a "echo ${FILENAME} >> FILEOFFILENAMES.txt" in the loop) and have my FTP script read and send all those files in a single session. Is this possible?


Solution

  • It's possible. You can add this to your shell script to generate the ftp script and then have it run after you've generated the files:

    echo open $HOST > ftp.txt
    echo user $USER $PASS >> ftp.txt
    find . -type f -name '*hrfcst.png' -printf "put destination/%f %f\n" >> ftp.txt
    echo bye >> ftp.txt
    
    ftp < ftp.txt
    

    The above code will generate file ftp.txt with commands and pass that to ftp. The generated ftp.txt will look like:

    open host
    user user pass
    put destination/forecast1.hrfcst.png forecast1.hrfcst.png
    put destination/forecast2.hrfcst.png forecast2.hrfcst.png
    put destination/forecast3.hrfcst.png forecast3.hrfcst.png
    ...
    bye