繁体   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