shelllings

a practical way to learn shell
git clone https://git.davidvoz.net/shelllings.git
index
logs
tree
license

shelllings.sh
1#!/bin/sh
2
3EX_DIR="exercises"
4TEST_DIR="tests"
5
6list_prev() {
7 echo "Listing all challenges"
8
9 for file in "$TEST_DIR"/*; do
10 name=$(basename "$file")
11
12 case "$name" in
13 11_userinput.sh|13_copying_and_moving.sh)
14 echo "$name: skipped"
15 continue
16 ;;
17 esac
18
19 sh "$file" > /dev/null 2>&1 || echo "$name: failed"
20 sh "$file" > /dev/null 2>&1 && echo "$name"
21 done
22
23 rm -rf blueberry 11_file .youfoundme
24}
25
26
27run_ex() {
28 num="$1"
29 ex_file=$(ls "$EX_DIR"/*.sh | sort | sed -n "${num}p")
30 test_file=$(ls "$TEST_DIR"/*.sh | sort | sed -n "${num}p")
31
32 if [ ! -f "$ex_file" ]; then
33 echo "ERROR exercise file not found"
34 exit 1
35 fi
36
37 if [ ! -f "$test_file" ]; then
38 echo "ERROR test file is not found"
39 exit 1
40 fi
41
42 echo "Running exercise: $(basename "$ex_file")"
43 echo "Testing with: tests/$(basename "$test_file")"
44 echo ""
45
46 sh "$test_file"
47}
48
49usage() {
50 echo "Usage"
51 echo " sh shelllings.sh INT run the INTth exercise"
52 echo " sh shelllings.sh list list the exercises you have completed"
53 echo " sh shelllings.sh test test the program if runnable"
54 exit 0
55}
56
57case "$1" in
58 test)
59 echo "shelllings.sh is working (for now)"
60 ;;
61 list)
62 list_prev
63 ;;
64 ''|*[!0-9]*)
65 usage
66 ;;
67 *)
68 run_ex "$1"
69 ;;
70esac