Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi perl monks,

In a perl script i am calling a ksh file to copy files. (ie.) inside ksh file i am running the below tar command to copy the files from source to destination.

My task is, Need to create a 'success.ok' file, if tar command got succeed, else create 'failure.ok'. The issue is, while running ksh file through perl script, by default i am getting the exist value as '0' for both failure and success scenario..

KIndly please suggest how to solve this.

(tar cvf - * | cd /var/tmp/dump; tar xcf - ); output=\$\?; if \$output -eq 0; then touch /var/tmp/dump/success.ok; );

(tar

Replies are listed 'Best First'.
Re: exit value in tar command
by eyepopslikeamosquito (Archbishop) on Aug 30, 2015 at 08:10 UTC

    To simplify detecting success or failure, I just echoed success or failure. I also made some other corrections to your attempt:

    • Used && instead of ; so that tar extraction is not attempted if the cd fails.
    • Changed the parens to create sub-shell on RHS.
    • Used . rather than * in the tar command to get all files in the dir (even those starting with .) and to ensure all sub-dirs are (recursively) copied.
    • Removed verbosity on tar command.
    I used the existence of the target directory as a simple way to test the failure case (how are you testing the failure case?). Worked for me as shown below:
    $ pwd /home/knob $ cat t1.sh #!/bin/ksh if tar cf - . | (cd /var/tmp/dump && tar xf -); then echo "success" else echo "failure" fi $ rm -fr /var/tmp/dump $ ./t1.sh ./t1.sh[2]: /var/tmp/dump: not found. failure $ mkdir /var/tmp/dump $ ./t1.sh success $ ls -l total 8 -rwxrwxrwx 1 knob devel 108 30 Aug 17:51 t1.sh $ ls -l /var/tmp/dump total 8 -rwxrwxr-x 1 knob devel 108 30 Aug 17:51 t1.sh

    Note that, for simplicity, I created a scratch directory (/home/knob above) containing just the test script itself t1.sh and cd'ed into this directory -- so this is the directory that is copied. If yours does not work, post the output of running your command (as I did above).

Re: exit value in tar command
by CountZero (Bishop) on Aug 30, 2015 at 11:51 UTC
    Is there a reason why you must run the tar command inside the ksh-script? Wouldn' it be better to run the tar command directly from Perl or perhaps even use the Archive::Tar module?

    Update: If all you want to do is to make a recursive copy of a directory, File::Copy::Recursive is a good alternative.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: exit value in tar command
by Laurent_R (Canon) on Aug 30, 2015 at 08:10 UTC
    Hmm,

    this isn't really a Perl question, but my quick tests (though not exactly your syntax) return a non negative value, usually 2, when tar fails.

    Did you try to run the shell command from the shell prompt (i.e. not from your Perl script)?

Re: exit value in tar command
by karlgoethebier (Abbot) on Aug 30, 2015 at 21:19 UTC

    A TMTOWTDI not using ksh (considered harmful) - not thoroughly tested and written in a hurry (but also assuming that using tar is the most performant way for recursive copying of dirs):

    #!/usr/bin/env perl use strict; use warnings; use Capture::Tiny qw(capture); use Path::Tiny; use feature qw (say); my $source = qq(nose/); path("failure.ok")->touch && die $! unless -e $source; my $target = qq(fubar/); path("failure.ok")->touch && die $! unless -e $target; my $tar = qq(tar cf - $source | (cd $target && tar xvf - )); my ( $stdout, $stderr, $exit ) = capture { system($tar); }; say $stdout; say $stderr; say $exit; $exit ? path("failure.ok")->touch : path("success.ok")->touch; __END__

    IPC::Run might also be interesting.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: exit value in tar command
by Anonymous Monk on Aug 30, 2015 at 07:08 UTC
    The correct tar command:

    (tar cvf - * | cd /var/tmp/dump; tar xvf - ); output=\$\?; if \$output -eq 0; then touch /var/tmp/dump/success.ok; fi;);