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

时间:2015-07-09 08:25:08

标签: 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不起作用?

1 个答案:

答案 0 :(得分:1)

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

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

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

您可以通过从stdin明确重定向/dev/null来查看是否遇到过相同的情况,如下所示:

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
相关问题