Search code examples
bashloopstype-conversionkml

loop over files with bash script


I have a js script that converts kml location history files to csv. I wrote a bash script to loop through all the files in a directory. The script works when I execute it from command line ./index.js filename.kml > filename.csv

But nothing happens when I execute the bash file that is supposed to loop through all files.

I know it probably is a simple mistake but I can't spot it.

#!/bin/bash
# file: foo.sh
for f in *.kml; do
  test -e "${f%.kml}" && continue
  ./index.js "$f" > "-fcsv"
done

Solution

  • Just delete the "&& continue", if I'm not wrong you're skipping the current iteration with the "continue" keyword, that's why nothing happens

    EDIT

    Also, you shouldn't test if the file exists, the for loop is enough to be sure that "f" will be a valid .kml file. Anyways, if you still want to do it you have to do it like:

    #!/bin/bash
    # file: foo.sh
    for f in *.kml; do
      if [ -e "$f" ]; then
      ./index.js "$f" > "$f.csv"
      fi;
    done