質問

I did hours on research regarding the following question but I wasn't able to find an answer at all. Though there seem to be many fellows having problems with that. I hope I will recieve some help from the community. ;)

I have a Cshell script where I need to call a Python3 script from. Also I am passing a variable. .csh

#!/bin/csh -f

set variable = value
/../geos.py $variable  

So far so fine. In my Python3 script I take this variable, do some calculations and now want to pass back the 'new_variable' to the VERY SAME C shell script in order to proceed my set of data.

.py

import os
...
new_variable = 'foobar'
os.environ['new_variable'] = new_variable
return new_variable

My actual goal is that my C Shell script:

#!/bin/csh -f

set variable = value
/../geos.py $variable  

echo $new_variable

doesn't return 'Undefined variable'. So obviously my code doesn't work. Sure, I might be able to temporarily save the python calculations into a file but this seems quite unconvincingly. Also, I understand that it is just not possible to manipulate an environmental variable of the shell through a child process, but still I only want to pass a normal variable. There should be one way, no? If it is possible, I wasn't able to figure out any solution using subprocess.check_call. What am I missing?

E D I T: Merci beaucoup.

I knew that there must have been an easy solution. Thanks a lot! For CSHELL the following code worked:

set new_variable=`../geos.py $variable`
echo $new_variable

For BASH the following code worked:

new_variable=`../geos.py $variable`
echo $new_variable

In the python script itself you don't need to do anything but putting your desired variable into standard output, e.g. print(you_even_can_name_them_as_you_want). No os.environ oo whatever necessary. Made my day. SOLVED

役に立ちましたか?

解決

in bash I'd use:

new_variable=$(../geos.py $variable)

Have the python script produce the new value as standard out (i.e. print(new_variable) )

In csh I don't know, maybe you would have to use backquotes instead of $() ?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top