LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   creating loop within ftp session for fetching data (https://www.linuxquestions.org/questions/linux-newbie-8/creating-loop-within-ftp-session-for-fetching-data-4175546847/)

deepblue_86 06-30-2015 03:33 PM

creating loop within ftp session for fetching data
 
Code:

echo "which stations you want to download (example input, brmu ankr) ? : "
read stations
echo "which years interval (example input, 2013 2015 or 2010 2010)? :"
read year
echo "which days of year (example 001 010 or 025 025)? :"
read doy
stringarray_1=($stations)
stringarray_2=($year)
stringarray_3=($doy)
echo "connecting the ftp to fetch data"
echo
HOST='x.x.edu'
USER='anonymous'
PASSWD='12345'
ftp -n $HOST  <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary

END_SCRIPT

I need to create a nested loop inside ftp to allow me fetch data, for example, inputs are;

stations=ankr brmu

year=2013 2014

doy=001 002
w.r.t. these inputs I need to create loop like;

Code:

cd 2013;cd 001;mget *;cd ..

cd 002;mget *;cd ..;cd ..

cd 2014; cd 001;mget *;cd ..

cd 002;mget *

How can I create such a loop inside ftp?

schneidz 06-30-2015 03:46 PM

seems like scp/sftp/sshfs would be safer and easier ?

deepblue_86 06-30-2015 04:03 PM

Is there any example related this issue in scp/sftp/sshfs?

schneidz 06-30-2015 04:06 PM

Code:

scp user1@host1:/whatever/floats/your/boat user2@host2:/whatever/grinds/your/gears

michaelk 06-30-2015 07:18 PM

Assuming you have can login to the server via ssh then schneidz method would be easier since the command to get the files would be

dir1=/../$year/$day (etc...)
dir2=...
scp user@host:/$dir1/ . (The . means to transfer the files to the current working directory.)
scp user@host:/$dir2/ .

Additional code would be required to parse for year intervals and knowing the absolute path to the ftp directories. However, since you are logging in as anonymous then maybe not. Once you are logged in via ftp you are limited to using its command set. Just brainstorming one way would be to create your loop outside the herdoc (i.e. everything enclosed by <<end_script end_script) and run it over the year interval or whatever. Create a variable for each directory as above and then

Code:

foreach year do (just psuedo code)
dir=/../$year/$doy/..
HOST='x.x.edu'
USER='anonymous'
PASSWD='12345'
ftp -n $HOST  <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary
cd $dir
mget *
quit
END_SCRIPT
end



All times are GMT -5. The time now is 08:13 PM.