Firstly realize that the shell will expand *.pdf
to a list of arguments. This means that your shell script will never ever see the *
. Instead it will get a list of arguments.
You can use a construction like the following:
#!/bin/bash
function convert() {
local filename=$1
# do your thing here
}
if (( $# < 1 )); then
# give your error message about missing arguments
fi
while (( $# > 0 )); do
convert "$1"
shift
done
What this does is first wrap your functionality in a function called convert
. Then for the main code it first checks the number of arguments passed to the script, if this is less than 1 (i.e. none) you give the error that a filename should be passed. Then you go into a while loop which is executed as long as there are arguments remaining. The first argument you pass to the convert function which does what your script already does. Then the shift
operation is performed, what this does is it throws away the first argument and then shifts all the remaining arguments "left" by one place, that is what was $2
now is $1
, what was $3
now is $2
, etc. By doing this in the while loop until the argument list is empty you go through all the arguments.
By the way, your initial assignments have a few issues:
- you can't assume that the filename has an extension, your code could match a dot in some directory path instead.
- your directory assignment seems to be splitting on . instead of /
- your directory assignment will contain the filename if no absolute or relative path was given, i.e. only a bare filename
- ...
I think you should spend a bit more time on robustness