在shell脚本中删除分叉,以便它在Cygwin中运行良好

时间:2013-05-29 10:06:37

标签: linux windows shell cygwin fork

我正在尝试在Cygwin的Windows上运行shell脚本。我遇到的问题是它在下面的代码段中运行得非常慢。从一点谷歌搜索,我相信它是由于脚本中有大量的fork()调用,并且由于Windows必须使用Cygwins模拟这一点,它只会慢下来爬行。

典型情况是在Linux中,脚本将在< 10秒(取决于文件大小),但在Windows上的Cygin上,对于同一个文件,它需要将近10分钟.....

所以问题是,我如何删除其中的一些分支仍然让脚本返回相同的输出。我不期待奇迹,但我想将这10分钟的等待时间缩短一点。

感谢。

check_for_customization(){
  filename="$1"    
  extended_class_file="$2"
  grep "extends" "$filename" | grep "class" | grep -v -e '^\s*<!--' | while read line; do 
    classname="$(echo $line | perl -pe 's{^.*class\s*([^\s]+).*}{$1}')"
    extended_classname="$(echo $line | perl -pe 's{^.*extends\s*([^\s]+).*}{$1}')"

    case "$classname" in
    *"$extended_classname"*) echo "$filename"; echo "$extended_classname |$classname | $filename" >> "$extended_class_file";;
    esac
  done
}

更新:更改了正则表达式并使用了更多perl:

check_for_customization(){
  filename="$1"    
  extended_class_file="$2"
  grep "^\(class\|\(.*\s\)*class\)\s.*\sextends\s\S*\(.*$\)" "$filename" | grep -v -e '^\s*<!--' | perl -pe 's{^.*class\s*([^\s]+).*extends\s*([^\s]+).*}{$1 $2}' | while read classname extended_classname; do
    case "$classname" in
    *"$extended_classname"*) echo  "$filename"; echo "$extended_classname | $classname | $filename" >> "$extended_class_file";;
    esac
  done
}

因此,使用上面的代码,运行时间从大约8分钟减少到2.5分钟。相当不错。

如果有人可以提出任何其他更改我会很感激。

1 个答案:

答案 0 :(得分:3)

将更多命令放入一个perl脚本中,例如:克。

check_for_customization(){
  filename="$1" extended_class_file="$2" perl -n - "$1" <<\EOF
next if /^\s*<!--/;
next unless /^.*class\s*([^\s]+).*/; $classname = $1;
next unless /^.*extends\s*([^\s]+).*/; $extended_classname = $1;
if (index($extended_classname, $classname) != -1)
{
    print "$ENV{filename}\n";
    open FILEOUT, ">>$ENV{extended_class_file}";
    print FILEOUT "$extended_classname |$classname | $ENV{filename}\n"
}
EOF
}