Translate shell script not working within Automator

automatorbashscriptterminal

I have this code to translate a word from/to English/Spanish:

S=`/usr/local/bin/trans -b -id -no-ansi "$a" | grep Code | awk '{print $2}'`
if [ "$S" = "en" ]; then T=es; else T=en; fi
/usr/local/bin/trans -b :"$T" "$a"

(trans is from https://github.com/soimort/translate-shell/)

Running the script straight in Terminal (bash) works great.

But when I try to use it in Automator, I get this error:

/usr/local/bin/trans: line 5073: gawk: command not found

Running gawk in Terminal also works fine.


UPDATE: Thanks to solver below, final working script for automator is:

PATH=$PATH:/usr/local/bin
S=`/usr/local/bin/trans -b -id -no-ansi "$@" | grep Code | awk '{print $2}'`
if [ "$S" = "en" ]; then T=es; else T=en; fi
/usr/local/bin/trans -b :"$T" "$@"

Best Answer

The default PATH passed to the Run Shell Script action in Automator is:

/usr/bin:/bin:/usr/sbin:/sbin

I always add a PATH=..., where ... is the actual PATH used in Terminal, to the top of the Run Shell Script action in Automator.

Or you have to add the fully qualified pathname of any executable to the command line that is not in the default PATH passed to the Run Shell Script action in Automator.


The last line in the trans script is gawk -f <(echo -E "$TRANS_PROGRAM") - "$@" and unless gawk is in the default PATH passed you'll need to add the location of gawk to the PATH you pass to the Run Shell Script action in Automator, or add the fully qualified pathname to the gawk command at the end of the trans script.

In Terminal use which gawk to get its path, e.g.:

which gawk
/usr/local/bin/gawk

In trans, change:

gawk -f <(echo -E "$TRANS_PROGRAM") - "$@"

To:

/usr/local/bin/gawk -f <(echo -E "$TRANS_PROGRAM") - "$@"

Or in the Run Shell Script action in Automator, e.g.:

PATH=$PATH:/usr/local/bin

Or use the PATH from the output of echo $PATH in Terminal.