1

My professor wrote this shell script to time my program, and display the results. For some reason it just outputs 0s with my program. He provided the following files:

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

Here are the contents of sequence

java EditDistance

The contents of timeit.csh are further below.

java EditDistance < ecoli2500.txt works as expected

In fact the program executes flawlessly with each of the above files other than sequence.

What I don't understand is why

./timeit.csh sequence

produces all zeros

Here is timeit.csh... (further below is 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

Here is 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));
    }
}

Any Ideas why things aren't working? I don't know much about shell scripts, but this section of the shell script:

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

confirms in my mind that my program should be expecting this command:

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

but the program works with those commands. I need to set up my program to respond correctly to the shell script. Maybe some of you can help.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254

2 Answers2

1

Fixed. The problem was here:

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

in the script. My computer doesn't execute shell scripts without a "./" before the command. My professors computer must be different. Changing the script to

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

Ran the program perfectly.

I know for certain that my professor and I are both using Fedora 8. What would be the difference that would let me run programs in the terminal simply by typing their name?

objectivesea
  • 597
  • 1
  • 6
  • 22
0

I'm not sure what the state of the environment (eg: PATH) or the state of the files and permissions are, but it could be as simple as a permissions problem with the sequence shell script (which I think you're saying contains 'java EditDistance'). If you 'chmod +x sequence', does it work? The other issue is that it may not be in your path, can you run sequence by typing: './sequence < ecoli2500.txt'?

Kyle Burton
  • 26,788
  • 9
  • 50
  • 60
  • ./sequence < ecoli2500.txt worked fine. Also, I did chmod 777 *, and still had the same issue. All files are in the same directory. –  Oct 14 '08 at 00:46
  • Also, you are correct: the contents of sequence is "java EditDistance" –  Oct 14 '08 at 00:49