Shell Script: Execute a Python Program from Within a Shell Script

Shell Script: Execute a python program from within a shell script

Just make sure the python executable is in your PATH environment variable then add in your script

python path/to/the/python_script.py

Details:

  • In the file job.sh, put this
#!/bin/sh
python python_script.py
  • Execute this command to make the script runnable for you : chmod u+x job.sh
  • Run it : ./job.sh

Execute python code from within a shell script

You can use a so-called "here document":

#!/usr/bin/env bash

echo "hello from bash"

python3 - <<'EOF'
print("hello from Python 3")
EOF

The single quotes around the first EOF prevent the usual expansions and command substitions in a shell script.

If you want those to happen, simply remove them.

shell script execute python code as shell script

You have some syntax errors etc in your scripts.....try this:

Create the following two scripts.

Name: test.sh

#!/usr/bin/env bash
ARRAY=(1 2 3 4)
for num in ${ARRAY[@]}; do
./hello.py
echo $num"th loop"
done

Name: hello.py

#!/usr/bin/env python
print('hello')

From your command line, run the following

chmod 700 test.sh ;  chmod 700 hello.py

Now from the command line, you can run:

./test.sh

The output will be:

>./test.sh 
hello
1th loop
hello
2th loop
hello
3th loop
hello
4th loop

Using bash to run python script

On MacOS, set the name of your script file to end in .command, and make the file executable. With that filename extension, you can double-click the file, and it will run Terminal application and any output (to stdout / stderr) will be displayed in the terminal window which pops up for execution.

=== /Users/john/Desktop/foo.command ===
#!/bin/bash
echo 'hello'

Then:

===  At command prompt===
$ cd ~/Desktop
$ chmod 755 foo.command

Double click on foo.command and you'll see window popup:

Last login: Thu Oct 5
/Users/john/Desktop/foo.command ; exit;
iMac:~/Desktop john$ /Users/john/Desktop/foo.command ; exit;
hello
logout
[Process completed]

In the popup window, you'll see lots of lines, plus your output ("hello").

In your particular example, I think you have two problems:

First, you mention /Desktop, which probably isn't what you want, as the user's Desktop is ~/Desktop. This would cause your script to fail.

Second, the output you would see in the popup window is the output your script writes directly to standard out. If you script is writing to another file, it may be working great, but you'll not see that information displayed in the popup (it will be in whatever file you wrote it to.) So it depends what your grab.py file actually does.

Finally, you say "run in the background". Technically, that's not what's happening, as it will run in a separate foreground process. I assume that's what you mean.

How to execute a shell program taking inputs with python?

I'll try and give you some hints to get you started - though bear in mind I do not know any of your tools, i.e. waf or csp-client, but hopefully that will not matter.

I'll number my points so you can refer to the steps easily.


Point 1

If waf is a build system, I wouldn't keep running that every time you want to run your csp-client. Just use waf to rebuild when you have changed your code - that should save time.


Point 2

When you change directory to /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1 and then run ./build/csp-client you are effectively running:

/home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1/build/csp-client -k/dev/ttyUSB1

But that is rather annoying, so I would make a symbolic link to that that from /usr/local/bin so that you can run it just with:

csp-client -k/dev/ttyUSB1

So, I would make that symlink with:

ln -s /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1/build/csp-client  /usr/local/bin/csp-client

You MAY need to put sudo at the start of that command. Once you have that, you should be able to just run:

csp-client -k/dev/ttyUSB1

Point 3

Your Python code doesn't work because every os.system() starts a completely new shell, unrelated to the previous line or shell. And the shell that it starts then exits before your next os.system() command.

As a result, the cmp ident command never goes to the csp-client. You really need to send the cmp ident command on the stdin or "standard input" of csp-client. You can do that in Python, it is described here, but it's not all that easy for a beginner.

Instead of that, if you just have aa few limited commands you need to send, such as "take a picture", I would make and test complete bash scripts in the Terminal, till I got them right and then just call those from Python. So, I would make a bash script in your HOME directory called, say csp-snap and put something like this in it:

#/bin/bash

# Extend PATH so we can find "/usr/local/bin/csp-client"
PATH=$PATH:/usr/local/bin

{
# Tell client to take picture
echo "nanoncam snap"
# Exit csp-client
echo exit
} | csp-client -k/dev/ttyUSB1

Now make that executable (only necessary once) with:

chmod +x $HOME/csp-snap

And then you can test it with:

$HOME/csp-snap

If that works, you can copy the script to /usr/local/bin with:

cp $HOME/csp-snap /usr/local/bin

You may need sudo at the start again.

Then you should be able to take photos from anywhere just with:

csp-snap

Then your Python code becomes easy:

os.system('/usr/local/bin/csp-snap')


Related Topics



Leave a reply



Submit