Home > Blockchain >  How to pass list of quotated file names as a parameter to another script in Bash?
How to pass list of quotated file names as a parameter to another script in Bash?

Time:11-29

I am stuck with passing list of quoted file names that contain a space in their names to pdfunite in my script. It works in shell but not in my bash script.

Prove of concept

This way I collect all the file names of given pattern encapsulated with double quotation ":

# Collect quoted file names 
$ ls -x -Q "file"*.pdf
"file 01.pdf" "file 02.pdf" "file 03.pdf"

# Manually passed params in shell following the syntax: pdfunite <PDF-sourcefile-1>..<PDF-sourcefile-n> <PDF-destfile>
$ pdfunite "file 01.pdf"  "file 02.pdf" "file output.pdf"

# File is successfully created
$ ls "file output.pdf"
'file output.pdf'

My script

In my script I tried to collect the list of files in various ways but nothing works

# first approach - single line
pdfunite $(ls -x -Q "file"*.pdf) "file output.pdf"

# second approach - using variable
filesin=`ls -x -Q "file"*.pdf) "file output.pdf"`
pdfunite $filesin "file output.pdf"

Error output from my script

Both approaches above fails with following message from the pdfunite:

pdfunite '"file' '01.pdf"' '"file' '02.pdf"' 'file output' \
  I/O Error: Couldn't open file '"file': No such file or directory.

So what is the trick to pass the list of file names encapsulated with the quotation?

CodePudding user response:

pdfunit file*.pdf 'file output.pdf'

or

pdfunite 'file '*.pdf 'file output.pdf'

Note that if file output.pdf exists it will be in the glob list, so it should not exist.

No word splitting occurs in a glob expansion, but it does for (an unquoted) command substitution. Parsing or otherwise using ls output in a script is usually a mistake. Glob expansion and find are better alternatives.

  • Related