ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
i'm having a problem with a script i am trying to write, i am writting a perl script that once complete will ran in cron.weekly, This script is to take the desired folder that tell it too (which are probably going to be in an array of some kind) and then using this array in conjunction with Tar to compress the contents of all these folders into one file,
the purpose of this is to backup my log files on a weekly basis and compress them into a file, I am new at Perl, and i need help, I created an array, and i'm looping through the array and in the loop i am using tar but it's not working...
urgh, anyone have any pointers ;P, or any link that would proove useful for me, t'ill then i'll be hacking away at it,
thanks ahead.... here's a sample, it's rough, but i have an excuse, beginner. lol. thanks.
by doing foreach, it does display what i'm trying to tar but when i check the file.tar there's only the content of the first folder in the array.
hmmmm......BTW ignore the while loop earlier testing... :P
Each time it creates the tar file anew.
the order of keys is not dependent of the order they went in.
so it just a coincidence that the first folder in comes out last
so it appears to be the only one working
Also, you are using a sledgehammer to crack a small nut.
why using a hash?
Hi Matt,
As someone else posted a solution that works, I'll just note a few problems:
1. As a Perl beginner, the most important thing you can do for yourself is begin every script with "-w" and "use strict;", i.e.:
Code:
#! /usr/bin/perl -w
use strict;
This turns on warnings and strict mode - making it very hard for you to do silly beginner mistakes without having perl shout at you...
2. No indentation - I mean, come on, this is elementry in any programming language, not just Perl.
3. Using a hash where you really want an array - a little reading of Perl's documentation and some light help from a friend with even basic Perl knowledge would have helped you here. Always make sure you use the right data structure for the problem you're trying to solve...
4. Since you're tar-ing entire directories, why the "*" at the end? Is it really needed?
5. No "\n" with you print statements - "print" doesn't add a newline by itself.
6. Here's the modified script, using the suggested solution from the first reply (as he noted, simply calling tar more than once will simply overwrite the test.tar file):
Code:
#! /usr/bin/perl -w
use strict;
my @bdirs = (
"/home/mbrunet/apache",
"/home/mbrunet/apache-ssl",
"/home/mbrunet/ntop"
);
print "About to tar the following folders: @bdirs\n";
system("tar -cvvf test.tar @bdirs");
print("===============================\n");
}
Note that both the first print statement and the system command use a feature of double-quoted strings in Perl, where embedded arrays are interpolated into a string by joining the the elements with a whitespace (by default). Once you know about this and use this once or twice you'll remember, but it is a bit hard to find in the documentation (if you're curious, this is documented in "perldoc perldata").
As a beginner, if you can, I highly suggest getting the book "Learning Perl", from O'Reilly. It is the best book, hands-down, to learn Perl from.
# perl test
Unmatched right curly bracket at test line 12, at end of line
syntax error at test line 12, near "}"
Execution of test aborted due to compilation errors.
#
I hope you don't mind that I modified it abit to use date for the tar file name.
Code:
#! /usr/bin/perl -w
use strict;
my $filename = `date +%m%d%y`;
my @dirs = (
"/mnt/win/tech/aclinux",
"/home/cdbuild",
"/home/distros"
);
print "About to tar the following folders: @dirs\n";
system("tar -czvf '$filename'.tar @dirs");
print("===============================\n");
Originally posted by homey MetaPhase,
Now it's time to chew on you.
# perl test
Unmatched right curly bracket at test line 12, at end of line
syntax error at test line 12, near "}"
Execution of test aborted due to compilation errors.
#
Oops - must have snuck in with the copy-paste
Matt, for reference, the offending brace is at the very last line - it isn't supposed to be there - just delete it and the code will work fine.
Quote:
Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi.
why does this just try to tar the same folders over and over until the while loop complets, i am incrementing "$i", i even print-out the when the pointer is 'pointing' and it does incremement the pointer, but then it always tries to TAR the same directories, i do not know why, and i'm stumped.
awesome, my sciprt is complete, i'm thinking of putting my script into cron.weekly, i am curious, do i just put my perl script into cron.weekly?...
hmm, can i just put my script into cron.weekly and it will run it once a week, or is there something else i have to do to get my perl script to run in cron.weekly?
Originally posted by matt1982 print "@bfiles[$i]"; #4 testing
Hi Matt,
Please note that in Perl, an array is a collection of scalars, e.g.:
Code:
my @array = (1, "two", 3, "four");
Now why am I saying something that you obviously already know? Because I want to emphazise the word scalars here - each member of the array is a scalar, which means that when you access a single member of an array, you must prefix the call with a $ sign, which is used to prefix scalars in Perl -- don't use the @ symbol, which is used to denote a collection of scalars.
So the correct way to write the above code is:
Code:
print "$bfiles[$i]"; #4 testing
Now in this specific case, it didn't matter that you used an @ symbol, the code worked correctly (for reasons I won't go into). But in other situations, this way of writing could generate errors, so I strongly advise you to learn the correct way of accessing members of an array, and use that. "-w" and "use strict" will be your friends here - use them!
Originally posted by matt1982 awesome, my sciprt is complete, i'm thinking of putting my script into cron.weekly, i am curious, do i just put my perl script into cron.weekly?...
hmm, can i just put my script into cron.weekly and it will run it once a week, or is there something else i have to do to get my perl script to run in cron.weekly?
hope i didn't confuse anyone... :P
thanks ahead.
Urmm, "chmod +x script.pl"?
I don't know what your level of Unix is, so I'm not sure I understood you - please forgive if I was being too simplistic. If not the above, what did you mean?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.