Store the output of command in variable in unix [duplicate] - python

This question already has answers here:
How to store standard error in a variable
(20 answers)
Closed 7 years ago.
Need to check the version of python so for that i am hitting this command in a script.
I am trying to store the output of the given command in a variable "x".So that I can use this x in further script.
But when I am trying to print x it is showing null value(no value).
[bin]$x=`/path/thirdparty/python/2.7/bin/python2.7 -V`
Python 2.7.8
[bin]$echo x
Please help me to store the value of the command in a variable.

python -V writes to the standard error, not to the standard output. So you have to redirect STDERR (2) to STDOUT (1).
$ x=$({python -V} 2>&1)
$ echo $x
Python 2.7.6

Hm... the output is possibly written to stderr, not stdout. Try this:
x=`/path/thirdparty/python/2.7/bin/python2.7 -V 2>&1`

Related

How python can execute shell command with env [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Python subprocess/Popen with a modified environment
(9 answers)
Closed 3 months ago.
Basically, os.system() can execute shall commands, such as:
os.system("echo $A")
It will work well and output value environment variable A.
But this seems to not work:
os.system("A=b echo $A")
It won't output "b" as expected.
How Python can execute this type of command command?
It won't output "b" as expected.
The expansion $A to empty string is done by the shell earlier, before running echo.
Try this:
os.system("A=b; echo $A")

Pass Python Variable to Shell [duplicate]

This question already has answers here:
Environment variables set from python not visible in shell script
(2 answers)
Closed 7 months ago.
I defined a shell script with a python script inside on the fly. Getting variables from shell to Python works fine, but the other way round does not work as expected. My Idea was to create an env variable os.environ["NEW_VAR"] = "test", but when trying is echo it, it is just None.
#!/bin/bash
args=("$#")
GIT_PASSWORD=${args[0]}
export GIT_PASSWORD=$GIT_PASSWORD
python - << EOF
import os
print(os.environ.get("GIT_PASSWORD"))
os.environ["NEW_VAR"] = "test"
EOF
echo $GIT_PASSWORD
echo $NEW_VAR
echo "Back to bash"
Why does that not work and 2. What is the correct way to pass a variable here?
Thank you!
Written in the comments, inheriting environment variables only works from parent to child processes, not the other way round.
if you need shell variables set from a python script you can create the output in your shell script and eval the results:
eval "$(python -c 'print("HELLO=WORLD")')"
echo "$HELLO"
yields WORLD

Capture run time of python script executed inside shell script [duplicate]

This question already has answers here:
Get program execution time in the shell
(11 answers)
Closed 5 years ago.
I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.
Any leads would be helpful thanks in advance.
You can use the time command to get the runtime of the python script.
]$ cat time_test.bash
#!/bin/bash
# You can use: time python script.py
time python -c 'import os;os.getenv("HOME")'
Output will be something like this
]$ ./time_test.bash
real 0m0.010s
user 0m0.005s
sys 0m0.005s
Call the python script with /usr/bin/time script. This allows you to track CPU and wall-clock time of the script.

How to make `python2 -V >> myfile` "work"? [duplicate]

This question already has answers here:
Redirect all output to file using Bash on Linux? [duplicate]
(6 answers)
Closed 6 years ago.
I want to automatically record my python version in a computerinfo file. However
python2 -V >> myfile
prints only to the console but not to myfile. How can I get the output into a file?
Curiously, for python3 -- python3 -V >> myfile -- this works as I expected.
I am on Ubuntu 16.04 and I use the bash shell.
There is a bug in python2 -V, that prints the version as stderr instead of stdout. Try to redirect the stderr like:
python2 -V 2>> myfile

Is there anything like "set -x" in python [duplicate]

This question already has answers here:
What is the Python equivalent of `set -x` in shell?
(4 answers)
Closed 5 years ago.
I'd like to run my python script in such a way that every line is printed as it is executed (for debugging purposes), like how you can add set -x to a bash script. Is this possible? If not, what other strategies are there for debugging python that don't involve adding log lines everywhere? I usually use pdb but this isn't always suitable for time sensitive applications eg networking.
Use the trace module.
$ python -m trace -t myscript.py | grep myscript.py

Categories