- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: need a perl script to read from text file and ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-25-2004 09:03 AM
тАО02-25-2004 09:03 AM
Re: need a perl script to read from text file and rename files
thanks, it works very well.
sorry for asking again, but I have
still only one problem with:
-f $1 or next;
-f $2 and next;
in most cases the second file will be transfered first and I don't know how to change it.
if I try with
-f $1 or next;
-f $2 or next;
then wont work and I get in the log:
"files are not completed."
for me are very importannt 2 things:
- the second file cannot be transfered
before the first one
- first and second cannot be transfered twice
the code now is:
@ARGV = ("info.txt");
my %ren;
while (<>) { # for every line in info.txt
m/^\s*(\S+)\s+(\S+)\s*$/ or next; # That matches like "XXXXXXXX XXXXXXXX"
-f $1 or next; # skip if file to rename does not exist
-f $2 and next; # skip if file to rename does not exist
$ren{$1} = $2;
}
unless (2 == scalar keys %ren) {
die "files are NOT complete !\n";
# extend to your feeling
}
foreach my $f1 (keys %ren) {
my $f2 = $ren{$f1};
move ($f1, $f2); # Rename the file
my $server = "192.168.0.1";
my $user = "anonymous";
my $passwd = "";
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); # Default timeout od 120 (2minutes) is too short
$ftp or die "$server: cannot connect: $@";
# If you don't use ~/.netrc
$ftp->login ($user,$passwd) or
die "$_: Could not login: " . $ftp->message;
# Put file 2 (not 1) to the ftp server
# VPN connection might go down, so retry twice
foreach my $try (0 .. 2) {
$ftp->put ($f2) and last; # Success
if ($try == 2) {
# Put file 2 (not 1) to the ftp server
# VPN might go down, so retry twice
foreach my $try (0 .. 2) {
$ftp->put ($f2) and last; # Success
if ($try == 2) {
open my $mail, "| /usr/sbin/sendmail -t" or die "Cannot send mail: $!\n";
print $mail "To: $mail\n",
"From: $linux\n",
"Subject: ftp transfer was NOT successfull\n",
"\n------------------------------------------------------------------\n",
"ftp transfer was NOT successfully\n\n";
"Time: ". scalar localtime ."\n",
"------------------------------------------------------------------\n.\n";
close $mail;
die "$server: cannot put $f2: " . $ftp->message;
}
print STDERR "Trasfer of $f2 failed: ",$ftp->message, ", retry in 3 minutes\n";
sleep (3 * 60);
}
$ftp->quit;
# And wait 5 minutes
sleep (5 * 60);
}
I tried to fix self, but I can't.
I know, I asked to many times ,but you are only one person, who can help me.
kind regards
chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-26-2004 01:24 AM
тАО02-26-2004 01:24 AM
Re: need a perl script to read from text file and rename files
Here is what to change to preserve the order:
--8<---
@ARGV = ("info.txt");
my @ren;
while (<>) { # for every line in info.txt
m/^\s*(\S+)\s+(\S+)\s*$/ or next; # That matches like "XXXXXXXX XXXXXXXX"
-f $1 or next; # skip if file to rename does not exist
-f $2 and next; # skip if file to rename does not exist
push @ren, [ $1, $2 ];
}
unless (@ren == 2) {
die "files are NOT complete !\n";
# extend to your feeling
}
foreach my $f (@ren) {
my ($f1, $f2) = @$f;
move ($f1, $f2); # Rename the file
-->8---
Of course there are many more ways to do this, but this is by far the easiest
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-26-2004 02:16 AM
тАО02-26-2004 02:16 AM
Re: need a perl script to read from text file and rename files
and help
sorry to disturb again, but the script
stop now at:
foreach my $f2 (values %ren) {
move $f2, "/home/test/out/$f2";
move "info.txt", "/home/test/out/info.txt";
move "/home/test/logs/errorlog.txt", "/home/test/out/errorlog.txt";
with the message:
Global symbol "%ren" requires explicit package name at ftp3.cgi line 115.
Execution of ftp3.cgi aborted due to compilation errors.
Errors:
I tried to change to:
foreach my $f2 (values @ren) {
but it doesn't help:
"Type of arg 1 to values must be hash (not private array)"
greetings
chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-26-2004 02:20 AM
тАО02-26-2004 02:20 AM
Re: need a perl script to read from text file and rename files
--8<---
foreach my $f2 (map { $_->[1] } @ren) {
move $f2, "/home/test/out/$f2";
-->8---
Time to learn Perl? Buy a book?
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-26-2004 02:30 AM
тАО02-26-2004 02:30 AM
Re: need a perl script to read from text file and rename files
I will do
thanks
chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-25-2004 09:27 AM
тАО03-25-2004 09:27 AM
Re: need a perl script to read from text file and rename files
sorry to ask again, but I have a problem and I don't know how to solve it.
I try to send a mail with log file contents
and I've put on the end of the script following code:
................................................
................................................
................................................
die "$server: cannot put $ft2: " . $ftp->message;
}
print STDERR "Trasfer of $ft2 failed: ",$ftp->message, ", retry in 15 minutes\n";
sleep (15 * 60);
}
$ftp->quit;
# wait 5 minutes before processing next file.
sleep (5 * 60);
}
# create backup subfolder when transfer completed
my @dt = localtime;
my $subfolder_name = ((((1900 + $dt[5]) * 100 + 1 + $dt[4]) * 100 + $dt[3]) * 100 + $dt[2]) * 100 + $dt[1];
mkdir "/home/test/out/$subfolder_name" or die "$subfolder_name: $!";
# backup all files
foreach my $ft2 (map { $_->[1] } @ren) {
move $ft2, "/home/test/out/$subfolder_name/$ft2";
move "info", "/home/test/out/$subfolder_name/info";
move "/home/test/logs/errorlog.txt", "/home/test/out/$subfolder_name/errorlog.txt";
}
# read log file
my $content;
open(FILE, "/home/test/out/$subfolder_name/errorlog.txt") || die "Cant open file. Reason: $!"; # (-:
while(
$content .=$_; # get all of the file into content including past new lines
}
close(FILE);
# send mail with log contents, when transfer completed
open(MAIL, "|/usr/sbin/sendmail -t") || die "Cant send mail. Reason: $!";
print MAIL "to:$mail\n";
print MAIL "from:$linux\n";
print MAIL "subject:transfer of 2 files was successfully !\n";
print MAIL "log file: \n";
print MAIL "$content \n\n";
print MAIL "Time: ", scalar localtime, "\n";
close(MAIL);
exit;
mail with the log contents will be sent, but the problem is:
after the second file was sent and all files are saved, it waits half hour and only after this time sends a confirmation mail.
how to change this script, to send a mail immediately after backup ?
greetings
chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 09:25 AM
тАО04-20-2004 09:25 AM
Re: need a perl script to read from text file and rename files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 06:05 PM
тАО04-20-2004 06:05 PM
Re: need a perl script to read from text file and rename files
close STDERR;
just before you are done will all successes? What happens is that you write the log to STDERR, and later open the log from the file that STDERR is writing to.
close STDERR;
will sync the output, so the log reader can continue.
This is however NOT a good solution, because STDERR is also used for die () and warn (). And you might need them on other failures.
The solution IS to explicitely open a stream to LOG, and write all errors/warnings to LOG instead of to STDERR, and close LOG before you open it for reading
Enjoy, Have FUN! H.Merijn [ who hopes that this analysis is correct ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 09:24 PM
тАО04-20-2004 09:24 PM
Re: need a perl script to read from text file and rename files
I don't know exactly what or how do you mean.
I tried to put
close STDERR;
before
# create backup subfolder when transfer completed
my @dt = localtime;
but the problem still exists.
greetings
chris
- « Previous
-
- 1
- 2
- Next »