【问题标题】:Issue trying to run chrome in a bash script on mac尝试在 mac 上的 bash 脚本中运行 chrome 时出现问题
【发布时间】:2020-11-21 20:56:16
【问题描述】:

我正在尝试通过 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

【问题讨论】:

    标签: bash macos shell


    【解决方案1】:
    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
    

    您在这里转义了空格 (\ ) 以告诉 shell 这个字符串是完整的,而不是程序及其参数。

    替代方法是使用引号,即foo "a b" 实际上与foo a\ b 相同。当我们使用引号时,我们不会转义空格,所以应该是:

    EXEC="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
    

    【讨论】:

      【解决方案2】:

      你双引号。 "foo bar" 已经等同于 foo\ bar(真的,\f\o\o\ \b\a\r,但除了空格,没有一个字符具有需要转义的特殊含义,因此例如 \f 计算结果为 f)。

      "foo\ bar",则等价于foo\\\ bar;引用的反斜杠是文字,而不是引用以下空格。

      【讨论】:

        猜你喜欢
        • 2015-02-24
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 2020-08-28
        • 1970-01-01
        • 1970-01-01
        • 2020-08-14
        • 2019-12-26
        相关资源
        最近更新 更多