【问题标题】:Piping SQL to mysql via -find -exec通过 -find -exec 将 SQL 连接到 mysql
【发布时间】:2009-09-03 21:24:48
【问题描述】:

我这辈子都得不到这个。失败的例子,最后一行加上sed,就是最终目的:

find -maxdepth 1 -name "*.sql" -exec "mysql -D ootp < {}" \;
find -maxdepth 1 -name "*.sql" -exec "mysql -D ootp << {}" \;
find . -maxdepth 1 -name \"*.sql\" -exec /usr/bin/mysql -D ootp << sed -e 's/ , );/1,1);/g' {}

什么给了?

【问题讨论】:

  • 我不明白你的问题:(

标签: mysql bash shell find exec


【解决方案1】:

我会将以上所有内容重写为以下变体之一:

find -maxdepth 1 -name '*.sql' -exec sed -e 's/ , );/1,1);/g' '{}' + \ | mysql -D ootp 查找 -maxdepth 1 -name '*.sql' -print0 \; \ | xargs -0 sed -e 's/ , );/1,1);/g' \ | mysql -D ootp 查找 -maxdepth 1 -name '*.sql' -exec cat '{}' \; \ | sed -e 's/ , );/1,1);/g' \ | mysql -D ootp find -maxdepth 1 -name '*.sql' -exec sed -e 's/ , );/1,1);/g' '{}' \; \ | mysql -D ootp

第一个变体需要 GNU findutils 4.2.12、SRV4 派生的 find 或其他一些与 POSIX 兼容的 find,应该是最有效的,但可能不适用于所有 find 实现(值得注意的是,我相信 BSD find 错过了这个功能。

【讨论】:

    【解决方案2】:

    here document”,但您使用它是因为它是文件名和/或流。

    听起来像你想要的

    找到 . -maxdepth 1 -name '*.sql' -exec "sed -e 's/ , );/1,1);/g' '{}' | /usr/bin/mysql -D ootp" \;

    如果你使用 -maxdepth 1 也许你可以这样做

    for i in *.sql ; do 
      sed -e 's/ , );/1,1);/g' "$i" | /usr/bin/mysql -D ootp
    done
    

    【讨论】:

      【解决方案3】:

      我试过这样的东西,它对我有用:

      find -maxdepth 1 -name "*.sql" -exec sh -c "mysql -D ootp < {}" \;
      

      【讨论】:

        猜你喜欢
        • 2014-06-11
        • 2019-09-08
        • 2018-06-03
        • 2021-06-01
        • 1970-01-01
        • 2019-08-31
        • 2021-08-07
        • 2012-02-12
        • 2014-06-17
        相关资源
        最近更新 更多