Executing output of python from loop in bash - python

I wrote a bash that has python command included in loop: (part of script)
#!/bin/bash
ARG=( $(echo "${#:3}"))
for (( i=1; i<=$#-3; i++ ))
do
python -c "print('<Command with variables>' * 1)"
done
When I run it, depends on number of my args for example I have this output:
nohup command-a &
nohup command-b &
nohup command-c &
How do I execute the output lines from bash immediately?
Can I tell python command to run them in each iteration? How?
Thank you

You can achieve that by executing the python code in a sub-shell and evaluating the content of that shell afterwards.
eval $(python -c ...)
$() returns a string you can evaluate with eval

You are asking two questions. I can only answer the first one:
If you want to run commands coming to the standard output, just redirect them to bash:
python -c 'print "ls"' | bash

Related

Not getting prompt after executing python script from bash

I am executing a python script from bash script as follows
"python -O myscript.pyo &"
After launching the python script I need to press "enter" manually to get prompt.
Is there a way to avoid this manual intervention.
Thanks in advance!
pipe a blank input to it:
echo "" | python -O myscript.pyo
you might want to create a bash alias to save keyhits: alias run_myscript="echo '' | python -O myscript.pyo"
placing wait after the line to run the process in background seems to work.
Source:
http://www.iitk.ac.in/LDP/LDP/abs/html/abs-guide.html#WAITHANG
Example given:
!/bin/bash
test.sh
ls -l &
echo "Done."
wait
Many thanks

Return prompt after accessing nohup.out (bash)

I'm running a Python script from bash using nohup. The script is executed via my bashrc as part of a shell function. If I run it like this:
function timer {
nohup python path/timer.py $1 $2 > path/nohup.out 2>&1 &
echo 'blah'
}
Everything works and I get my prompt back. However, if instead of echo I call tail to access the end of the nohup output file, like this:
function timer {
nohup python path/timer.py $1 $2 > path/nohup.out 2>&1 &
tail -f path/nohup.out
}
my prompt is not returned. I would like to see the contents of nohup.out and get back to the prompt without having to use CTRL-c.
I have followed the advice here, but adding </dev/null yields the same results as above.
You won't get prompt.
Because, tail -f will always watch the file (path/nohup.out) to output appended data as the file grows. You can try tail -n to get last 10 lines of path/nohup.out.

Calling python script from bash stops at command prompt

I am calling a python script:
/bin/sh
python ~/Documents/Projects/Programming/Python/svg/svg2dxf.py $1 0
After running the script, I get a python command prompt ($) and it's only when I type "exit" at the command prompt that the script runs.
What am I doing wrong?
Change the following line:
/bin/sh
with (shebang interpreter directive):
#!/bin/sh
Otherwise, the new shell instance is invoked; until the new shell is exit, next line is not executed.
You should remove the leading:
/bin/sh
Your current script does two things:
1) execute a new instance of /bin/sh
==> which gives your the shell $ sign
2) execute the python script
Your script should be:
python ~/Documents/Projects/Programming/Python/svg/svg2dxf.py $1 0

How to pipe output from a linux command to a python script?

We have a python script that accepts the following arguments as example:
mypie.py --size=20 --pielist='{"apple":[5],"orange":[7]}' --quantity=10
We tried in our test.sh bash shell script:
test.sh 20 5 7 10
test.sh
export SIZE=$1
export APPLE=$2
export ORANGE=$3
export QUANTITY=$4
echo "--size=$SIZE" --pipelist='{\"apple\":[$2],\"orange\":[$3]}\"' --quantity=$4" | tee output.txt
cat output.txt | sudo python mypie.py
We got error
ERROR: size is not specified.
but when we cat output.txt, we can see that it is there with size value.
--size=20 --pielist='{"apple":[5],"orange":[7]}' --quantity=10
What are we doing wrong ?
Thanks
This would do -
cat output.txt | sudo xargs python mypie.py
Using the pipe redirects the content to stdin of the program - not the command line.
You could consider building up an args variable - then passing that to both the output.txt, and your python script.
For example:
export SIZE=$1
export APPLE=$2
export ORANGE=$3
export QUANTITY=$4
export ARGS="--size=$SIZE" --pipelist='{\"apple\":[$2],\"orange\":[$3]}\"' --quantity=$4"
echo $ARGS | tee output.txt
sudo python mypie.py $ARGS
You can use command substitution to directly place the output of command as the arguments to your python program.
python mypie.py `cat output.txt`
or
python mypie.py $(cat output.txt)
The simple and obvious solution is to change test.sh so that it passes the parameters correctly.
#!/bin/sh
set -x # if you want to see the script's parameters
sudo python mypie.py --size="$1" \
--pipelist="{'apple':[$2],'orange':[$3]}" --quantity="$4"
Passing the arguments as standard input (which is what a pipe does) is simply not how it is usually done, nor a particularly suitable model for this particular type of interaction.

Passing arguments to python interpreter from bash script

Sorry this is a very newbie question, but I just can't seem to get it to work.
in my bash script, I have
python=/path/to/python
script=$1
exec $python $script "$#"
How would I pass an argument, say -O to the python interpreter? I have tried:
exec $python -O $script "$#"
And have tried changing python variable to "/path/to/python -O", as well as passing -O to the script, but every time i do any of these three, I get import errors for modules that succeed when I remove the -O.
So my question is how to tell the python interpreter to run with -O argument from a bash script?
Thanks.
You should shift your positional parameters to the left by 1 to exclude your script which is in the first arguments from being included to the arguments for python.
#!/bin/sh
python=/path/to/python
script=$1; shift
exec "$python" -O "$script" "$#"
Then run the script as bash script.sh your_python_script arg1 arg2 ... or sh script.sh your_python_script arg1 arg2 ....

Categories