0

I want to batch covert files in a directory with a program. I want news files to keep old names, apart from the new extension. To illustrate, a single conversion would go like this:

ogr2ogr -f "GeoJSON" world_borders.json world_borders.shp

(meaning, program options out-file in-file)

Now, I want to do this with all .shp files in a directory, to get .json files. How do I create a bash script like this?

I already did

for file in *.shp ; 
do ogr2ogr -f "GeoJSON" "${file}" "${file}".json; 
done

But it didnt work. Why?

1 Answer 1

3

Use parameter expansion to remove the extension from a file name:

#! /bin/bash
dir=$1
for file in "$dir"/*.shp ; do
    ogr2ogr -f GeoJSON "${file%.shp}".json "$file"
done

Save to ogr.sh, make executable, call with

ogr.sh /path/to/dir
Sign up to request clarification or add additional context in comments.

9 Comments

I added a comment of what I did and it didnt work. Would you care to comment on that?
@toninoj: In your first example, it's output input. In your update, it's input output.
@toninoj Also, what you did is not what choroba suggested.
@toninoj: It seems you didn't provide the dir argument.
@toninoj: You have to add code to do that. You can check the source - there's nothing special about $1 being empty. Or, just run it in the correct dir with ogr.sh ..
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.