scalar variable in system command function

Discussion in 'Perl Misc' started by Rocky Allen, Sep 8, 2005.

  1. Rocky Allen

    Rocky Allen Guest

    I have an odd problem with the $file variable listed in the loop below
    the syntax of unix split command is as follows:
    Usage: split [OPTION] [INPUT [PREFIX]] and split will default to "x" as a
    prefix

    here is the output of this script (i ^c it)
    found file rallen.tar.gz
    error splitting file rallen.tar.gz
    :2 at ./perl-ATSDailyback line 49.

    it remembers the $file variable before and after the system command runs,
    but it doesnt add "$file." as a prefix

    any thoughts?
    thanks in advance


    #!/usr/bin/perl

    use strict;
    use warnings;
    use Sys::Hostname;
    use POSIX qw/strftime/;

    my $error ;
    my @errors;
    my @filestobackup;
    # Variables. This section will need to be modified for each Cooperative.

    my $host = hostname;
    my $jobstart = localtime();
    my $fileformat = strftime('contents-%Y-%m-%d_%H:%M:%S', localtime);
    my $contentsfile = "/etc/backup/$fileformat";
    my $errorfile = '/tmp/cpioerrors';
    my @dirs = ('/working');
    my $finderrors = '/tmp/finderrors';
    my @largefiles;
    #my @tapestat = `mt -f /dev/st0 status`;
    #if (scalar(@tapestat) == 0) {
    # print "No tape in drive or no tape device on server\n";
    # exit 0;
    #}



    foreach my $backupdir (@dirs) {
    chdir($backupdir);
    @largefiles = `find . -follow -size +2147483648c -print 2>/tmp/largefilerr`;
    foreach my $file (@largefiles) {
    $file =~ s/\.\///;
    print "found file $file";
    system("split -d --bytes=2000000000 $file $file.") == 0 or die "error splitting file $file:$?";
    }
    @filestobackup = `find $backupdir -follow -print 2> $finderrors`;
    }
     
    Rocky Allen, Sep 8, 2005
    #1
    1. Advertising

  2. Rocky Allen

    Paul Lalli Guest

    Rocky Allen wrote:
    > I have an odd problem with the $file variable listed in the loop below
    > the syntax of unix split command is as follows:
    > Usage: split [OPTION] [INPUT [PREFIX]] and split will default to "x" as a
    > prefix
    >
    > here is the output of this script (i ^c it)
    > found file rallen.tar.gz
    > error splitting file rallen.tar.gz
    > :2 at ./perl-ATSDailyback line 49.


    Compare this output...

    print "found file $file";
    > system("split -d --bytes=2000000000 $file $file.") == 0 or die "error splitting file $file:$?";


    .... with this code.

    Do you notice that the output has a newline after each file name?
    Do you notice that the code does not?

    Somewhere along the way, you neglected to remove the \n from each
    filename. You did not find the file "rallen.tar.gz". You found the
    file "rallen.tar.gz\n".

    perldoc -f chomp

    Paul Lalli

    > }
    > @filestobackup = `find $backupdir -follow -print 2> $finderrors`;
    > }
     
    Paul Lalli, Sep 8, 2005
    #2
    1. Advertising

  3. Rocky Allen

    Rocky Allen Guest

    On Thu, 08 Sep 2005 09:41:27 -0700, Paul Lalli wrote:

    > Rocky Allen wrote:
    >> I have an odd problem with the $file variable listed in the loop below
    >> the syntax of unix split command is as follows:
    >> Usage: split [OPTION] [INPUT [PREFIX]] and split will default to "x" as a
    >> prefix
    >>
    >> here is the output of this script (i ^c it)
    >> found file rallen.tar.gz
    >> error splitting file rallen.tar.gz
    >> :2 at ./perl-ATSDailyback line 49.

    >
    > Compare this output...
    >
    > print "found file $file";
    >> system("split -d --bytes=2000000000 $file $file.") == 0 or die "error splitting file $file:$?";

    >
    > ... with this code.
    >
    > Do you notice that the output has a newline after each file name?
    > Do you notice that the code does not?
    >
    > Somewhere along the way, you neglected to remove the \n from each
    > filename. You did not find the file "rallen.tar.gz". You found the
    > file "rallen.tar.gz\n".
    >
    > perldoc -f chomp
    >
    > Paul Lalli
    >
    >> }
    >> @filestobackup = `find $backupdir -follow -print 2> $finderrors`;
    >> }

    Thank you mr Lalli. You Rock
     
    Rocky Allen, Sep 8, 2005
    #3
  4. Rocky Allen

    Anno Siegel Guest

    Rocky Allen <> wrote in comp.lang.perl.misc:
    > I have an odd problem with the $file variable listed in the loop below


    Paul Lalli has cleared up your main problem.

    Somewhere else you say:

    > foreach my $file (@largefiles) {
    > $file =~ s/\.\///;


    That is an unsafe way to clean up a file name. Look at what it does
    to "a./b". On the other hand, it doesn't deal with "././x" correctly.
    File::Spec has the function canonpath() to logically clean up paths.
    Use it.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 9, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiggaz
    Replies:
    6
    Views:
    32,220
    Raghudubba
    Nov 5, 2009
  2. =?Utf-8?B?S2VuIEFkZW5pamk=?=

    Must declare the scalar variable '@FirstName'

    =?Utf-8?B?S2VuIEFkZW5pamk=?=, Apr 26, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    80,359
    leojose
    Aug 17, 2007
  3. Oded Dror

    Must Declare the Scalar Variable error

    Oded Dror, May 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    19,053
    Oded Dror
    May 28, 2006
  4. Clint Olsen
    Replies:
    6
    Views:
    407
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    193
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page