By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

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

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

Code:
===
Expand|Select|Wrap|Line Numbers
  1. Perl file 
  2. my $diff1=system("sh diff.sh");
  3. my $diff2=system("sh diff1.sh");
  4.  
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 .
Expand|Select|Wrap|Line Numbers
  1. diff1.sh
  2. -------
  3. a=diff aaa ccc
  4. diff.sh
  5. --------
  6. b=diff aaa bbb
  7.  
  8.  
  9.  
  10. 122 $> cat aaa 
  11. 1hi hello
  12. 123 $> cat bbb
  13. hi hello
  14. 125 $ cat ccc
  15. 1hi hello
  16.  
  17. 120 $ diff aaa ccc
  18. echo $?
  19.  
  20.  
  21.  
  22. diff aaa bbb
  23. 1c1
  24. < 1hi hello
  25. ---
  26. > hi hello
  27. Exit 1
  28.  
  29.  echo $?
  30. 1
  31.  
Nov 11 '09 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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.

Expand|Select|Wrap|Line Numbers
  1. my $diff1 = `sh diff.sh`;
  2. my $diff2 = `sh diff2.sh`;
  3.  
Regards,

Jeff
Nov 12 '09 #2

P: 10
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 .
Nov 12 '09 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Nov 13 '09 #4

P: 10
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
Nov 20 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.