CSC352: Using Bash Scripts to Measure Program Execution Time

From dftwiki3
Revision as of 13:41, 11 September 2013 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- <bluebox> This page illustrates how to use a Bash script to run a Java program multiple times while passing it a different parameter every time. </bluebox> <br />...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 13:41, 11 September 2013 (EDT)


This page illustrates how to use a Bash script to run a Java program multiple times while passing it a different parameter every time.


A simple Java Program


Note that the program gets the value of the variable N from the command line.

class PrintN {
 
     public static void main( String[] args ) {
 
         int N = Integer.parseInt( args[0] );
         System.out.println( "I got " + N );
     }
 
 }


Running the program 10 times from the command line

  • Running the program above 10 times can be done as follows, from the command line:
   javac PrintN.java
   for i in 1 2 3 4 5 6 7 8 9 10 ; do
      java PrintN $i
   done

Running the program from a Bash script

The script is shown below:

#! /bin/bash
# runPrintN.sh
# 
javac PrintN.java
for i in 1 2 3 4 5 6 7 8 9 10 ; do
    java PrintN $i
done


Make sure you make the script executable before you call it:


 chmod a+x runPrintN.sh


You can now run the program 10 times with just one command:

 ./runPrintN.sh