cygwin - bash shell script error works on command line, not in script -


when running shell script via cygwin64, getting error (output below). relevant portion of script follows. interesting if copy , paste echoed command, runs without complaint. so, failing properly?

[worldwidewilly@sal9000 resources]$ makebook mybook generating dblatex pdf output via a2x a2x -v -f pdf -l --asciidoc-opts='-a lang=en -v -b docbook -d book' --dblatex-opts='-v -t db2latex' mybook.asciidoc usage: a2x [options] source_file  a2x: error: option -d: invalid choice: "book'" (choose 'article', 'manpage',   'book') done. 

here script logic:

asciidoc_opts="--asciidoc-opts='-a lang=en -v -b docbook -d book'" dblatex_opts="--dblatex-opts='-v -t db2latex'" echo "generating dblatex pdf output via a2x" cmd="a2x -v -f pdf -l ${asciidoc_opts} ${dblatex_opts} $1.asciidoc" echo $cmd $cmd echo "done." 

the script has been saved utf-8 *nix file endings. fresh install of cygwin64 running on windows 7.

fwiw - have of workaround. if add space after word book , before single apostrophe, gets error above. however, -t in dblatex_opts flagged in error.

[worldwidewilly@sal9000 resources]$ makebook mybook generating dblatex pdf output via a2x a2x -v -f pdf -l --asciidoc-opts='-a lang=en -v -b docbook -d book ' --dblatex-opts='-v -t db2latex' mybook.asciidoc usage: a2x [options] source_file a2x: error: no such option: -t done. 

and, again, if copy echoed command , run command line, works. confusing.

variables supposed contain data, , bash treats them data. means shell meta-characters quotes treated data.

see this article complete discussion on topic.

the short answer use arrays instead:

asciidoc_opts=( --asciidoc-opts='-a lang=en -v -b docbook -d book' ) dblatex_opts=( --dblatex-opts='-v -t db2latex' ) cmd=(a2x -v -f pdf -l "${asciidoc_opts[@]}" "${dblatex_opts[@]}" "$1".asciidoc) # print command in pastable format: printf '%q ' "${cmd[@]}" printf '\n' # execute "${cmd[@]}" 

make sure not use eval:

eval "$cmd" #noooo 

this appear work code posted it, has caveats , security problems.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -