bash - 尝试在 mac 上的 bash 脚本中运行 chrome 时出现问题
问题描述
我正在尝试通过 mac 上的 bash 脚本获取 chrome 的版本。我可以直接从终端运行以下命令:
macbook-pro:jenkins_server$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version
Google Chrome 87.0.4280.67
但是当我尝试将这样的内容放在 bash 脚本中时:
EXEC="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
"$EXEC" --version
我得到以下结果:
macbook-pro:jenkins_server$ ./test_script.sh
./test_script.sh: line 2: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome: No such file or directory
解决方案
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
在这里,您转义了空格 ( \
) 以告诉 shell 这个字符串是完整的,而不是程序及其参数。
另一种方法是使用引号,即foo "a b"
实际上与foo a\ b
. 当我们使用引号时,我们不会转义空格,所以应该是:
EXEC="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
推荐阅读
- javascript - 有没有办法按类过滤所有以前的元素?
- javascript - 不推荐使用 Node/Express 未处理的承诺拒绝
- flutter - SliverAppBar 仅在滚动后向上推
- c# - 如何从 API 端点设置 cookie?
- mysql - 休眠 HHH000346 托管刷新期间出错。插入未提交
- python-3.x - 在 tkinter 中,在 python 3 中,我怎样才能让一个按钮执行一个命令来执行诸如“def helloCallBack(a)”之类的功能,并允许我给出一个?
- data-binding - 如何获取数据的特定字段以在请求中使用它?
- java - H2 的休眠多租户问题:错误的架构
- php - PHP 在没有 SSH/Console 的情况下在虚拟主机上调度任务
- excel - 如果找到匹配错误,我如何计算