簡體   English   中英

如何使用從 Python 返回的輸入參數從 Bash 腳本執行 python 腳本?

[英]How to execute python script from Bash Script with the input parameter which returns from Python?

我正在從 bash 腳本調用 py 腳本,但是我的 python 腳本返回了我想在 bash 腳本中使用的值。 有人可以幫忙嗎?

你好

這很簡單。 只需使用 print() 輸出您想要的內容,您就可以使用這個 output。 從 python function 中,您還可以使用 return 和字符串或其他任何內容將 output 作為 bash/sh 腳本的輸入。 在 linux 中,當程序/腳本以零( 0 )的返回碼退出時,一切都很好。 所以規則應該是:以文本形式返回/退出/錯誤消息:print('whatever')Returncode:sys.exit(number)

#!/usr/bin/python3
import sys
print('No error - But i return 1 and not 0')
sys.exit(1)

在 bash ( test.py ) 中玩...

# test.py &>/dev/null && echo 'zero' || echo 'not zero'
not zero
# echo -ne $(test.py)'\n'
No error - But i return 1 and not 0

所以第一個命令(short if then else)只檢查返回碼(exitcode),第二個使用 output。 兩者都使用,一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM