View Single Post
Old 08-01-2015, 10:28 PM   #1
wladdy
Enthusiast
wladdy began at the beginning.
 
Posts: 28
Karma: 10
Join Date: Oct 2010
Device: iPad / Kindle DX
Quoting variables containing options in CLI

I'm on Max OSX. I want to use a bash shell script to fill the 'authors' field and a 'translator' custom field.

This works perfectly:

Code:
#!/bin/bash
id=10030
calibredb set_metadata -f "authors:John Doe" -f "#translator:Jane Doe" $id
Now, I want to do the same thing with variables.

Code:
#!/bin/bash
id=10030
a='-f "authors:John Doe"'
t='-f "#translator:Jane Doe"'
echo calibredb set_metadata $a $t $id
calibredb set_metadata $a $t $id
Since what's returned by echo works when pasted in a new shell window, I'd expect the last line to work as well. However, it returns the following error:
You must specify a record id as the first argument.

If I put the variables in the last line inside double quotes, the error becomes:
"authors is not a known field

This gives me idea to do the following, which does work, despite the fact that what echo returns now fails when pasted in a new shell window.

Code:
#!/bin/bash
id=10030
a='-fauthors:John Doe'
t='-f#translator:Jane Doe'
echo calibredb set_metadata "$a" "$t" "$id"
calibredb set_metadata "$a" "$t" "$id"
I do not understand what's going on. How come that a command typed directly in Terminal requires a space after -f while a script with variables works with no space between -f and what follows?
I realize that the main issue here is my confusion about quotes and expansions in bash, but since I am trying to teach myself scripting mainly to use with Calibre, I thought that someone in this forum will enlighten me .

My practical goal is to use set_metadata in a script with only one variable for all the options - I want only one because my script builds it progressively, through loops and conditionals. However, any general description and explanation of the phenomena I'm encountering here would be immensely valued.
Thanks in advance. W.
wladdy is offline   Reply With Quote