Home > other >  How do I deal with unusual filenames in a command's parameters?
How do I deal with unusual filenames in a command's parameters?

Time:08-11

I have a file ('list') which contains a large list of filenames, with all kinds of character combinations such as:

-sensei

I am using the following script to process this list of files:

#!/bin/bash
while read -r line
do
    html2text -o ./text/$line $line
done < list

Which is giving me 'Cannot open input file' errors. What is the correct way of dealing with these filenames, to prevent any errors?

I have changed the example list above to now include only one filename (out of many) which does not work, no matter how I quote or don't quote it.

#!/bin/bash
while read -r line
do
    html2text -o "./text/$line" "$line"
done < list

The error I get is:

Unrecognized command line option "-sensei", try "-help".

As such this question does not resolve this issue.

CodePudding user response:

Something like this should fix your issues (unless the file list has CRLF line endings):

while IFS='' read -r file
do
    html2text -o ./text/"$file" -- "$file"
done < filelist.txt

notes:

  • IFS='' read -r is mandatory when you want to capture a line accurately
  • most commands support -- for signaling the end of options; whatever the following arguments might be, they will not be treated as options. BTW, an other common work-around for filenames that start with - is to prepend ./ to them.

CodePudding user response:

Are you sure the files are present? Usually there should not be a problem with your script. For example:

#!/bin/bash
while read -r line
do
    touch ./text/$line
done < $1
ls -l /tmp/text

works perfectly fine for me (with your example input). Bash should escape them for you. Are you in the right directory? If you are sure the files are present, there is a problem with html2text.

Also make sure not to have a trailing blank line in your input file.

  • Related