简体   繁体   中英

Bash script executed within Python with os.systems returns 0 but does not execute/ write

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

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.

Use subprocess and capture the output:

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

Check output.stderr and output.stdout

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM