简体   繁体   中英

bash single quote escaping is driving me mad

I'm trying to use gdalsrsinfo to get the proj4 string from a .shp and then pass it to ogr2ogr for use in a batch reprojection loop. It's close to working, but the single quotes are getting passed to the ogr2ogr command and I can't figure out how to strip them off:

My script:

        #!/bin/bash
for f in *.shp; do 
    projsrs=$(gdalsrsinfo -o proj4 $f)
    ogr2ogr -f "ESRI Shapefile" -s_srs "$projsrs" -t_srs EPSG:3857 ${f%}3857.shp $f
done

running the gdalsrsinfo command by itself returns:

username:shpfrm username$ gdalsrsinfo -o proj4 filename.shp
'+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '

When I use bash -x to test the output, I see that ''\\' is at the beginning of the string and \\''' is on the end.

+ for f in '*.shp'
++ gdalsrsinfo -o proj4 filename.shp
+ projsrs=''\''+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '\'''
+ ogr2ogr -f 'ESRI Shapefile' -s_srs ''\''+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '\''' -t_srs EPSG:3857 filename.shp3857.shp PC_Sec05_Frm64.shp
Failed to process SRS definition: '+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '

what I need is:

ogr2ogr -f 'ESRI Shapefile' -s_srs '+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs ' -t_srs EPSG:3857 filename.shp3857.shp filename.shp

You haven't shown the output from the command, but if it does have stray quotes that you don't want, you could just remove them. Reference: http://wiki.bash-hackers.org/syntax/pe#search_and_replace

#!/bin/bash
for f in *.shp; do 
    projsrs=$(gdalsrsinfo -o proj4 "$f")
    ogr2ogr -f "ESRI Shapefile" -s_srs "${projsrs//\'/}" -t_srs "EPSG:3857" "${f}3857.shp" "$f"
done

Also, you should always quote filenames, as they can contain spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM