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

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system command will return 0 . diff1.sh ------- a=diff aaa ccc diff.sh -------- b=diff aaa bbb 122 $> cat aaa 1hi hello 123 $> cat bbb hi hello 125 $ cat ccc 1hi hello 120 $ diff aaa ccc echo $? 0 diff aaa bbb 1c1 < 1hi hello --- > hi hello Exit 1 echo $? 1
  • Comment on How to return value of a variable from shell script to perl script

Replies are listed 'Best First'.
Re: How to return value of a variable from shell script to perl script
by ikegami (Patriarch) on Nov 11, 2009 at 19:25 UTC

    Echo it and capture the script's STDOUT.

    my $output = `sh diff1.sh`;

    See qx// in perlops

Re: How to return value of a variable from shell script to perl script
by Plankton (Vicar) on Nov 11, 2009 at 19:41 UTC
    I like using HEREDOC's like so ...
    my $shell_out = <<`SHELL`; diff $file1 $file2 ls -l SHELL print "$shell_out";
Re: How to return value of a variable from shell script to perl script
by Perlbotics (Archbishop) on Nov 11, 2009 at 23:52 UTC

    Welcome to the Monastery, babp. You might get better feedback if you format your question a little bit more readable. Please see Markup in the Monastery (esp. <code>). (Un-)fortunately, the HTML source of this page is more readable, so let me repeat the basic parts of your question:

    Perl program:
    my $diff1 = system("sh diff.sh"); my $diff2 = system("sh diff1.sh");
    diff.sh:
    a=diff aaa ccc
    diff1.sh:
    b=diff aaa bbb

    The problem is, that diff.sh and diff1.sh don't do what you (probably) had in mind: The variable (a or b) is set to the string diff, then the shell tries to execute(!) aaa with argument bbb or ccc.

    Suggestion (non-Perl): Correct diff.sh (analogue diff1.sh), e.g.

    diff aaa bbb a=$? # do something else with $a exit $a

    Which can be reduced to...

    diff aaa bbb
    ... in case you only want to execute diff. The shell (usually) returns the exec state of the last statement executed. BTW: Perl has borrowed this idea here and there (e.g. return) - although this is not best practice.

    But then, wouldn't it be easier to avoid the *.sh wrappers and put that into the Perl script too?

    my $diff1 = system("diff aaa ccc >/dev/null 2>&1"); my $diff2 = system("diff aaa bbb >/dev/null 2>&1");
    Note the redirections to get rid of diff's output. If your version of diff supports a switch to suppress output and just return the exec state, then give the system LIST variant a try for better security.
    Please be aware that $diff1 and $diff2 contain 0 on success but not necessarily 1 on error since catched signals are encoded into the return value (details in system).

    Finally, CPAN offers a lot of modules when searching for diff. Might be worth a check...

    HTH
Re: How to return value of a variable from shell script to perl script
by ikegami (Patriarch) on Nov 11, 2009 at 19:25 UTC

    Echo it and capture the script's STDOUT.

    my $output = `sh diff1.sh`;

    See qx// in perlops