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.
- 1
os.system("bash -c "+bashcode)
?2016-09-25 17:06:21 +00:00CommentedSep 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
. 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
.tripleee– tripleee2016-09-25 17:27:43 +00:00CommentedSep 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 runbash -c ...
and capture and parse the output.ysth– ysth2016-09-25 18:50:26 +00:00CommentedSep 25, 2016 at 18:50
2 Answers2
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.
4 Comments
%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.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
Comments
Explore related questions
See similar questions with these tags.