Can I Inline a Bourne/C shell script?

P

Prab_kar

Hi all,

1. Is it possible to inline a sh/csh script from a Perl program.
I realize, I can run the shell script as,
system("Foo.sh") ;
but I want all the variables I set in my Perl program to be available
to Foo.sh.

2. Failing that, is there a way I can 'export' all the variables I set
in Perl to Foo.sh?

Thanks for your time,
Prabh
 
J

Jürgen Exner

1. Is it possible to inline a sh/csh script from a Perl program.
I realize, I can run the shell script as,
system("Foo.sh") ;
but I want all the variables I set in my Perl program to be available
to Foo.sh.

2. Failing that, is there a way I can 'export' all the variables I set
in Perl to Foo.sh?

Your Question is Asked Frequently: "perldoc -q environment"

jue
 
C

Chris Mattern

Hi all,

1. Is it possible to inline a sh/csh script from a Perl program.
I realize, I can run the shell script as,
system("Foo.sh") ;
but I want all the variables I set in my Perl program to be available
to Foo.sh.

Um, how do you mean that? If you mean the environment variables you
set in perl, they *are* available to Foo.sh:

testit.pl:
#!/usr/bin/perl

use strict;
use warnings;

$ENV{thisone} = "from perl";

system("./Foo.sh");

---------------------------------------------------------

Foo.sh:
#!/bin/sh

echo "$thisone"

---------------------------------------------------------

syscjm@sakura:~$ ./testit.pl
from perl

---------------------------------------------------------

If you mean you want to be able to access the perl script's variables
from the shell script, how about opening the shell as a pipe and
putting in the script as a here doc?

#!/usr/bin/perl

use strict;
use warnings;

my $an_interpolated_var = "from perl";

open (my $mysh, "|-", "sh") or die ("Couldn't start shell: $!");
print $mysh <<ENDIT;
echo "This is my shell":
echo "This is $an_interpolated_var"
echo "This is \$a_shell_var"
ENDIT

close ($mysh);

-----------------------------------------------------------

syscjm@sakura:~$ export a_shell_var="from the shell"
syscjm@sakura:~$ ./testit.pl
This is my shell
This is from perl
This is from the shell

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
D

Darren Dunham

1. Is it possible to inline a sh/csh script from a Perl program.
I realize, I can run the shell script as,
system("Foo.sh") ;
but I want all the variables I set in my Perl program to be available
to Foo.sh.

Most programs don't do that. Private variables aren't shown to other
programs.
2. Failing that, is there a way I can 'export' all the variables I set
in Perl to Foo.sh?

*all*? Hmm. Not sure.

Just like in shell you can export individual variables. In perl you can
explicitly populate the environment.

system('echo x ${foo} x');

$ENV{'foo'} = 'something';
system('echo x ${foo} x');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top