forked from Noah-Huppert/net-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.sh
executable file
·66 lines (62 loc) · 1.35 KB
/
filter.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
#!/usr/bin/env bash
#
#?
# Filter - Searches Net Test output for network connectivity tests with specific
# statuses
#
# Usage: filter.sh
#
# Expects stdin to be net-test.sh output
#
# Arguments:
# --status (String): Filter output to only show tests which "pass" or
# "fail"
# --sites: Will only show the sites header from test output. Can not be
# used with any other argument
#?
# Parse arguments
default_op_status=".*"
op_status="$default_op_status"
op_sites="false"
while [ ! -z "$1" ]; do
key="$1"
shift
case "$key" in
--status)
if [ "$1" == "pass" ]; then
op_status=1
elif [ "$1" == "fail" ]; then
op_status=0
else
echo "Error: --status argument expects either \"pass\" or \"fail\"" >&2
exit 1
fi
shift
;;
--sites)
op_sites="true"
;;
*)
echo "Error: unknown argument \"$key\"" >&2
exit 1
;;
esac
done
# Check --sites argument is only argument if passed
if [ "$op_status" != "$default_op_status" ] && [ "$op_sites" == "true" ]; then
echo "Error: --sites argument can not be provided with any other arguments" >&2
exit 1
fi
# If sites arg provided
if [ "$op_sites" == "true" ]; then
# Read lines until no more sites headers
while read -r line; do
if [[ "$line" =~ ^# ]]; then
echo "$line" | sed -e 's/^#\(.*\)/\1/'
else
exit 0
fi
done
else
cat - | grep -P "^.* $op_status .* .*"
fi