2

I have a bash script that sets two variables to values (not a STDOUT) . I want to store those variables inside a perl script.

Sorry dont have example.. but this is the requirement. any thoughts please

In shell script i am doing something like this

source <Script Name> <Variable1> <Variable2>
Value1=$output1
Value2=$output2

===================== I want to do same inside perl

Tried

system("sh <Script> <Variable1> <Variable2>");
$Value1 = "$output1";
$Value2 = "$output2";
print $Value1 

Getting Blank

3
  • Is 'output1' just the name of a variable internal to the script? You'll need to communicate it somehow, probably by writing its value to a stream. Variables internal to the script will not be directly available to its caller. Commented Aug 5, 2020 at 21:06
  • so the shell script <Script Name> sets the variables $output1 and $output2 ? Commented Aug 5, 2020 at 21:51
  • 1
    @Kris :First of all, that is no bash involved in your code (you explicitly run your script with sh), so I suggest to remove the bash tag. For sending values from your bash script back to Perl, you can use either stdout, or an external file, or maybe a bidirectional pipe. In all cases, you have to parse the output from your script. Commented Aug 6, 2020 at 7:01

2 Answers 2

3

To build on the answer by @Snorik, here is one way you can do it:

system 'bash', '-c', 'source xyz.sh; echo -n $output1 >1.txt; echo -n $output2 >2.txt';
my $output1 = `cat 1.txt`;
my $output2 = `cat 2.txt`;
Sign up to request clarification or add additional context in comments.

3 Comments

Should be i.e. echo -n "$output1" >1.txt, to preserve the number of spaces in the variable.
Thank you i wrote a wrapper to the shell script and redirect the output to file and then i will use that file in perl script
can you answer your own question please?
1

As Bash doesnt allow to use actual return values, the easiest way is the bash script to write each value to a file and then read each value from the file.

Have a look at this

1 Comment

Unfortunately i dont have access to the bash script, all that i can do is to capture the variables and use it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.