bash 从 Mac Automator 运行 Shellscript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25161100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:03:09  来源:igfitidea点击:

Run Shellscript from Mac Automator

macosbashautomator

提问by Teletubbi-OS X

In OS X 10.9.5

在 OS X 10.9.5 中

I wrote a Shell Script (via vim). Save it and navigate to this script and

我写了一个 Shell 脚本(通过 vim)。保存并导航到此脚本,然后

sh code.shit runs perfect (in iTerm & Total Terminal).

sh code.sh它运行完美(在 iTerm 和 Total Terminal 中)。

The same command in the same directory produces via Mac Automator always an ERROR. Why?

同一目录中的相同命令通过 Mac Automator 始终产生一个错误。为什么?

在 Automator 和终端中。

echo $SHELL /bin/bash Why is it impossible to run a shellscript via Automator. enter image description here

echo $SHELL /bin/bash 为什么不可能通过 Automator 运行 shellscript。 在此处输入图片说明

回答by Lucademicus

This problem can be solved by adding the following code above your current code:

可以通过在当前代码上方添加以下代码来解决此问题:

export PATH=/usr/local/bin:$PATH

回答by Droppy

I suspect it's the cd Desktopbit. You can try:

我怀疑是cd Desktop一点。你可以试试:

(cd ~/Desktop; sh code.sh)

However:

然而:

  • You should make code.shexecutable so you don't need to invoke it with sh. This is done with chmod 0755 code.sh.

  • If you need the shell script to work from a certain directory (i.e. the directory where the script is located) then build that into the script so it can be invoked with just ~/Desktop/code.sh:

    #!/bin/bash
    dir=$(dirname 
    #!/bin/bash
    dir=$(dirname 
    ?  ~  cat tmp/code.sh
    #!/bin/bash
    dir=$(dirname ##代码##)
    cd $dir
    ls -l
    
    ?  ~  chmod 0755 tmp/code.sh
    ?  ~  tmp/code.sh
    total 64
    drwxr-xr-x   6 andy  staff    204 Feb 22 18:53 Archives
    drwxr-xr-x  11 andy  staff    374 Jun 18 13:59 DerivedData
    -rw-r--r--   1 andy  staff    225 May 20 13:44 MyFirstProgram.X
    -rwxr-xr-x   1 andy  staff   3072 May 20 13:44 MyFirstProgram.exe
    drwxr-xr-x   3 andy  staff    102 Jan  6  2014 bug_reports
    -rwxr-xr-x   1 andy  staff     43 Aug  6 14:15 code.sh
    -rw-r--r--   1 andy  staff  11539 May 20 08:33 iOS_Team_Provisioning_Profile_.mobileprovision
    -rw-r--r--   1 andy  staff   1438 May 20 08:40 ios_development.cer
    -rwxr-xr-x   1 andy  staff    272 Aug  5 08:55 script.sh
    
    ) cd $dir # do work
    ) cd $dir # do work
  • 您应该使code.sh可执行文件,因此您不需要使用sh. 这是通过chmod 0755 code.sh.

  • 如果您需要 shell 脚本在某个目录(即脚本所在的目录)中工作,则将其构建到脚本中,以便可以通过以下方式调用它~/Desktop/code.sh

    ##代码##

For example:

例如:

##代码##

回答by Teletubbi-OS X

Solution

解决方案

Create two output files: In Automator: env > ~/Desktop/file_1.txtin iTerm: env > ~/Desktop/file_2.txt

创建两个输出文件: 在 Automator 中: env > ~/Desktop/file_1.txt在 iTerm 中: env > ~/Desktop/file_2.txt

DIFF

差异化

diff -y ~/Desktop/file_1.txt ~/Desktop/file_2.txt

diff -y ~/Desktop/file_1.txt ~/Desktop/file_2.txt

And, voila, there are interessting differences! Insert the differences e.g. export LANG=de_DE.UTF-8to the script, and it works!

而且,瞧,有一些有趣的差异!将差异插入export LANG=de_DE.UTF-8到脚本中,然后就可以了!