使用变量的Bash命令

时间:2014-05-09 14:47:41

标签: bash shell command-line terminal sh

我想用程序批量隐藏目录中的文件。 除了新扩展之外,我希望新闻文件保留旧名称。 为了说明,单个转换将如下所示:

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

(意思是program options out-file in-file

现在,我想对目录中的所有.shp文件执行此操作,以获取.json文件。 如何创建这样的bash脚本?

我已经做过了

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

但它没有用。为什么呢?

1 个答案:

答案 0 :(得分:3)

使用参数扩展从文件名中删除扩展名:

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

保存到ogr.sh,制作可执行文件,然后使用

进行调用
ogr.sh /path/to/dir