0

I have a bash script that I can run flawlessly in my Rpi terminal in its folder:

./veye_mipi_i2c.sh -r -f mirrormode -b 10

it works like this: Usage: ./veye_mipi_i2c.sh [-r/w] [-f] function name -p1 param1 -p2 param2 -b bus

options:

-r read

-w write

-f [function name] function name

-p1 [param1] param1 of each function

-p2 [param1] param2 of each function

-b [i2c bus num] i2c bus number

When I try to run it in Python (2) via my Spyder editor with os.system, I get a "0" return which I interpret as "succesfully executed" but in fact the script has not been executed and the functions have not been performed. I know this because the script is suppose to change the camera functioning and by checking the images I take afterwards, I can see that nothing has changed.

import os
status = os.system('/home/pi/VeyeMipi/Camera_Folder/veye_mipi_i2c.sh -w -f mirrormode -p1 0x04 -b 10')
print status

Any idea, what causes this? The bash script uses two other scripts that lie in the same folder location (read and write). Could it be, that it cannot execute these additional scripts when startet through Python? It does not make sense to me, but so do a lot of things....

Many thanks

3
  • How are we supposed to reproduce the problem here? Run bash -x /home/pi/... instead and see what's going on under the hood. Commented Jan 24, 2021 at 7:45
  • veye_mipi_i2c.sh does not appear to be written to handle failures or return canonical exit codes, so you can not assume that 0 means success Commented Jan 24, 2021 at 8:09
  • What happens if you just exit 1 from the bash script? If you can see that in Python, it just shows you need to work further on extracting a minimal reproducible example for your question. Commented Jan 24, 2021 at 9:39

4 Answers 4

1

Ok, I understand that my question was not exemplary because of the lack of a minimal reproducible example, but as I did not understand what the problem was, I was not able to create one.

I have found out, what the problem was. The script I am calling in bash requires two more scripts that are in the same folder. Namely the "write" script and "read" script. When executing in terminal in the folder, no problem, because the folder was the working directory.

I tried to execute the script within Spyder editor and added the file location to the PATH in the user interface. But still it would not be able to execute the "write" script in the folder.

Simply executing it in the terminal did the trick.

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

Comments

0

It would help if you fix your scripts so they don't depend on the current working directory (that's a very bad practice).

In the meantime, running

import subprocess
p = subprocess.run(['./veye_mipi_i2c.sh', '-r', '-f', 'mirrormode', '-b', '10'], cwd='/home/pi/VeyeMipi/Camera_Folder')
print(p.returncode)

which changes the directory would help.

2 Comments

Thank you Diego. But how do you deal with this problem if scripts depend on each other? Do you mean I should add the depencies to the system path?
You can add the scripts to a directory that is already in PATH (i.e. /usr/local/bin) or symlink them there
0

Use subprocess and capture the output:

import subprocess
output = subprocess.run(stuff, capture_output=True)

Check output.stderr and output.stdout

Comments

0

The Python print function displays the exit status of the system call. Try it without the print function and you should get just the output of the system call without the exit status:

import os
status = os.system('/home/pi/VeyeMipi/Camera_Folder/veye_mipi_i2c.sh -w -f mirrormode -p1 0x04 -b 10')
status

Comments

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.