简体   繁体   English

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

[英]Removing forks in a shell script so that it runs well in Cygwin

I'm trying to run a shell script on windows in Cygwin. 我正在尝试在Cygwin的Windows上运行shell脚本。 The problem I'm having is that it runs extremely slowly in the following section of code. 我遇到的问题是它在下面的代码段中运行得非常慢。 From a bit of googling, I believe its due to there being a large amount of fork() calls within the script and as windows has to use Cygwins emulation of this, it just slows to a crawl. 从一点谷歌搜索,我相信它是由于脚本中有大量的fork()调用,并且由于Windows必须使用Cygwins仿真,它只是慢慢爬行。

A typical scenario would be in Linux, the script would complete in < 10 seconds (depending on file size) but in Windows on Cygin for the same file it would take nearly 10 minutes..... 一个典型的场景是在Linux中,脚本将在<10秒内完成(取决于文件大小),但在Windows上的Cygin中,对于相同的文件,它将花费将近10分钟.....

So the question is, how can i remove some of these forks and still have the script return the same output. 所以问题是,如何删除其中一些分支仍然让脚本返回相同的输出。 I'm not expecting miracles but I'd like to cut that 10 minute wait time down a fair bit. 我并不期待奇迹,但我想将这10分钟的等待时间缩短一点。

Thanks. 谢谢。

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
}

Update : Changed the regex a bit and used a bit more perl: 更新 :更改了正则表达式并使用了更多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
}

So, using the above code, the run time was reduced from about 8 minutes to 2.5 minutes. 因此,使用上面的代码,运行时间从大约8分钟减少到2.5分钟。 Quite an improvement. 相当不错。

If anybody can suggest any other changes I would appreciate it. 如果有人可以提出任何其他变化,我将不胜感激。

Put more commands into one perl script, eg 将更多命令放入一个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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM