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.shperl -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.

askedSep 25, 2016 at 17:03
mrthingfish's user avatar
3
  • 1
    os.system("bash -c "+bashcode) ?CommentedSep 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 bydata::dumper. Justenv in the shell should give you the output you want. Whether you callbash -c "code" from Perl or Python should be immaterial; the only question is really why you are usingeither when clearly this is a Bash task. If the task is to import these variables to Python, perhaps look atshlex.CommentedSep 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 withenv as the third line of the bash script) and use some pythony way to runbash -c ... and capture and parse the output.CommentedSep 25, 2016 at 18:50

2 Answers2

1

Your program is generating a script and running it.

A first python approximation is:

import osscript=""". /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.

answeredSep 25, 2016 at 17:11
sureshvv's user avatar
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.shperl -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.shperl -mdata::dumper -e 'print dumper \%env';"""

You would shell out to Bash using something likeos.system orsubprocess.check_output.

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

(The wisdom, and the result of theeval 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. Useeval 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 isENV in uppercase. In Bash, you access the environment with a lowercaseenv

To access the host environment in Python, useos.environ

answeredSep 25, 2016 at 17:12
dawg's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.