linux - 为什么 svn2git 导致循环在 bash 脚本中中断?

标签 linux bash svn2git

我正在尝试使用 svn2git 将一些模块从 SVN 迁移到 GIT。我在 .csv 文件中有一个模块列表,如下所示:

pl.com.neokartgis.i18n;pl.com.neokartgis.i18n;test-gis;svniop
pl.com.neokartgis.cfg;pl.com.neokartgis.cfg;test-gis;svniop
pl.com.neokart.db;pl.com.neokart.db;test-gis;svniop

我想将每个模块迁移到单独的 GIT 存储库。我尝试了以下脚本,该脚本从 .csv 文件中读取模块列表并循环导入每个模块:

#!/bin/bash

LIST=$1
SVN_PATH=svn://svn.server/path/to/root
DIR=`pwd`

function importToGitModule {
    cd $DIR

    rm -rf /bigtmp/svn2git/repo
    mkdir /bigtmp/svn2git/repo
    cd /bigtmp/svn2git/repo
    svn2git --verbose $SVN_PATH/$1  
    #some other stuff with imported repository
}

cat $LIST | gawk -F";" '{ print $2; }' | while read module_to_import
do
    echo "before import $module_to_import"
    importToGitModule "$module_to_import";
done;

问题是脚本在第一次迭代后结束。但是,如果我删除对 svn2git 的调用,脚本将按预期工作并为文件中的每个模块打印消息。

我的问题是:为什么这个脚本在第一次迭代后结束,我如何更改它以循环导入所有模块?

编辑:

以下循环版本可以正常工作:

for module_to_import in `cat $LIST | gawk -F";" '{ print $2; }'`
do
    echo "before import $module_to_import"
    importToGitModule "$module_to_import";
done;

那么为什么 while read 不起作用?

最佳答案

我怀疑你的循环中的某些东西——可能是 svn2git 进程的一部分——正在消耗 stdin。考虑这样一个循环:

ls /etc | while read file; do
    echo "filename: $file"
    cat > /tmp/data
done

无论/etc 中有多少文件,这个循环只会运行一次。此循环中的 cat 将消耗 stdin 上的所有其他输入。

你可以通过从/dev/null显式重定向stdin来查看你是否遇到过同样的情况,就像这样:

cat $LIST | gawk -F";" '{ print $2; }' | while read module_to_import
do
    echo "before import $module_to_import"
    importToGitModule "$module_to_import" < /dev/null
done

关于linux - 为什么 svn2git 导致循环在 bash 脚本中中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312030/

相关文章:

linux - 如何在 Linux 上制作文档集?

linux - 让用户验证密码

linux - Cassandra 全局快照

c - 位移 0 的目的是什么

bash - 如何从 stdin 读取多行输入到变量中以及如何在 shell(sh,bash) 中打印出一个?

java - Bash 脚本无法将 Java 程序作为 cron 作业运行

git - 如何将平面 svn 存储库迁移到 git 存储库

git2svn 无法创建符号链接(symbolic link)(文件名太长)

linux - 如何验证字符串是否为字母数字组合(包含字母和数字)#linux #Bash