1

我的教授编写了这个 shell 脚本来计时我的程序,并显示结果。出于某种原因,它只是在我的程序中输出 0。他提供了以下文件:

timeit.csh
sequence
ecoli2500.txt   
ecoli3000.txt    
ecoli5000.txt    
ecoli7000.txt    
ecoli8000.txt    
ecoli9000.txt    
ecoli10000.txt

以下是序列的内容

java EditDistance

timeit.csh 的内容如下。

java EditDistance < ecoli2500.txt 按预期工作

事实上,除了序列之外,上述每个文件都可以完美地执行该程序。

我不明白为什么

./timeit.csh sequence

产生全零

这是timeit.csh ...(下面是EditDistance.java):

#!/bin/csh
# 
# A Unix script to time programs.
#
# Command line: timeit sequence


# the array of programs from the commandline
set program = $argv[1]

# adjust as needed
set CPULIMIT = 120
limit cpu $CPULIMIT seconds
limit core 0

# input files
set input = ( stx1230.txt      \
    ecoli2500.txt    \
    ecoli3000.txt    \
    ecoli5000.txt    \
    ecoli7000.txt    \
    ecoli8000.txt    \
    ecoli9000.txt    \
    ecoli10000.txt)

# adjust as needed
set inputpath = `pwd`

# print header
printf "CPU limit = %d seconds\n\n" $CPULIMIT
printf "%-25s" "Data File"
foreach program ($argv)
printf "%16s" $program
end
printf "\n"

# print right number of = for table
@ i = 25 + 16 * $#argv 
while ($i > 0)
printf "="
@ i = $i - 1
end
printf "\n"


# time it and print out row for each data file and  column for each program
foreach datafile ($input)
printf "%-25s" $datafile
if (-f $inputpath/$datafile) then
    foreach program ($argv)
    # printing running time of program on datafile
    # -p flag with time to ensure its output is measured in seconds and not minutes
    nice /usr/bin/time -p $program <                    \
        $inputpath/$datafile |&                        \
        egrep '^user[ ]*[0-9]' |                       \
        awk '{ if ($2 >= '$CPULIMIT') printf "       CPU limit"; else printf("%16.2f", $2) }'
    # egrep, awk commands extract second column of row corresponding to user time

    end
else printf "could not open" $datafile
endif
printf "\n"

end

这是 EditDistance.java

import java.util.*;

class EditDistance {
    public static int min(int a, int b, int c) {
        return Math.min(a,Math.min(b,c));
    }
    public static int distance(String one, String two) {
        if (one.length()>two.length()) {
            String temp1 = one;
            String temp2 = two;
            one = temp2;
            two = temp1;
        }
        int[][] d = new int[one.length()+1][two.length()+1];
        d[0][0] = 0;
        int top, left, topleft, cost;
        for (int i = 1; i <= one.length(); i++) {
            d[0][i] = 2*i;
            d[i][0] = 2*i;
        }
        for (int i = 1; i <= one.length(); i++) {
            for (int j = 1; j <= two.length(); j++) {

                if (one.charAt(i-1) == two.charAt(j-1))
                    cost = 0;
                else
                    cost = 1;

                top = d[i][j-1];
                left = d[i-1][j];
                topleft = d[i-1][j-1];
                d[i][j] = min(top+2,left+2,topleft+cost);
            }
        }
        return d[one.length()][two.length()];
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String one = scanner.next();
        String two = scanner.next();
        System.out.println(distance(one,two));
    }
}

任何想法为什么事情不工作?我对shell脚本了解不多,但是shell脚本的这一部分:

nice /usr/bin/time -p $program <                    \
    $inputpath/$datafile |&                        \
    egrep '^user[ ]*[0-9]' |                       \
    awk '{ if ($2 >= '$CPULIMIT') printf "       CPU limit"; else printf("%16.2f", $2) }'

在我心中确认我的程序应该期待这个命令:

java EditDistance < ecoli2500.txt
java EditDistance...etc. etc. 

但该程序适用于这些命令。我需要设置我的程序以正确响应 shell 脚本。也许你们中的一些人可以提供帮助。

4

2 回答 2

1

固定的。问题出在这里:

 nice /usr/bin/time -p $program < 

在脚本中。我的计算机不会在命令前没有“./”的情况下执行 shell 脚本。我教授的电脑一定不一样。将脚本更改为

nice /usr/bin/time -p ./$program <

完美运行程序。

我确定我的教授和我都在使用 Fedora 8。让我只需输入他们的名字就可以在终端中运行程序有什么区别?

于 2008-10-14T20:17:30.583 回答
0

我不确定环境的状态(例如:PATH)或文件和权限的状态是什么,但它可能就像序列 shell 脚本的权限问题一样简单(我想你说的是包含'java EditDistance')。如果你'chmod +x 序列',它有效吗?另一个问题是它可能不在您的路径中,您可以通过键入:'./sequence < ecoli2500.txt' 来运行序列吗?

于 2008-10-14T00:36:15.337 回答