Name:
PID: (page 1)
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 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
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
files=`find written_2`
for file in $files
do
if [[ -f $file ]] && [[ $file == *Bahamas* ]]
then
wc -l $file
fi
done
bahamas-files.sh
$ ls written_2/
non-fiction travel_guides
$ bash bahamas-files.sh
169 written_2/travel_guides/berlitz2/Bahamas-WhereToGo.txt
24 written_2/travel_guides/berlitz2/Bahamas-Intro.txt
68 written_2/travel_guides/berlitz2/Bahamas-WhatToDo.txt
43 written_2/travel_guides/berlitz2/Bahamas-History.txt
$ javac AgeCalc.java
$ java AgeCalc
1987/6/22
You're 35 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. Which expectation below is incorrect?
AgeCalc.java
check.sh
test-files/
test1.txt "1987/6/22"
test1.txt.expect "You're 35 yrs old."
test2.txt "2024/7/12"
test2.txt.expect "You don't exist yet."
test3.txt "2023/12/1"
test3.txt.expect "You're 0 yrs old."
...
set -e
javac AgeCalc.java
for _____________________________
done
Write a bash script that will run the program on all the test files. Challenge: print an error message if it doesn't match the expectation.
Name:
PID: (page 2)
A process is what our computer keeps track of for each command while it is running. This includes:
When a process ends, it has a return code or exit code. The rule is that 0 means success and non-0 means error.
In bash, the exit code of the last process that ran is stored in the special variable: $? (yes, that's a dollar sign then a question mark)
set -e
javac ExitCode.java
java ExitCode 0
echo $?
java ExitCode 1
echo $?
java ExitCode 3
echo $?
class ExitCode {
public static void main(String[] args) {
System.out.println("Exiting with code " + args[0]);
System.exit(Integer.parseInt(args[0]));
}
}
run-exit-code.sh
ExitCode.java
bash-3.2$ bash run-exit-code.sh
# what's the output? why?
bash-3.2$ ls does-not-exist.txt
ls: does-not-exist: No such file or directory
bash-3.2$ echo $? # fill in a guess/the result below
_________________________________
bash-3.2$ cat BadFile.java
class Bad {
private statik void mane(Strong[] orgs) {}
}
bash-3.2$ javac BadFile.java
BadFile.java:2: error: <identifier> expected
private statik void mane(Strong[] orgs) {}
^
1 error
bash-3.2$ echo $?
1
bash-3.2$ echo $? # fill in a guess/the result below
________________________________
bash-3.2$ cat GoodFile.java
class Main {
public static void main(String[] args) { }
}
bash-3.2$ javac GoodFile.java
bash-3.2$ echo $? $ fill in a guess/the result below
________________________________
Where might it be useful to use or check an exit code?
What happens when you submit a PA in CSE12?