14

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. enter image description here

Teletubbi-OS X
  • 391
  • 1
  • 3
  • 13
  • Works for me. Does `code.sh` expect some input from somewhere? Can you run it with `sh -x` (or rather, `bash -x`, if it's really properly a Bash script) to see where exactly it's failing? – tripleee Aug 06 '14 at 14:00
  • Does the file have a trailing newline? For me, it doesn't print anything, but succeeds; but I have a different version of OSX for testing (some really old one). – tripleee Aug 06 '14 at 14:03
  • "Gestoppt" doesn't sound like an error proper, though. Did you stop it manually after some 30 seconds? – tripleee Aug 06 '14 at 14:04
  • "Gestoppt": it means in German language "STOP". It is not the usual ```can't find lib-x or lib-y```. Only STOP. The Shell-script itself contains a **RScript**. ```Rscript -e 'shiny::runApp(("/Users/Einstein/Git/RShiny/fooapp"),launch.browser=TRUE)'``` – Teletubbi-OS X Aug 06 '14 at 14:10
  • Well, yes, I get substantially the same message (albeit not in German) if I put `sleep 1000` in the command to run, and press the Stop button after allowing it to run for a while. So I guess that's what you did as well. – tripleee Aug 06 '14 at 14:37
  • The information above starkly constracts with the problem description. It is quite clear then that you don't get this error message if you run a script which contains `echo $SHELL` and that the actual problem (if there is one) is inside the R script. Nominating this question for closing. – tripleee Aug 06 '14 at 14:38
  • Not sure about the significance of `launch.browser` but speculating that this is the actual source of your problem. – tripleee Aug 06 '14 at 14:39
  • ```launch.browser``` isn't the problem. I tested several variations of execution the RScript - with and without shell script. Both variations, direct from terminal or as shell script working very well. But from Automator there is always a problem. I have no idea how to identify the difference of Terminal ans Automator-Temrinal (run shell script). – Teletubbi-OS X Aug 07 '14 at 00:29
  • Try running it with [`--verbose`](http://astrostatistics.psu.edu/su07/R/html/base/html/options.html)? – tripleee Aug 07 '14 at 04:16
  • No, sorry, ```--verbose```brings no working process. I was in good hope that the ```chmod```could save the problem, but it doesn't. – Teletubbi-OS X Aug 08 '14 at 17:45

3 Answers3

13

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

export PATH=/usr/local/bin:$PATH
Lucademicus
  • 383
  • 3
  • 9
4

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
Droppy
  • 9,691
  • 1
  • 20
  • 27
  • Thx. this works perfect for any "normal" shell-script. – Teletubbi-OS X Aug 08 '14 at 01:01
  • @Teletubbi-osx Sorry I don't understand what you are saying. – Droppy Aug 08 '14 at 05:40
  • 1
    Your example runs perfect! My problem: my shell-script contains "material" that is easy to run by iTerm/Terminal etc. but not from Automator. I recreate this problem with 5 OS X machines (macbook air, macbook pro, iMac; 10.9.4/10.9.5). Its always the same; works from Terminal doesnt work from Automator. So it´s obvious that autmator is the problem. When you execute ```Rscript -e 'shiny::runApp(("/Users/Einstein/Git/RShiny/fooapp"),launch.browser=TRUE)'``` via Terminal, you can see the loading process and the browser opens - perfect. In Automator, no output ... – Teletubbi-OS X Aug 08 '14 at 10:27
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!

Community
  • 1
  • 1
Teletubbi-OS X
  • 391
  • 1
  • 3
  • 13
  • 3
    Difference in `PATH` variable between Automator and Terminal is the main cause of this. Automator defaults to `PATH=/usr/bin:/bin:/usr/sbin:/sbin` while environmental variable would be similar to `PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin` so Automator is missing `/usr/local/bin`. Exporting `PATH` in Automator script should indeed fix the issue. – Ninetou May 24 '16 at 17:42