There are a number of things wrong with the script, that are obvious for the more experienced scripter. But your question was "how do I debug".
First of all: write the script in a legible form instead of doing a one-liner:
#!/bin/bash
for i in $(ls $*) ; do
x=$(echo $(basename $i".md"))
pandoc $i -t "latex" -o $x.pdf
done
You will notice that I added a #!/bin/bash
. That forces the use of bash
for the script. So, how to debug this?
$i
is the loop variable; does it contain what you think when you go through the loop? An echo will make that clear. You could do the same for your $x:
#!/bin/bash
for i in $(ls $*) ; do
echo "LOOP VARIABLE $i"
x=$(echo $(basename $i".md"))
echo "AND X IS $x"
pandoc $i -t "latex" -o $x.pdf
done
That is a basic way of debugging.
So, now back to your script, output etc. Am I correct that you have a file called "test de bye.md
" in your directory?
OK. Start at the beginning (actually: the missing @!
was that but that is nitpicking). Never parse the output of ls
. I have been chastised many times for it and here you can read why.
You may also be interested in the difference between $*
and $@
, which I will leave to your google skills.
The construction x=$(echo $(basename $i".md"))
is bizar. I do not even understand what you are trying accomplish here, especially if you are already calling the script with the .md
at the end of the filename.
You may also want to quote arguments that have spaces in them. For example: if $i would contain test de bye.md
, ls $i
would call `ls with 3 arguments:
while ls "$i"
would cal ls
with one argument:
which will have completely different results.
Another question: are you sure that pandoc
is installed on your system? The error message suggests it is not. Verify with which pandoc
.
For those who did Fortran (and I am that old!), i is an integer. But you might as well use descriptive names, which will make maintenance of scripts easier late on.
So, that would make the script:
#!/bin/bash
for infile in "$@" ; do
outfile=${infile%.md}.pdf
pandoc "$infile" -t latex -o "$outfile"
done
Or something like that.