Run Shellscript from Mac Automator

為{幸葍}努か 提交于 2019-12-18 07:44:37

问题


In OS X 10.9.5

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

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

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

in Automator and in Terminal.

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


回答1:


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

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



回答2:


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

(cd ~/Desktop; sh code.sh)

However:

  • You should make code.sh executable 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 $0)
    cd $dir
    # do work
    

For example:

➜  ~  cat tmp/code.sh
#!/bin/bash
dir=$(dirname $0)
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



回答3:


Solution

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

DIFF

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-8 to the script, and it works!



来源:https://stackoverflow.com/questions/25161100/run-shellscript-from-mac-automator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!