0

I have a shell script that converts a .md file to .pdf using pandoc.

When executing the script for example:

./script *file.md

it doesn't detect the files needed to be converted because they resemble this:

-first-file.md

second _ file.md

First one starts with a dash. Second one had an underscore in between spaces in the center.

  • 4
    You need to include the part of your script that fails with those filenames. The first example can be a bit more difficult, but the second filename is easily handled with proper quoting. – jordanm Dec 01 '18 at 17:42
  • 1
    oh I actually solved it. a simple ./ was needed. – Jan Villapaz Dec 01 '18 at 19:14

1 Answers1

4

It's likely that the script uses the filename of the file in such a way that its name is being interpreted as a set of options to some utility, as in

rm "$filename"

when $filename starts with a dash.

There are two ways of making sure that the command executes correctly whenever a pathname may start with a dash:

  1. Make sure that the pathname does not start with a dash, for example by including an absolute or relative path. If a file in the current directory is to be removed with rm "$filename", then make sure that $filename starts with ./. This is usually matter of changing a loop from

    for filename in some-pattern; do
    

    into

    for filename in ./some-pattern; do
    
  2. Use -- to signal the end of command line options to the utility, as in

    rm -- "$filename"
    

    The parsing of options on the command line will stop at the -- and all the later arguments will be taken as operands to the utility rather than as possible options. This is a good solution when you are receiving a pathname from the user on the command line and you don't know whether it's given with a path or not. Note that you can't just prepend ./ to the given pathname as that would make the pathname relative to the current directory, even though the user may have given an absolute path.

Related to filenames containing spaces etc.:

Kusalananda
  • 333,661