Search code examples
linuxbashshelldjvu

linux shell script using djvused program not working


I want to write a script that renames the pages of a DJVU file recursively. From http://djvu.sourceforge.net/doc/man/djvused.html I know that the command to do that on a single page (say call page 5 of the bundle with name 6) is

djvused file.djvu -e 'select 5; set-page-title 6; save'

But then I have troubles when trying to do it recursively, namely running the script

./for.sh

which is given by

for (( i=12; i<=823; i++ ))
do
 djvused file.djvu -e 'select $i; set-page-title $((i-10)); save'
done

The error I get is due to the program being unable to understand $i as a number, instead of an expression. I think this is related to my small knowledge of shell scripts and djvused.

Could anyone tell me how to modify it so that it works?


Solution

  • You should quote it properly with double-quotes. Using single quotes would not allow variables to be expanded and remain as literal form.

    for (( i=12; i<=823; i++ ))
    do
     djvused file.djvu -e "select $i; set-page-title $((i-10)); save"
    done
    

    As noted about single quotes:

    Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

    Whereas on double quotes:

    Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘’ retain their special meaning within double quotes