-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.sh
More file actions
86 lines (77 loc) · 2.73 KB
/
validate.sh
File metadata and controls
86 lines (77 loc) · 2.73 KB
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
#!/bin/bash
# Validates task4_1.out against the required format rules.
# Usage: ./validate.sh [path/to/task4_1.out]
FILE="${1:-task4_1.out}"
ERRORS=0
fail() { echo "FAIL: $1"; ERRORS=$((ERRORS + 1)); }
pass() { echo "OK: $1"; }
if [ ! -f "$FILE" ]; then
fail "Output file '$FILE' not found"
exit 1
fi
# Read entire file into array of lines
mapfile -t LINES < "$FILE"
TOTAL=${#LINES[@]}
check_line() {
local idx="$1" expected_prefix="$2" allowed_chars="$3"
local line="${LINES[$idx]}"
# Must start with exact prefix (field name + ": ")
if [[ "$line" != "$expected_prefix"* ]]; then
fail "Line $((idx+1)): expected '$expected_prefix...', got: '$line'"
return
fi
local value="${line#"$expected_prefix"}"
# Must not start with a space (would mean two spaces after colon)
if [[ "$value" == " "* ]]; then
fail "Line $((idx+1)): extra space after colon in '$line'"
return
fi
# Check allowed characters in value (optional — only when pattern given)
if [ -n "$allowed_chars" ] && [[ "$value" =~ [^$allowed_chars] ]]; then
fail "Line $((idx+1)): disallowed characters in value: '$value'"
return
fi
pass "Line $((idx+1)): $expected_prefix..."
}
# Expected line order
IDX=0
check_line $IDX "--- Hardware ---"; IDX=$((IDX+1))
check_line $IDX "CPU: "; IDX=$((IDX+1))
check_line $IDX "RAM: "; IDX=$((IDX+1))
check_line $IDX "Motherboard: "; IDX=$((IDX+1))
check_line $IDX "System Serial Number: "; IDX=$((IDX+1))
check_line $IDX "--- System ---"; IDX=$((IDX+1))
check_line $IDX "OS Distribution: "; IDX=$((IDX+1))
check_line $IDX "Kernel version: " "[^ ]"; IDX=$((IDX+1)) # no spaces allowed
check_line $IDX "Installation date: "; IDX=$((IDX+1))
check_line $IDX "Hostname: "; IDX=$((IDX+1))
check_line $IDX "Uptime: "; IDX=$((IDX+1))
check_line $IDX "Processes running: " "0-9"; IDX=$((IDX+1))
check_line $IDX "Users logged in: " "0-9"; IDX=$((IDX+1))
check_line $IDX "--- Network ---"; IDX=$((IDX+1))
# Network lines: one per interface
if [ "$IDX" -ge "$TOTAL" ]; then
fail "No network interface lines found"
else
while [ "$IDX" -lt "$TOTAL" ]; do
line="${LINES[$IDX]}"
if [[ "$line" =~ ^[a-zA-Z0-9._-]+:\ .+$ ]]; then
val="${line#*: }"
if [[ "$val" =~ [^0-9./,\ -] ]]; then
fail "Line $((IDX+1)): disallowed character in network line: '$line'"
else
pass "Line $((IDX+1)): network: $line"
fi
else
fail "Line $((IDX+1)): malformed network line: '$line'"
fi
IDX=$((IDX+1))
done
fi
echo ""
if [ "$ERRORS" -eq 0 ]; then
echo "All checks passed."
else
echo "$ERRORS check(s) failed."
exit 1
fi