Inserire FTP in uno script ksh

#! /usr/bin/ksh

HOST=remote.host.name
USER=whoever
PASSWD=whatever

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p cd directory
print -p binary
print -p put tar.gz
print -p bye

wait
exit 0

That is the file descriptor manipulation. Recall that fd 0 is standard-in, fd 1 is standard-out, and fd 2 is standard-error. The line “exec 4&>1” opens fd 4 and assigns it to whatever fd 1 was assigned to. As you will see, I am sorta “saving a copy of fd 1 in fd 4”. The line “ftp -nv >&4 2>&1 |&” is a little harder. The “|&” turns the process into a co-process that allows subsequent “print -p” statements to send lines to the co-process’ standard-in and “read -p” to read from its standard-out. So ksh forks a copy of itself and fiddles with the fd’s 0 and 1 until this it set-up. But it leaves the rest of the fd’s alone. Then it encounters “>&4” which causes it to set the ftp process’ standard out to whatever 4 is. Well since 4 is a copy of 1 before the co-process, we are back to writing to the original shell’s standard out. Lastly, the 2>&4 does the same thing for standard error. I could’ve used “2>&1” at this point for the same effect. This is hard to explain, but I hope this helps.

Contrassegnato da tag , , ,

3 thoughts on “Inserire FTP in uno script ksh

  1. Akexjan Carraturo ha detto:

    Questa cosa meriterebbe una bella guida su freedoc, non trovi? Direi che è proprio interessante.

  2. Alessandro ha detto:

    Direi che oggi anche no, ho fatto brutal copy&paste. magari giovedí 😛

Scrivi una risposta a Alessandro Cancella risposta