Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Capture error from one-liner

by dirtdog (Monk)
on Sep 19, 2014 at 16:04 UTC ( [id://1101236]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, Does anyone know how i would capture the return code from a perl one-liner executed from a korn shell script?

perl -i.bak -pe 's/AJZ/AZZ/g if /^AB/' <filename> if [[ $? != 0 ]];then exit 1 fi

if the one-liner fails for any reason I'd like to be able to exit with a failed status from the shell, but what i have doesn't work.

any help would be greatly appreciated. thanks

Replies are listed 'Best First'.
Re: Capture error from one-liner
by choroba (Cardinal) on Sep 19, 2014 at 16:41 UTC
    This is not specific to korn shell. Perl with -p doesn't die if a file doesn't exist (cf. the behaviour of open). Unfortunately, autodie doesn't help here, so if you want it to die, you have to change the logic to something like
    | V perl -e 'for my $file (@ARGV) { open my $IN, "<", $file or die $!; whi +le (<$IN>) {s/AJZ/AZZ/g if /^AB/; print}}' input > output
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Perl with -p doesn't die if a file doesn't exist

      Right you are; a workaround is

      qwurx [shmem] ~ > perl -p -e 'BEGIN{$SIG{__WARN__}=sub{die@_}}' nonexi +stent Can't open nonexistent: No such file or directory. qwurx [shmem] ~ > echo $? 2
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        Well, my one-liners are usually not warning-proof...
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      But wait the OP showed us an error displayed on the console!

      Maybe -i is producing it?

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      thanks for the help

      I'll pipe the command to a log and grep for errors.

Re: Capture error from one-liner
by LanX (Saint) on Sep 19, 2014 at 16:14 UTC
    > but what i have doesn't work.

    Unfortunately "doesn't work" doesn't work for us. :)

    Could you plz show us an error condition to reproduce your problem?

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Sure. Below is an example where the filename is not found. In this case i'd like to be able to capture an error and know that it failed.

      perl -i.bak -pe 's/AJZ/AZZ/g if /^AB/' J_BB@ Can't open J_BB@: No such file or directory. echo $? 0
        not an expert of korn-shell, but what you want is capturing STDERR.

        If there is no direct way to do it, but you could redirect STDERR to STDOUT and capture STDOUT.

        No idea if you need backticks° in ksh then!¹

        (I would do the whole task completely within Perl not in ksh²)

        HTH (otherwise others will know =)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        updates

        °) ksh seems to support $() for backticks

        ¹) normally errors are piped into a file like here, bash can use backticks to capture STDOUT

        ²) should be mentioned that this is not a Perl question!

Re: Capture error from one-liner
by Laurent_R (Canon) on Sep 19, 2014 at 17:53 UTC
    A possible workaround redirecting STDERR:
    $ perl -i.bak -pe 's/AJZ/AZZ/g if /^AB/' foobar 2> error.txt ~ $ cat error.txt Can't open foobar: No such file or directory.
    So you would just need to check if a non-empty error.txt file is created.
Re: Capture error from one-liner
by Bloodnok (Vicar) on Sep 19, 2014 at 16:57 UTC
    At it's simplest, could it not be ...
    set -e [ -f $fname ] && [ -r $fname ] perl -i.bak -pe 's/AJZ/AZZ/g if /^AB/' $fname
    Just a thought ...

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101236]
Approved by hominid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found