-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
92 lines (65 loc) · 1.75 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
clear;
# Reset arguments
CHAPTER_INPUT=$1;
EXERCISE_INPUT=$2;
FLOOR=1;
TRAIN=0;
# Check if training
if [ -n "$CHAPTER_INPUT" ] && [ "$CHAPTER_INPUT" = "train" ]; then
TRAIN=1;
CHAPTER_INPUT="random";
fi;
# Functions
function RANDOM_SMALLER_THAN()
{
FLOOR=1;
RANGE=$(($CEILING-$FLOOR+1));
RESULT=$RANDOM;
let "RESULT %= $RANGE";
RESULT=$(($RESULT+$FLOOR));
echo $RESULT;
}
# Clear classes
rm src/chapter*/*.class 2> /dev/null;
# Set variables
CHAPTERS=$(( $(ls -l ./src | wc -l) - 2));
# Check for random chapter
if [ -z "$CHAPTER_INPUT" ] || [ "$CHAPTER_INPUT" = "random" ]; then
CEILING=$CHAPTERS;
CHAPTER_INPUT=$(RANDOM_SMALLER_THAN);
fi;
#Set variables
CEILING=$(( $(ls -l "./src/chapter$CHAPTER_INPUT" | wc -l) - 2));
# Check for random exercise
if [ -z "$EXERCISE_INPUT" ] || [ "$EXERCISE_INPUT" = "random" ]; then
EXERCISE_INPUT=$(RANDOM_SMALLER_THAN);
fi;
# Parse input
CHAPTER="chapter$CHAPTER_INPUT";
EXERCISE="Exercise$EXERCISE_INPUT";
FULL_PATH="src/$CHAPTER/$EXERCISE";
# Compile and run
if [ "$TRAIN" = "0" ]; then
echo "Compiling $FULL_PATH" && \
javac "$FULL_PATH.java" && \
echo "Running $FULL_PATH" && echo "" && \
java "$FULL_PATH";
else
cat "$FULL_PATH.java";
echo "";
echo "";
read -s -n 1 -p "Look at the code, and think about what will happen. Press any key to see the output.";
echo "";
echo "";
echo "Compiling $FULL_PATH" && \
javac "$FULL_PATH.java" && \
echo "Running $FULL_PATH" && echo "" && \
java "$FULL_PATH";
echo "";
echo "";
read -s -n 1 -p "Was this what you expected? Press any key to proceed to the next exercise.";
bash ./run.sh train;
fi;
# Clear classes again
rm src/chapter*/*.class 2> /dev/null;