0

I'm making an automator service, and the first part takes files and RARs them using a bash script

So in Automator, I have a Get Specified Finder Items action to test the workflow, and then the bash script

F=''
for i in "$@"; do 
    F="$F \"${i//\"/\\\"}\""
done

/usr/local/bin/rar a ~/archive.rar $F

so it just takes the file paths, puts quotes around them, and then runs the RAR command with all the files as arguments. When I run this, automator gives me the error

Cannot open "/Users/user/test.txt" No such file or directory

Cannot create ~/archive.rar No such file or directory

No clue why it's getting those errors. When I add an echo to the last command

echo /usr/local/bin/rar a ~/archive.rar $F

The results are the exactly the command I need. I can copy and paste it in terminal and it works.

Does automator run bash scripts differently then they would run in terminal?

6
  • ~ doesn't get expanded inside quotes, so you need at least ~/"archive.rar" there. And I would suspect some issues with handling spaces and quoting also for $F. Does it work if you run it directly in Terminal?
    – nohillside
    Commented Jan 12, 2016 at 7:04
  • Oops, yeah I didn't mean to have quotes around the ~/archive.rar. and actually, yeah it also does not work in terminal, the error says it still can't open the file(s) in the arguments Commented Jan 12, 2016 at 15:07
  • Why do you preprocess $@ into $F, can't you pass "$@" to rar directly?
    – nohillside
    Commented Jan 12, 2016 at 17:00
  • Passing "$@" would pass all the file paths as 1 long string. Each file path argument needs to be encased in quotes Commented Jan 12, 2016 at 17:05
  • There's some weird auto quoting going on when the command is called that screws it up. Since echoing the command actually displays the correct command, I can just pipe it to bash, and it works fine lol.... Commented Jan 12, 2016 at 17:13

2 Answers 2

2

bash does expansion of $@ different than all other environment variables to preserve white space etc in arguments. From bash(1):

@ Expands  to  the  positional  parameters,  starting from one.  When the expansion occurs
  within double quotes, each parameter expands to a  separate  word.   That  is,  "$@"  is
  equivalent  to  "$1"  "$2" ...  If the double-quoted expansion occurs within a word, the
  expansion of the first parameter is joined with the beginning part of the original word,
  and  the  expansion  of  the last parameter is joined with the last part of the original
  word.  When there are no positional parameters, "$@" and $@  expand  to  nothing  (i.e.,
  they are removed).

So you basically should be able to call rar within the Shell Script action without any parsing:

/usr/local/bin/rar a ~/archive.rar "$@"
1
  • Ha this did work and now I feeld stupid. Thanks man. Commented Jan 13, 2016 at 23:57
-1

Weird auto-quoting going behind the scenes that I couldn't figure out. So I just did this work-around to get it to do what I wanted, although not exactly a solution:

echo /usr/local/bin/rar a ~/archive.rar $F | bash
1
  • Please add the working scripts so others can benefit as well.
    – nohillside
    Commented Jan 12, 2016 at 18:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .