forked from deeplearning4j/deeplearning4j-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runexamples.sh
executable file
·145 lines (124 loc) · 3.06 KB
/
runexamples.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
# Run one or all of the examples.
# Use runexamples.sh -h to see the options.
help() {
[ -n "$1" ] && echo $1
cat <<-EOF1
usage: $0 [-h|--help] [-a|-all] [-n|--no-pauses]
where:
-h|--help Show help and quit
-a|--all Run all the examples. Default is to prompt for which one to run.
-n|--no-pauses Don't pause between examples (use with --all).
}
EOF1
}
# This is shown only if all examples are executed.
banner() {
if [ $all -eq 0 ]
then
cat <<-EOF2
=======================================================================
deeplearning4j examples:
Each example will be executed, then some of them will pop up a
dialog with a data plot. Quit the data plot application to proceed
to the next example.
EOF2
if [ $pauses -eq 0 ]
then
cat <<-EOF2
We'll pause after each one; hit <return> to continue or <ctrl-c>
to quit.
EOF2
fi
cat <<-EOF3
=======================================================================
EOF3
fi
}
let all=1
let pauses=0
while [ $# -ne 0 ]
do
case $1 in
-h|--h*)
help
exit 0
;;
-a|--a*)
let all=0
;;
-n|--n*)
let pauses=1
;;
*)
help "Unrecognized argument $1"
exit 1
;;
esac
shift
done
# Most have class names that end with "Example", but not all.
# So, we use a hack; we search the Java files for "void main"
# to find all of them.
dir=$PWD
cd dl4j-examples/src/main/java
find_examples() {
# Find all the Java files with "main" routines, then replace
# all '/' with '.', then remove extraneous leading '.' and
# the file extension, yielding the fully-qualified class name.
find . -name '*.java' -exec grep -l 'void main' {} \; | \
sed "s?/?.?g" | sed "s?^\.*\(.*\)\.java?\1?"
}
# The following works because IFS, the "field" separator is \n.
# So, substituting the result of find_examples into the the
# string and then evaluating the array definition, produces
# an array of the class names!
eval "arr=($(find_examples))"
cd $dir
# Invoke with
# NOOP=echo runexamples.sh
# to echo the command, but not run it.
runit() {
echo; echo "====== $1"; echo
$NOOP java -cp ./dl4j-examples/target/dl4j-examples-*-bin.jar "$1"
}
let which_one=0
error="ERROR: Must enter a number between 1 and ${#arr[@]}"
re='^[0-9]+$'
if [ $all -ne 0 ]
then
for index in "${!arr[@]}" # ! returns indices instead
do
let i=$index+1
echo "[$(printf "%2d" $i)] ${arr[$index]}"
done
printf "\nEnter a number for the example to run (q to quit): "
read which_one
if [ -z "$which_one" ]; then
echo $error
exit 1
elif [ $which_one = 'q' ]; then
exit 0
elif ! [[ $which_one =~ $re ]]; then
echo $error
exit 1
elif [ $which_one -le 0 -o $which_one -gt ${#arr[@]} ]; then
echo $error
exit 1
else
let which_one=$which_one-1
fi
runit ${arr[$which_one]}
else
banner
# now loop through the above array
for e in "${arr[@]}"
do
runit "$e"
if [ $pauses -eq 0 ]
then
echo; echo -n "hit return to continue: "
read toss
fi
done
fi