【问题标题】:Bash command using variables使用变量的 Bash 命令
【发布时间】:2014-06-27 08:51:52
【问题描述】:

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

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

但它没有工作。为什么?

【问题讨论】:

    标签: bash shell command-line terminal sh


    【解决方案1】:

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

    #! /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
    

    【讨论】:

    • 我添加了我所做的评论,但它没有工作。您愿意对此发表评论吗?
    • @toninoj:在您的第一个示例中,它是output input。在您的更新中,它是input output。
    • @toninoj 另外,你所做的并不是 choroba 建议的。
    • @toninoj:看来你没有提供dir 参数。
    • @toninoj:您必须添加代码才能做到这一点。您可以查看来源 - 1 美元为空并没有什么特别之处。或者,只需使用 ogr.sh . 在正确的目录中运行它。
    猜你喜欢
    • 2012-03-02
    • 2023-04-08
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多