1 of 2

Name:

PID: (page 1)

import java.util.Scanner;

class Read {

public static void main(String[] args) {

System.out.print("What's your name? ");

Scanner in;

in = new Scanner(System.in);

String s = in.nextLine();

System.out.println("Hello " + s);

}

}

​

​

$ java Read

What's your name? JoeTyped

Hello JoeTyped

$ cat name.txt

JoeFromFile

$ java Read < name.txt

​

​

​

​

​

​

Read.java

«command» < «file» Input redirection – use contents of «file» as the standard input to the command. That's what you would type!

​

​

bash has for statements! They can iterate over many things, including lists of paths or lines of output.

​

​

for VAR in <sequence>

do

<commands; $VAR is bound to an element of sequence>

done

$ javac AgeCalc.java

$ java AgeCalc

1987/6/22

You're 36 yrs old.

$ java AgeCalc

2024/7/12

You don't exist yet.

Assume we have a program with the following behavior.

Underlined text is typed at standard input

Assume we have a directory of files like below, where the contents of each file is in quotes next to it.

AgeCalc.java

check.sh

test-files/

test1.txt "1987/6/22"

test1.txt.expect "You're 36 yrs old."

test2.txt "2024/7/12"

test2.txt.expect "You don't exist yet."

test3.txt "2023/12/5"

test3.txt.expect "You're 0 yrs old."

...

set -e

​

javac AgeCalc.java

​

for _____________________________

​

​

​

​

​

​

​

done

​

Let’s write a bash script that will run the program on all the test files.

2 of 2

Name:

PID: (page 2)

bash has if statements! elif/else are optional, and there can be multiple elif clauses.

​

if <condition>

then

<commands>

elif <condition>

then

<commands>

else

<commands>

fi

​

Bash has variables, which are declared with

​

NAME=value

​

Bash also lets you store the output of a program in a variable, with:

​

NAME=$(some-command with some argos)

​

Bash also lets you store the output of a program in a file, with:

​

command > file

​

This last example is called output redirection.

Some common <condition>s (iff means "if and only if"):

​

[[ -e <val> ]] – true iff the path val exists

[[ -f <val> ]] – true iff the path val exists and is a file

​

[[ ______ <val> ]] – true iff the path val exists and is a directory

​

[[ <val1> -eq <val2> ]] – true iff values arithmetically equal

[[ <val1> -ne <val2> ]] – false iff values arithmetically equal

[[ <val1> -gt <val2> ]] – true iff <val1> is greater than <val2>

​

[[ <val1> ______ <val2> ]] – true iff <val1> is less than <val2>

​

[[ <val1> == <pattern> ]] – true iff val1 matches the pattern

​

A <val> here could be a use of a bash variable (like $SOMEVAR), or constant string values like "0" or "1", or a written out path like data-dir/. Generally think of these all as being string values.

​

A <pattern> here could be a constant string, or something using * like *Bahamas*.

​

​

set -e

​

javac AgeCalc.java

​

for _____________________________

​

​

​

​

​

​

​

​

​

​

​

​

​

done

​

Let’s update the AgeCalc script to print out whether each test matched its expectation