Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put condition in FTP 2

Status
Not open for further replies.

hok1man

Technical User
Feb 16, 2008
102
Hi guys,

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

print -p open $HOST
print -p user $USER $PASSWD

if [-f $1];then
    print -p put $1
fi

print -p bye

what the "exec 4" exactly for?
if it wants to redirect to output file, where is it going to?

and I was just wondering how to put condition like above using normal FTP pattern like :
Code:
ftp -n $HOST <<FTP_Block
user $USER $PASSWD

[red]how to put if condition here ...[/red]

bye
FTP_Block

Thanks Guys,
 
exec 4>&1 duplicates the file descriptor 1 so that you can refer to it as 4 as well. 1 is standard output.

This means that if any subsequent command redirects standard output, you can still write to the original standard output using file descriptor 4.

By example:

Code:
$ sh testexec
$ cat testexec
# redirect standard out to /tmp/stdout
exec 1> /tmp/stdout
# duplicate file descriptor 1 to 4
exec 4>&1
echo "this should go to /tmp/stdout"
echo "this should go to /tmp/logfile" > /tmp/logfile
# start a subshell whose output is appended to /tmp/logfile by default
(
        echo "this also goes to /tmp/logfile"
        echo "however this line will go to /tmp/stdout" >&4
) >> /tmp/logfile
# close file descriptor 4
exec 4>&-
$ cat logfile
this should go to /tmp/logfile
this also goes to /tmp/logfile
$ cat stdout
this should go to /tmp/stdout
however this line will go to /tmp/stdout
$

In the first example you posted the ftp process is being started as a co-process, so the exec 4>&1 is probably there to make sure that you can still see its output, even if you choose to redirect stdout while it is running. I haven't used co-processes much, so can't say for sure.

You should be able to use the co-process method to insert your logic. Note that your example is missing a space between [ and -f.

Annihilannic.
 
Thanks Anni,

what about the other question?
to put condition if ftp logic the second one?

Cheers,
 
I answered that too, see the last line of my previous post. In short, I think you are most of the way there with the first code example you posted.

Annihilannic.
 
Replace this:
if [-f $1];then
with this:
if [ -f $1 ]; then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
ftp -n $HOST <<FTP_Block
user $USER $PASSWD

if [ -f $1 ]; then 
put $1
fi

bye
FTP_Block

This doesn't work guys...
 
This doesn't work guys...

No surprise, since you changed it in the wrong piece of code. As I said, replace it in the first code example you posted. If you're going to ask questions here, please take the time to read the responses carefully.

Feherke's suggestion is good, this should work too:

Code:
#!/bin/ksh
HOST=hostname
USER=username
PASSWD=password
exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD

if [ -f $1 ];then
        print -p put $1
fi

print -p bye
# consume stdout from co-process, otherwise shell pauses
# for some reason
read -p nothing


Annihilannic.
 
Thanks Feherke,

Anni, what Iam asking is using
ftp -n $HOST <<FTP_Block

not using
ftp -nv >&4 2>&4 |&

I already got it worked using |&
Thanks anyway for ur help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top