Running *nix commands in embedded script

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Running *nix commands in embedded script Fri Jul 20, 2012 6:57 pm • by tone
I'm having problems getting system commands to execute in an embedded shell script. For example, if Hazel detects a DJVU file in a folder, I want it to execute "djvu2pdf $1". However, I always get an error (status code 127 which, I think, means the shell command can't be found). If I manually execute "djvu2pdf [filename]" in Terminal, it works as expected, since djvu2pdf (and all other system commands) are in directories in $PATH.

I managed to get a command working by installing it to the folder Hazel is monitoring, but this isn't a solution.

How can I get Hazel to detect and run shell commands?

Cheers.
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Re: Running *nix commands in embedded script Sat Jul 21, 2012 8:19 pm • by a_freyer
The solution you found evidences the issue; Hazel executes the command from a different location than terminal.

Here's a better solution than copying the executable into the monitored directory:

Step 1: Find where you command exists

Open terminal and type "whereis thecommandtorun" where thecommandtorun is replaced by the command that you want to run (djvu2pdf). For instance, if I wanted to find where awk is, I'd type:

Code: Select all
Andrews-MacBook-Air:~ andrewfreyer$ whereis awk
/usr/bin/awk


The result is the full path to awk.

If the "whereis" command fails to find your executable, you can try "which" as well.

Hint: If you type whereis awk | pbcopy the full path will be copied directly to the clipboard.

Step 2: Prepend the path in the embedded script within Hazel

Before in Hazel:
Code: Select all
myCustomBinaryForUseWithHazel $1


After in Hazel:
Code: Select all
/usr/bin/myCustomBinaryForUseWithHazel $1
Last edited by a_freyer on Sun Jul 22, 2012 3:19 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 4:31 am • by tone
Thanks for the detailed reply. I should've mentioned that I had tried specifying the full path to the executable in Hazel, with no luck.

I use Homebrew, which installs packages to /usr/local/. For some reason, whereis doesn't return anything for any of my Homebrew-installed utilities.

For example, although for awk I get the expected

Code: Select all
MBA:~ fort$ whereis awk
/usr/bin/awk
MBA:~ fort$


for djvu2pdf (and all other brews), I get

Code: Select all
MBA:~ fort$ whereis djvu2pdf
MBA:~ fort$


I've also tried adding a symlink to the executable in the watched folder, but this doesn't work either.
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Re: Running *nix commands in embedded script Sun Jul 22, 2012 3:17 pm • by a_freyer
Full path should be what solves your problem. Perhaps you could change the shell to bash (/bin/bash) over bourne (/bin/sh)?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 3:21 pm • by a_freyer
Another possibility for the problem is character escaping.

From your OP it looks like you're doing this:

Code: Select all
/path/to/command $1


When you should be doing this:

Code: Select all
/path/to/command "$1"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 4:21 pm • by tone
Still no luck. This is the only line in my embedded script:

Code: Select all
/usr/local/Cellar/djvu2pdf/0.9.2/bin/djvu2pdf "$1"


but I'm still getting an error - this time it's status code 1. Any idea what it might mean?

Code: Select all
2012-07-22 21:19:15.478 hazelworker[45496] 2012-05-30.djvu: Rule djvu2PDF matched.
2012-07-22 21:19:15.991 hazelworker[45496] [Error] Shell script failed: Error processing shell script on file /Users/fort/Documents/DocumentLibrary/Word2Papers/2012-05-30.djvu.
2012-07-22 21:19:15.998 hazelworker[45496] Shellscript exited with non-successful status code: 1
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Re: Running *nix commands in embedded script Sun Jul 22, 2012 4:29 pm • by a_freyer
I'm assuming that when you run command /Users/fort/Documents/DocumentLibrary/Word2Papers/2012-05-30.djvu in terminal, the error is not present?
Last edited by a_freyer on Sun Jul 22, 2012 5:04 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 4:35 pm • by a_freyer
Its also possible that the status code of 1 for the script is success. Was the PDF created for that file?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 4:48 pm • by tone
a_freyer wrote:I'm assuming that when you run /Users/fort/Documents/DocumentLibrary/Word2Papers/2012-05-30.djvu in terminal, the error is not present?


Right. Actually, the command would be djvu2pdf 2012-05-30.djvu.

a_freyer wrote:Its also possible that the status code of 1 for the script is success. Was the PDF created for that file?


Unfortunately not :(
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Re: Running *nix commands in embedded script Sun Jul 22, 2012 5:03 pm • by a_freyer
This is the exact line that Hazel is executing:

/usr/local/Cellar/djvu2pdf/0.9.2/bin/djvu2pdf "/Users/fort/Documents/DocumentLibrary/Word2Papers/2012-05-30.djvu"

Does this work in terminal, or does this spit an error?

Looking at the source for your converter, it seems like a full path for the file should work fine.

Further, it looks like its creating your PDF in the temp directory. Do you have write access there?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 5:07 pm • by a_freyer
Another user had a similarly inexplicable problem a few weeks ago. He re-installed Hazel for all users and that solved his issue.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Sun Jul 22, 2012 5:18 pm • by tone
a_freyer wrote:This is the exact line that Hazel is executing:

/usr/local/Cellar/djvu2pdf/0.9.2/bin/djvu2pdf "/Users/fort/Documents/DocumentLibrary/Word2Papers/2012-05-30.djvu"

Does this work in terminal, or does this spit an error?


It works in terminal and produces the PDF.

a_freyer wrote:Looking at the source for your converter, it seems like a full path for the file should work fine.

Further, it looks like its creating your PDF in the temp directory. Do you have write access there?


Are you talking about /private/tmp? I do have write access to all but one of the folders in it but, anyway, when I execute djvu2pdf in the terminal, the PDF is created on my desktop.

a_freyer wrote:Another user had a similarly inexplicable problem a few weeks ago. He re-installed Hazel for all users and that solved his issue.


Thanks! I'll try that tomorrow. Now...to bed.
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Re: Running *nix commands in embedded script Mon Jul 23, 2012 4:47 am • by tone
I reinstalled Hazel for all users (it was previously installed for my user account only) but am still getting the error :(

This makes no sense...

I'll try a clean install, after backing up my rules.
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Re: Running *nix commands in embedded script Mon Jul 23, 2012 2:10 pm • by a_freyer
I'd next add some debug logging to the executable to see which exit 1 is tripping via hazel.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Running *nix commands in embedded script Mon Jul 23, 2012 4:45 pm • by tone
The clean install made no difference, so...

a_freyer wrote:I'd next add some debug logging to the executable to see which exit 1 is tripping via hazel.


Here's the debug log I get when I drop a .djvu file into Word2Papers:

https://www.dropbox.com/s/l0nv50nbc6yv544/hazelDebug.log
tone
 
Posts: 18
Joined: Sun Jun 12, 2011 6:02 pm

Next

Return to Support

cron