MacOS双击shell脚本报错的问题


张登友,张登友的博客,张登友的网站——

macos - 双击 shell 脚本问题

在终端可以正常运行脚本,但双击直接打开无法运行的问题

尝试在Mac上双击运行以下hexo server.sh脚本。 (要求:必须双击运行)

#!/usr/local/bin/zsh
hexo s

这是输出。 (终端保持打开,并显示以下消息)

Usage: hexo <command>

Commands:
  help     Get help on a command.
  init     Create a new Hexo folder.
  version  Display version information.

Global Options:
  --config  Specify config file instead of using _config.yml
  --cwd     Specify the CWD
  --debug   Display all verbose messages in the terminal
  --draft   Display draft posts
  --safe    Disable all plugins and scripts
  --silent  Hide output on console

For more help, you can use 'hexo help [command]' for the detailed information
or you can check the docs: http://hexo.io/docs/
Saving session...completed.

当我从终端运行相同的命令时…

INFO  Validating config
INFO  Start processing
INFO  Hexo is running at http://localhost:2222 . Press Ctrl+C to stop.

…它可以工作正常,并按预期方式打开。那么这应该怎么解决呢?

答案

将以下内容添加到脚本的开头使得双击时可以正常工作。

cd "$(dirname "$0")"

整个hexo server.sh脚本如下:

#!/usr/local/bin/zsh
cd "$(dirname "$0")"
hexo s

文章作者: 张登友
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 张登友 !
  目录