我有一個 Python3 腳本,它需要呼叫帶有一些引數的 shell 腳本。當我直接從終端呼叫這個 shell 腳本時——它可以作業。從終端呼叫 shell 腳本:
source $HW/scripts/gen.sh -top $TOP -proj opy_fem -clean
但是,當我嘗試使用 os.system(或 os.popen - 相同的結果)從 Python 3 以完全相同的方式呼叫 shell 腳本時,shell 腳本無法運行。對 shell 腳本的 Python 呼叫:
os.system("source $HW/scripts/gen.sh -top $TOP -proj opy_fem -clean")
獲取下一個錯誤:
/project/users/alona/top_fabric_verif_env/logic/hw/scripts/gen.sh: line 18: syntax error near unexpected token `('
/project/users/alona/top_fabric_verif_env/logic/hw/scripts/gen.sh: line 18: `foreach i ( $* )'
您能否解釋一下為什么相同的 shell 腳本無法從 Python 運行?感謝您的任何幫助
uj5u.com熱心網友回復:
foreach是一個 C-shell 命令。csh(以及類似的派生詞tcsh)不是 Unix/Linux 中的標準系統 shell。
如果您需要使用特定的 shell,例如 C-shell:
os.system('/bin/csh -c "put the command here"')
這將/bin/csh在標準 shell 中執行,但啟動兩個 shell 而不是一個會產生額外的開銷。更好的解決方案是:
subprocess.run(['/bin/csh', '-c', 'put the command here'])
請注意,source ...當 shell 在命令后退出時,使用 shell 的命令沒有多大意義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507786.html
標籤:Python python-3.x linux 壳
