1832978 Members
2774 Online
110048 Solutions
New Discussion

variable in ftp

 
Charles McCary
Valued Contributor

variable in ftp

Group,

Here's the scenario

SP=U020505

ftp -v -i HOST1
hash on
glob on
bin
mget ${SP}
bye

doesn't work.

mget U020505

does work.


Any ideas?

tx,
c
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: variable in ftp

Hi:

In the past, I used to do this stuff in the shell but now I NEVER do because I have found a much cleaner way to do it that makes error trapping and variable substitution duck soup. Do this stuff in Perl using the Net::FTP module which is available from http://www.cpan.org .

Here's how simple it can be:

#!/usr/bin/perl -w

use Net::FTP;
use strict;

my $ftp = Net::FTP->new("remotehost",Debug => 0);
$ftp->login("cstephen","top_secret");
$ftp->cwd("/tmp");
$ftp->get("myfile");
my $stat = $ftp->status;
my $full_stat = $ftp->code;
# $stat contains the first digit; usually all
# that you need to do is test if it is equal
# to 2. $full_stat contains the full 3-digit
# value but is seldom needed
printf("Status: %d Full Status: %d\n",$stat,$full_stat);
# Sample Test
if ($stat == 2)
{
print "Get was good\n";
}
else
{
print "Get was bad\n";
}
$ftp->quit;

I think if you start using this module you will never go back to shell scripting FTP. Moreover, these same scripts will run in the NT world. You can download a free version of perl for windows at http://www.activeperl.com.
Note that the answer is now the same on UNIX and NT and on the same subnet or across the net.


Notice that this method easily handles the error checking. If you like, you can use the shell for most of your script and simply use a bit a perl for the actual FTP transfers. In that case add the statement exit($stat) to the perl script and then your shell script does have a valid status indication.

Regards, Clay
If it ain't broke, I can fix that.
Mark Greene_1
Honored Contributor

Re: variable in ftp

try this script:

#!/bin/ksh
SP=U020505
ftp -v -i HOST1<hash on
glob on
bin
mget ${SP}
bye
EOF
exit

You will need to add the user/password info and perhaps a "cd" to get to the appropriate directory. However, if you were trying to do this via a .netrc file it won't work. You can only use explicit file names (and the usual wildcards) in the file, but no variable translations will get performed.

HTH
mark
the future will be a lot like now, only later
harry d brown jr
Honored Contributor

Re: variable in ftp

Charles,

without a doubt use Clay's example using perl - it rules!

live free or die
harry
Live Free or Die
Ricardo Bassoi
Regular Advisor

Re: variable in ftp

You can also create a simple function:

HOST=HOST1
SP=U020505
PATH_VAR=/tmp # Choose the dir where is $SP

function FTPCSV
{
`ftp -n -i $HOST< quote USER username
quote PASS password
bin
cd $PATH_VAR
mget ${SP} # or just: mget $SP
quit
END`
}

FTPCSV # Call FTP


Regards,

Bassoi
If you never try, never will work
If you never try, never will work
Charles McCary
Valued Contributor

Re: variable in ftp

Guys,

thanks - I got it working...

C