473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re: Adding environment variables to bash.

John Lawrence wrote:
You can make a command use the current shell though if you use the '.'
command e.g.:

jl cat env.sh
export TEST='hello'

jl ./env.sh && env | grep TEST #Doesn't set TEST in parent shell
jl . ./env.sh && env | grep TEST #Adding '. ' before the
command uses the same shell
TEST=hello
doesn't exactly work for Python scripts, though:

$ cat env.py
#!/usr/bin/env python
import os
os.environ["TEST"] = "hello"

$ . ./env.py && env | grep TEST
import: unable to open X server `'.
bash: os.environ[TEST]: command not found

</F>

Sep 11 '08 #1
1 3235
On Sep 11, 10:25 am, nntpman68 <news1...@free.frwrote:
doesn't exactly work for Python scripts, though:
$ cat env.py
#!/usr/bin/env python
import os
os.environ["TEST"] = "hello"
$ . ./env.py && env | grep TEST
import: unable to open X server `'.
bash: os.environ[TEST]: command not found
</F>

There's two options for the desperate ones.

1. Assuming the python script doesn't want to print anything useful
================================================== =======================

the python script just prints the exoprt commands and is
being called via backticks from a shell code snipped being sourced with .

#-------- mypythonfile.py ------------------------
#!/usr/bin/env python
value = myfavourite_python_function()
print 'export ENV_VAR="%s"' ^ value
#--------------- end of file --------------

#----------------- my_wrapper_file.sh ------------------
`./mypythonfile.py`
# file end

and then you call
. ./my_wrapper_file.sh

2._ Pytho script wants to display something and set a variable
================================================== =================

a file being invoked with . calls the python script (which will create a
small file with variables to be set).
then this created file is being sourced

#-------- mypythonfile.py ------------------------
#!/usr/bin/env python
value = myfavourite_python_function()
file('my_export_commands.sh','w').write('export ENV_VAR="%s"\n' % value)
#--------------- end of file --------------

#----------------- my_wrapper_file.sh ------------------
./mypythonfile.py
. ./my_export_commands.sh
# file end

bye

N

Fredrik Lundh wrote:
John Lawrence wrote:
You can make a command use the current shell though if you use the '.'
command e.g.:
jl cat env.sh
export TEST='hello'
jl ./env.sh && env | grep TEST #Doesn't set TEST in parent
shell
jl . ./env.sh && env | grep TEST #Adding '. ' before the
command uses the same shell
TEST=hello
doesn't exactly work for Python scripts, though:
$ cat env.py
#!/usr/bin/env python
import os
os.environ["TEST"] = "hello"
$ . ./env.py && env | grep TEST
import: unable to open X server `'.
bash: os.environ[TEST]: command not found
</F>
oooh, clever but also... teh evil! Ok not evil, but teh not-pretty.
But you did say "for the desperate" so good on you. cheers.
Sep 11 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SG | last post by:
I use the %ENV to access the environment variables. I came across the following problem. When I change the environment variable from command line, i.e let us say origianlly env var $FOO was bar1,...
2
by: btober | last post by:
I run the following script to export some data from my development database and then update or insert the records into to the quality assurance testing database, but I get a warning notice that I...
2
by: Sean Campbell | last post by:
Hello all! I have been looking in the Framework documentation for an answer and it has not shown itself. Could someone tell me if it is possible with VB or C# to add and modify Windows System...
5
by: Maxim Veksler | last post by:
Hello list, I'm trying to write a python script that would allow me to manipulate shell variables of the calling shell. I'm trying to write some logic that would know to add LD_LIBRARY_PATH to...
2
by: Henry Hollenberg | last post by:
Hello, I have written a script that uses environment variables set during a particular users login in ".bash_profile" and ".profile". I have changed to that users uid and gid in my python...
6
by: toralf | last post by:
Hello, I've to script sth under Windows and would like to set the DB2 environment w/o that batch file to stay at my bash prompt. How can I do that ? -- MfG/Sincerely
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
0
by: Fredrik Lundh | last post by:
aditya shukla wrote: not if you run the script from the shell. when a process starts, it gets a *copy* of the parent's environment. it can modify that copy, but it cannot modify the...
9
by: Michel Leunen | last post by:
Hi, Could someone explain me what I'm doing wrong here? I'm trying to retrieve the value of an environment variable in Ubuntu 8.04 like this: 'michel' This works but this doesn't: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.