Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
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
Old 08-02-2015, 11:14 AM   #2
wladdy
Enthusiast
wladdy began at the beginning.
 
Posts: 28
Karma: 10
Join Date: Oct 2010
Device: iPad / Kindle DX
To be even more specific, this works;

Code:
a="authors:John Doe"
calibredb set_metadata -f "$a" 10030
this does not:

Code:
a="-f authors:John Doe"
calibredb set_metadata "$a" 10030
but this does:

Code:
a="-fauthors:John Doe"
calibredb set_metadata "$a" 10030
Why does putting the -f flag inside a variable requires the removal of the subsequent space? Is it a bash issue or something specific to calibredb?

Thank in advance. W.
wladdy is offline   Reply With Quote
Advert
Old 08-02-2015, 11:27 AM   #3
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,188
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
What you need to understand as far as calibredb is concerned is that you need to pass either of the two following forms of command line arguments to it

*One* argument of the form
-fwhatever

or *Two* arguments of the form
-f whatever

These are exactly equivalent.

Now remember that bash splits command line arguments at spaces, unless you surround them in quotes, in which case they become one argument.

With that in mind you should be able to understand the results you are getting.
kovidgoyal is online now   Reply With Quote
Old 08-06-2015, 09:57 PM   #4
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Passing the entire command as one string to `eval` will allow it to be interpreted the way you would've expected. Of course, "eval is evil".

Code:
#!/bin/bash
id=10030
a='-f "authors:John Doe"'
t='-f "#translator:Jane Doe"'
eval calibredb set_metadata $a $t $id

Last edited by eschwartz; 08-06-2015 at 10:21 PM.
eschwartz is offline   Reply With Quote
Reply

Tags
bash, cli, mac, quotes, set_metadata


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Suggestion: Book level variables phossler Editor 15 02-17-2014 11:26 AM
Epub capable ebook reader for win7 with commandline options (CLI) Togg Windows Devices 1 04-24-2013 04:07 PM
Regex capturing variables Lonas Sigil 9 08-14-2010 03:05 PM
Noob: just getting my head around all the variables KAM Sony Reader 16 02-04-2009 06:16 PM


All times are GMT -4. The time now is 10:13 AM.


MobileRead.com is a privately owned, operated and funded community.