0

I want to ask you guys how to pass a variable from a shell script to a python script and save it then as local variable. I have a Variable in the shell script:

#!/bin/bash
userpem=$(egrep "CN=$1/" index.txt|awk '{print $3}').pem
output='openssl x509 -in $userpem -noout -text'
export output

I read on some posts that I can do that with os.environ(foo) but I just saw examples like this:

from django.shortcuts import render
import os
import subprocess
from django.http import HttpResponse


def info(request, benutzername):
  os.chdir("/var/www/openvpn/examples/easy-rsa/2.0/keys")
  subprocess.Popen(["/var/www/openvpn/examples/easy-rsa/2.0/keys/getinfo.sh",benutzername])
  output = os.environ['output']
  return HttpResponse(output)
2
  • 1
    var = os.getenv('variablename') or var = os.environ['variablename'] Commented Nov 14, 2013 at 11:30
  • If you don't want to print it, what do you want to do with it? Stick it in a global or local variable? (@hus787's comment shows how to do that.) Save it to a file? Turn the bits into PCM audio and play it through the sound card? Commented Nov 14, 2013 at 11:35

2 Answers 2

1

You can't do what your trying to do.

fedorqui's answer shows how to read environment variables from Python, but that won't help you.


First, you're just starting the shell script and immediately assuming it's already done its work, when it may not even have finished launching yet. You might get lucky and have it work sometimes, but not reliably. You need to wait for the Popen object (which also means you need to store it in a variable)—or, even more simply, call it (which waits until it finishes) instead of just kicking it off.


And you probably want to check the return value, or just use check_call, so you'll know if it fails.


Meanwhile, if you fix that, it still won't do you any good. export doesn't export variables to your parent process, it exports them to your children.

If you want to pass a value back to your parent, the simplest way to do that is by writing it to stdout. Then, of course, your Python code will have to read your output. The easiest way to do that is to use check_output instead of check_call.


Finally, I'm pretty sure you wanted to actually run openssl and capture its output, not just set output to the literal string openssl x509 -in $userpem -noout -text. (Especially not with single quotes, which will prevent $userpem from being substituted, meaning you looked it up for nothing.) To do that, you need to use backticks or $(), as you did in the previous line, not quotes.


So:

#!/bin/bash
userpem=$(egrep "CN=$1/" index.txt|awk '{print $3}').pem
output=$(openssl x509 -in $userpem -noout -text)
echo $output

And:

def info(request, benutzername):
    os.chdir("/var/www/openvpn/examples/easy-rsa/2.0/keys")
    output = subprocess.check_output(["/var/www/openvpn/examples/easy-rsa/2.0/keys/getinfo.sh",benutzername])
    return HttpResponse(output)

As a side note, os.chdir is usually a bad idea in web servers. That will set the current directory for all requests, not just this one. (It's especially bad if you're using a semi-preemptive greenlet server framework, like something based on gevent, because a different request could chdir you somewhere else between your chdir call and your subprocess call…)

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

1 Comment

omg ! all that worked out !!! well it seems like I have to learn much more ^^ I thank you very much for that good explanation as well !!! that check_output so awesome :D and thanks for note with the os.chdir problems :) pretty awesome you took so much time to explain that :)
1

You need to use os.environ['variablename'] to work with an environment variable.

For example here the v variable is created and exported:

$ export v="hello"

Let's create a script a.py:

import os

d=os.environ['v']
print "my environment var v is --> " + d

And calling it:

$ python a.py
my environment var v is --> hello

8 Comments

Updated using it as local variable, as the OP indicates on comments.
I tried what you did as well d=os.environ['v'], probably should have written it :/ it just gives me following: Exception Type: KeyError Exception Value: 'v'
@Newbie can you post everything you tried? It is not clear what you want to accomplish. Also, note a variable declared in a script might just be accessible from that subshell, so when the program finishes you cannot access to it any more.
@Newbie ok :) And how do you call it? You call the shell script and then python script?
@Newbie: Exporting a variable doesn't affect the parent process's environment, it only affects child processes' environments.
|

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.