我使用的是MacBook Pro 16“(与MacOS Catalina一起使用)。我希望通过Spotlight搜索直接运行MacOS脚本。我不想打开任何IDE或终端。我已经看过说明:
中的hello.py
#!/usr/bin/env bash /Users/Gory/hello.py python3
chmod u+x u+x
遵循上述步骤后,我通过Spotlight搜索samplescript.command并按enter键。我希望看到一个终端窗口上印着“你好世界”。相反,我得到以下信息:
MacBook-Pro:~ Gory$ /Users/Gory/samplescript.command ; exit;
/Users/Gory/samplescript.command: line 1: {rtf1ansiansicpg1252cocoartf2511: command not found
/Users/Gory/samplescript.command: line 2: syntax error near unexpected token `}'
/Users/Gory/samplescript.command: line 2: `\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
怎么啦?
发布于 2020-08-03 02:16:14
您的问题
您的问题是由您的第二步:
使用TextEdit创建一个文本文件,并使用.command文件扩展名保存它(例如: samplescript.command)。该文件应包含以下内容
默认情况下,TextEdit使用富文本格式。
结果:您的samplescript.command
文件不包含您期望的内容。
#!/usr/bin/env bash
python3 /Users/Gory/hello.py
但实际上
{\rtf1\ansi\ansicpg1252\cocoartf2513
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 #!/usr/bin/env bash\
python3 /Users/Gory/hello.py}
这些内容不是有效的命令,然后执行时会导致观察到的错误:
line 1: {rtf1ansiansicpg1252cocoartf2513: command not found
line 2: syntax error near unexpected token `}'
line 2: `\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'
如何修复它?
如果您想使用TextEdit,那么在输入任何内容之前,将格式从富文本更改为纯文本。
创建一个新的document
最好固定
不要使用TextEdit或任何其他类型的文本处理器应用程序来编写代码(脚本),这样从一开始就避免了此类问题。
在任何情况下,无论您使用什么编辑器,都要反复检查(脚本)文件内容实际上是您期望的内容。
发布于 2020-08-02 17:40:56
我是个学习者,也有过类似的问题。我的工作是在shebang行中增加一个空间,然后我运行chmod到完整的文件路径。
"#! /usr/bin/env bash"
https://stackoverflow.com/questions/61458373
复制相似问题