Run Shellscript from Mac Automator
Asked Answered
M

3

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

Mutant answered 6/8, 2014 at 12:59 Comment(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?Lustreware
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).Lustreware
"Gestoppt" doesn't sound like an error proper, though. Did you stop it manually after some 30 seconds?Lustreware
"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)'Mutant
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.Lustreware
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.Lustreware
Not sure about the significance of launch.browser but speculating that this is the actual source of your problem.Lustreware
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).Mutant
Try running it with --verbose?Lustreware
No, sorry, --verbosebrings no working process. I was in good hope that the chmodcould save the problem, but it doesn't.Mutant
D
13

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

export PATH=/usr/local/bin:$PATH
Dextroamphetamine answered 11/7, 2016 at 14:27 Comment(0)
U
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
Uracil answered 6/8, 2014 at 13:15 Comment(3)
Thx. this works perfect for any "normal" shell-script.Mutant
@Teletubbi-osx Sorry I don't understand what you are saying.Uracil
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 ...Mutant
M
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!

Mutant answered 3/9, 2014 at 9:28 Comment(1)
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.Calamite

© 2022 - 2024 — McMap. All rights reserved.