LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-13-2005, 07:47 AM   #1
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Rep: Reputation: 15
Angry Perl script using tar


hey y'all,

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.

#! /usr/bin/perl
#! /usr/bin/tar

%bdirs = ("/home/mbrunet/apache/*",
"/home/mbrunet/apache-ssl/*",
"/home/mbrunet/ntop/*");

foreach $holder (keys(%bdirs)){
print ($holder);
system("tar -cvvf test.tar $holder");
print("===============================");
}

#while ($count <= @bdirs[$count]) {
#print (@bdirs);
#$count++;
#$var = @bdirs[$count];
#print ($var);
#$count++;
#system("tar -cvvf /home/mbrunet/apache/test.tar $var");
#}

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

Last edited by matt1982; 01-13-2005 at 07:49 AM.
 
Old 01-13-2005, 08:33 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
because, you are calling
tar cvf 3 times.

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?

You can do it in one go,
this is all you need
Code:
#!/usr/bin/perl -w

@bdirs = qw(
    /export/home/billym/dot_files
    /export/home/billym/temp
    /export/home/billym/sql
    );

    print "@bdirs";
    system("tar -cvvf test.tar @bdirs");
~

Have a look at
man logrotate

it may do what you need.
 
Old 01-13-2005, 09:29 AM   #3
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Original Poster
Rep: Reputation: 15
hey thanks alot, that works alot better than mine, and yeah you're right it is a coincidence, seems to work from the bottom up...

thanks for your help, i'll be back shortly if i need anymore :P
 
Old 01-13-2005, 09:31 AM   #4
MetaPhase
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Rep: Reputation: 0
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.
 
Old 01-13-2005, 09:54 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
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.
#
 
Old 01-13-2005, 11:00 AM   #6
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Original Poster
Rep: Reputation: 15
looks to me like that "}" doesn't have an "{" to reference to, double check your {} and make sure that every { has an }....

i'm pretty sure that you'll find that you're missing either { or that you have an } to many.
 
Old 01-13-2005, 12:10 PM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
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");
 
Old 01-13-2005, 01:10 PM   #8
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Original Poster
Rep: Reputation: 15
lol of course not, if i minded i wouldn't of posted the code,

:P
 
Old 01-13-2005, 01:56 PM   #9
MetaPhase
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Rep: Reputation: 0
Quote:
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.

Larry Wall
 
Old 01-17-2005, 08:57 AM   #10
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Original Poster
Rep: Reputation: 15
Question urgh... arrays

urgh, i am wondering why this code:

#!/usr/bin/perl

$files = "log,web";
@bfiles = split(",",$files);

$dirs = "/home/mbrunet/apache/* /home/mbrunet/apache-ssl/* /home/mbrunet/ntop/*, /home/mbrunet/web/*";
@bdirs = split(",",$dirs);

$size = @bfiles;
print "size is: $size\n";
$i = 0;
while($i < $size) {
print "@bfiles[$i]"; #4 testing
print "@bdirs[$i]"; #4 testing
print "i = $i ===================\n"; #4 testing

system("tar -vczf '@bfiles[i]'.tar.gz @bdirs[i]");
$i++;
}

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.

anyone gotta any thoughts?... thanks ahead.

Last edited by matt1982; 01-17-2005 at 08:58 AM.
 
Old 01-17-2005, 09:11 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
matt, I haven't tested but it looks like you are doing the same thing
you need to call tar once using the cvf switches.

you haven't really had a look at the previous suggestions have you?
;-)
 
Old 01-17-2005, 09:28 AM   #12
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Original Poster
Rep: Reputation: 15
Thumbs up

:O oh, i see, thanks... nevermind,,, i'll blame this one on monday morning... :P
 
Old 01-17-2005, 10:31 AM   #13
matt1982
Member
 
Registered: Nov 2004
Location: ontario, canada
Distribution: debian
Posts: 52

Original Poster
Rep: Reputation: 15
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.
 
Old 01-17-2005, 11:08 AM   #14
MetaPhase
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Rep: Reputation: 0
Re: urgh... arrays

Quote:
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!
 
Old 01-17-2005, 11:16 AM   #15
MetaPhase
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Rep: Reputation: 0
Quote:
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i decompress this tar.tar file? hmmm sounds new.. tar.tar.. help ;) kublador Linux - Software 10 02-23-2008 05:40 AM
Strange tar usage problem through Perl Darthlord Programming 0 09-02-2004 09:46 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 06:39 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration