Re: Capture stderr stdout using system call with commas?

From: Leslie Hazelton (seawolf_at_attglobal.net)
Date: 02/03/04


Date: Tue, 03 Feb 2004 15:41:35 GMT


Marv wrote:
> Hello,
>
> I've got an issue that I can't seem to figure out.
>
> I'm trying to run some mount and rsync commands from inside a perl
> script.
>
> I've figured out from other postings that you have to use a system
> call and seperate all the command line options with quotes and commas.
> I'm not sure why this is, but I can never get the commands to work if
> I enclose them with backticks? Can anybody explain this?
>
> Anyway, if the commands could be enclosed in backticks I would have my
> problem solved, but since I have to use system (), I can't seem to
> figure out how to capture this data either to a variable or a file.
> I've tried using '2>&1 tmp.errors' but this doesn't work either
> because of the commas. Here is a sample of what I'm doing:
>
> system (
> "mount",
> "-r",
> "-t",
> "smbfs",
>
> "-o","username=$adminuser,password=$adminpass",
> "//$ip/$ashare",
> "/share/$nbname/$ashare",
> "2>&1 tmp.errors"
> );
>
> This method doesn't work because it takes the '2>&1 tmp.errors' as
> another option of mount and bombs out. If I include it at the end of
> '/share/$nbname/$ashare' then it takes as part of the name of the
> mount point and bombs out.
>
> Can anybody help me capture this info? Or show me how this can be down
> with backticks.
>
> Thanks.
> Marv
>

I have several perl scripts that run as cron jobs and I want to capture
all the output into a log file for later review. I use a two stage
approach to do this. The cron job is a shell script that runs the perl
program.

This is a snip of the perl portion. The main concept came from the Perl
Cookbook, O'Reilly, ISBN 1-56592-243-3.
------------- snippet -------------
  my @archiveDir = ("/mnt/Archives", "/tmp", "/root");

  foreach my $var ( @archiveDir) {
    my $args = "$var/\*$cleanDate\*";

    print "Removing files matching [$args]\n";
    print "\tRunning [rm -f $args]\n";

    open (MyCmd, "rm \-f $args |") or die "Can't run program: $!\n";
    while (<MyCmd>) {
          my $output .= $_;
          print $output;
          }
    close (MyCmd);
    }
-------------- end snippet ----------

The shell script which executes the perl program clean-archive is as
follows.

#!/bin/bash
#
runDate=`date +%F`
logFile=/root/clean-archive-$runDate.log
/root/clean-archive > $logFile 2>&1

While this may not be the most elegant approach I find it works well for
me. In any case, it's another varient for your consideration.

-- 
Les Hazelton
Registered Linux user # 272996




Relevant Pages

  • RE: Run Shell Script Using Perl
    ... Run Shell Script Using Perl ... You need to rewrite a shell script that does file transfer in Perl? ... would work -- ftp, scp, rsync, cp over NFS mount (or SMB mount, or DAV ...
    (perl.beginners)
  • Re: Run Shell Script Using Perl
    ... You need to rewrite a shell script that does file transfer in Perl? ... would work -- ftp, scp, rsync, cp over NFS mount (or SMB mount, or DAV ...
    (perl.beginners)
  • Re: Dialog and Tempfiles in Perl
    ... Both commands is include in shell script. ... direct replacement for dialog in Perl. ... databases in postgresql, define IP Address, Port ...
    (perl.beginners)
  • Re: [opensuse] Need help!
    ... has), I just pop it in one of my other OS machines, mount it and copy ... reiserfsck is tell me I can use a -B flag to fix it but when I try to ... For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx ...
    (SuSE)
  • Re: A _Bunch_ of Article_XXXX commands in _One_ send buffer.
    ... Speaking of perl... ... I love macros, they remove a lot of redundancy and other ugliness. ... issuing Article_XXXX commands one at a time. ... Terminals have a baud rate, ...
    (news.software.readers)