我的教授编写了这个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脚本。也许有些人可以提供帮助。
答案 0 :(得分:1)
固定。问题出在这里:
nice /usr/bin/time -p $program <
脚本中的。我的计算机在命令之前没有“./”就不执行shell脚本。我的教授计算机必须与众不同。将脚本更改为
nice /usr/bin/time -p ./$program <
完美地完成程序。
我确信我的教授和我都在使用Fedora 8.如果只是输入他们的名字,我会在终端上运行程序会有什么不同?
答案 1 :(得分:0)
我不确定环境的状态(例如:PATH)或文件和权限的状态是什么,但它可能就像序列shell脚本的权限问题一样简单(我认为你'重新说'java EditDistance')。如果你'chmod + x sequence',它有效吗?另一个问题是它可能不在您的路径中,您可以通过键入以下命令来运行序列:'。/ sequence&lt; ecoli2500.txt'?