簡體   English   中英

如何使Shell腳本啟動python腳本?

[英]How to make shell script launch python script?

我需要一個.command文件,該文件可以在與其所在的目錄中啟動python腳本。

到目前為止,我得到了:

#!bin/bash
python /Users/username/Desktop/IF.py

但是終端每次返回相同的錯誤。

Last login: Sun Oct 11 01:48:38 on ttys000
MacBook-Pro:~ username$ /var/folders/m7/yf7p3vdj2mx0l9r7qb68rttc0000gn/T/Cleanup\ At\ Startup/run 466235498.368.command.command ; exit;
/var/folders/m7/yf7p3vdj2mx0l9r7qb68rttc0000gn/T/Cleanup At Startup/run-466235498.368.command.command: line 3: bin/bash: No such file or directory
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

請注意錯誤是我有兩行代碼的第3行...

為什么不從一開始就啟動python腳本本身呢? 您可以在頂部添加shebang,如下所示:

#! /user/bin/env python
... the rest of your python script here

然后只需賦予腳本可執行權限:

chmod +x nameofscript.py

然后像這樣啟動它:

./nameofscript.py

如果您不希望腳本依賴於環境,則只需使用以下shebang即可:

#! /usr/bin/python

也許是因為shebang (或hashbang )。 關於什么是shebang (wiki)


#!bin/bash
  ^ 
python /Users/username/Desktop/IF.py

這意味着系統將使用bin/bash來運行該程序,但是bin/bash意味着path/bin/bash

例如,如果您在/home則系統將使用/home/bin/bash運行該程序。


我認為您需要這樣做:

#!/bin/bash
python /Users/username/Desktop/IF.py

請注意,我將#!bin/bash替換為#!/bin/bash

暫無
暫無

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

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