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

10
  • 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? Commented Aug 6, 2014 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). Commented Aug 6, 2014 at 14:03
  • "Gestoppt" doesn't sound like an error proper, though. Did you stop it manually after some 30 seconds? Commented Aug 6, 2014 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)' Commented Aug 6, 2014 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. Commented Aug 6, 2014 at 14:37

3 Answers 3

13

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

export PATH=/usr/local/bin:$PATH
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Thx. this works perfect for any "normal" shell-script.
@Teletubbi-osx Sorry I don't understand what you are saying.
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 ...
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!

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.