0

I've got some code that I've translated from perl into python, but I am having a time trying to figure out this last part.

my $bashcode=<<'__bash__';
. /opt/qip/etc/qiprc;
. /opt/sybase/sybase.sh
perl -mdata::dumper -e 'print dumper \%env';
__bash__
my $var1;
eval qx(bash -c "$bashcode");

While I understand (a bit) what this is doing, I can't seem to find out how to do this in python. Any help would be greatly appreciated.

3
  • 1
    os.system("bash -c "+bashcode) ? Commented Sep 25, 2016 at 17:06
  • Using Perl just to get a dump of the environment seems dubious, unless you specifcally need it in the Perlish format generated by data::dumper. Just env in the shell should give you the output you want. Whether you call bash -c "code" from Perl or Python should be immaterial; the only question is really why you are using either when clearly this is a Bash task. If the task is to import these variables to Python, perhaps look at shlex. Commented Sep 25, 2016 at 17:27
  • The existing code is using a bash script to call two other scripts to set some environment variables, and then getting that modified environment back to the original perl script by dumping in perl format from the bash script. You will need to replace that part (perhaps just with env as the third line of the bash script) and use some pythony way to run bash -c ... and capture and parse the output. Commented Sep 25, 2016 at 18:50

2 Answers 2

1

Your program is generating a script and running it.

A first python approximation is:

import os
script=""". /opt/qip/etc/qiprc;
          . /opt/sybase/sybase.sh
          perl -mdata::dumper -e 'print dumper \%env';
"""
os.system(script)

As you can see, perl is still being used inside your script which is using the module data::dumper. If you want to use python here, you may need the equivalent module.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that's where I'm stuck. The example posted is from the original perl script. Im not using it in the python code, but I do want to figure out how to translate it to the python equivalent
I suspect that it just dumps your environment variables in a readable manner. Just use the shell command "env" and that may be enough.
note that %env is not the same as %ENV. Although that's what could be in %env, it's probably worth-while checking to ensure that's the case.
and it should be Data::Dumper and Dumper too
0

This part:

my $bashcode=<<'__bash__';
. /opt/qip/etc/qiprc;
. /opt/sybase/sybase.sh
perl -mdata::dumper -e 'print dumper \%env';
__bash__

Is a here doc and you would do this in Python:

bashcode="""\
. /opt/qip/etc/qiprc;
. /opt/sybase/sybase.sh
perl -mdata::dumper -e 'print dumper \%env';
"""

You would shell out to Bash using something like os.system or subprocess.check_output.

The result of that is then fed to eval to be run as Perl code.

(The wisdom, and the result of the eval is dependant on what these Bash commands produce. It would appear that those commands are producing a Perl script. Obviously, that will not execute in Python. Python also has eval. Use eval in any language cautiously.)

The use of \%env in Perl would seem to indicate access to the environment of the host. However, the actual Perl variable to access the OS environment hash is ENV in uppercase. In Bash, you access the environment with a lowercase env

To access the host environment in Python, use os.environ

Comments

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.