How to return a value of a variable from shell script to perl script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babp
    New Member
    • Sep 2009
    • 10

    How to return a value of a variable from shell script to perl script

    HI ,
    Is there any way to return a value of variable from shell to perl script.

    Code:
    ===
    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 .
    Code:
    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
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    You are corrent, the system() command does return the return code (1 or 0) or the command it ran. My suggestion would be to use backtics in place of the system command. Its another way to execute system level commands, but you can get the output instead.

    Code:
    my $diff1 = `sh diff.sh`;
    my $diff2 = `sh diff2.sh`;
    Regards,

    Jeff

    Comment

    • babp
      New Member
      • Sep 2009
      • 10

      #3
      Hi Jeff ,
      Thanks for the response since i jhave explained already I need exit status as 1 or 0.

      If i'll use backtick it'll return the diff output like below:
      1c1
      < 1hi hello
      ---
      > hi hello

      But I need script to return a value like 1 if it finds any diffrence between two files and 0 if it doesnot find any diffrence between two files .

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Ok, I guess I can say that I am now confused. The system() function does just that. If the script/command that was run exits normally, then it returns zero. Otherwise it returns a 1.

        If the script you are running returns a value, then you would use backtics. Otherwise, I highly suggest you re-write your shell scripts in Perl to do what you need to do. It will preempt any and all confusion on what you are really wanting.

        Regards,

        Jeff

        Comment

        • babp
          New Member
          • Sep 2009
          • 10

          #5
          Hi jeff ,
          Thanks for the reply i got the solution :

          ACE25C1: ...Monday/package 154 $ cat diff.pl
          my $a=system("sh diff.sh");
          print $a;


          ACE25C1: ...Monday/package 155 $ cat diff.sh
          diff aaa bbb
          if [ `diff aaa bbb | grep -c "^1c1$"` -eq 0 ]
          then
          exit 0
          else
          exit 1
          fi

          Comment

          Working...