#!/bin/sh
# ASIC Campus - Linux Fundamentals practice self-check.
#
# Usage: sh linux-self-check.sh CHECK [PRACTICE_DIR]
#
# CHECK setup, 02, 03, 04, 05, 07, 12, 13, 14, 15 or all
# PRACTICE_DIR your practice directory (default: $HOME/asic-linux-practice)
#
# What it does: prints PASS, FAIL or SKIP for each file, permission or result
# that a lesson's worked example (and, where it names files, its practice)
# should have produced.
#
# What it never does: create, change, move or delete any file, use the
# network, or need sudo. It only reads inside PRACTICE_DIR; it refuses to run
# on / or on your home directory itself, and it does not follow a symbolic
# link that leads out of PRACTICE_DIR. It also never runs your own scripts or
# simulations; it reads the files they left behind.
#
# Exit status: 0 when nothing failed, 1 when a check failed, 2 on a usage error.
set -u
LC_ALL=C
export LC_ALL
GIT_OPTIONAL_LOCKS=0
export GIT_OPTIONAL_LOCKS
passed=0
failed=0
skipped=0
ok() {
passed=$((passed + 1))
printf 'PASS %s\n' "$1"
}
bad() {
failed=$((failed + 1))
printf 'FAIL %s\n' "$1"
if [ $# -gt 1 ]; then
printf ' hint: %s\n' "$2"
fi
}
skip() {
skipped=$((skipped + 1))
printf 'SKIP %s\n' "$1"
}
info() {
printf 'INFO %s\n' "$1"
}
usage() {
printf 'usage: sh %s CHECK [PRACTICE_DIR]\n' "$0" >&2
printf ' CHECK: setup 02 03 04 05 07 12 13 14 15 all\n' >&2
exit 2
}
have() {
command -v "$1" >/dev/null 2>&1
}
# inside REL: true when PRACTICE/REL exists and its real location is inside
# the practice directory (no symbolic link leads elsewhere).
inside() {
in_path=$P/$1
[ -e "$in_path" ] || return 1
if [ -d "$in_path" ]; then
in_real=$(cd -P -- "$in_path" 2>/dev/null && pwd -P) || return 1
else
[ -L "$in_path" ] && return 1
in_dir=$(dirname -- "$in_path")
in_real=$(cd -P -- "$in_dir" 2>/dev/null && pwd -P) || return 1
fi
case $in_real/ in
"$P"/*) return 0 ;;
*) return 1 ;;
esac
}
# need_dir REL HINT
need_dir() {
if [ -d "$P/$1" ] && inside "$1"; then
ok "directory $1/ exists"
else
bad "directory $1/ is missing" "$2"
fi
}
# need_file REL HINT: a regular file (not a symbolic link) inside the practice directory.
need_file() {
if [ -f "$P/$1" ] && inside "$1"; then
ok "file $1 exists"
return 0
fi
bad "file $1 is missing" "$2"
return 1
}
# file_ok REL: quiet version of need_file for checks that read the file.
file_ok() {
[ -f "$P/$1" ] && inside "$1"
}
# need_text REL ERE DESCRIPTION HINT
need_text() {
if ! file_ok "$1"; then
bad "$1 $3 (file not found)" "$4"
elif grep -Eq -- "$2" "$P/$1"; then
ok "$1 $3"
else
bad "$1 $3" "$4"
fi
}
# mode REL: the ten-character permission string shown by ls -l.
mode() {
ls -ld -- "$P/$1" 2>/dev/null | cut -c1-10
}
owner_can_execute() {
case $(mode "$1") in
???x*) return 0 ;;
*) return 1 ;;
esac
}
sha256_of() {
if have sha256sum; then
sha256sum < "$1" | cut -d' ' -f1
elif have shasum; then
shasum -a 256 < "$1" | cut -d' ' -f1
else
return 1
fi
}
practice_dir_needed() {
if [ -z "$P" ]; then
bad "practice directory $P_ARG does not exist" \
"create it with: mkdir -p \"\$HOME/asic-linux-practice\", then do the lesson's worked example"
return 1
fi
return 0
}
check_setup() {
printf '\n== setup: tools used in the course\n'
for tool in bash grep sed awk find sort tar gzip file less realpath sha256sum git make python3 iverilog vvp; do
if have "$tool"; then
ok "$tool found at $(command -v "$tool")"
else
bad "$tool not found" "install it as shown in the guide 'Set up your Linux practice environment'"
fi
done
for tool in gtkwave vim nano tree rg namei ssh rsync; do
if have "$tool"; then
ok "$tool found at $(command -v "$tool") (optional)"
else
skip "$tool not found (optional; some lessons use it)"
fi
done
if have iverilog; then
info "iverilog reports: $(iverilog -V 2>&1 | head -n 1)"
fi
if [ -r /etc/os-release ]; then
info "system: $(grep -E '^PRETTY_NAME=' /etc/os-release | cut -d= -f2- | tr -d '"')"
else
info "system: $(uname -s) (no /etc/os-release; this is probably not Linux)"
fi
if [ -z "$P" ]; then
bad "practice directory $P_ARG does not exist" \
"create it with: mkdir -p \"\$HOME/asic-linux-practice\""
return
fi
ok "practice directory $P exists"
case $P in
/mnt/[a-z]/*)
bad "practice directory is on a Windows drive ($P)" \
"on WSL keep course work in your Linux home directory; chmod does not behave normally on /mnt/c"
;;
esac
}
check_02() {
printf '\n== 02 Filesystem and navigation\n'
need_dir source "run the first worked-example block in lesson 02"
need_file source/note.txt "touch source/note.txt inside the practice directory"
if [ -L "$P/note-link" ]; then
ok "note-link is a symbolic link"
if have readlink && [ "$(readlink "$P/note-link")" = "source/note.txt" ]; then
ok "note-link points to source/note.txt"
elif [ -f "$P/note-link" ]; then
ok "note-link resolves to an existing file"
else
bad "note-link is dangling or points elsewhere" "it should be created with: ln -s source/note.txt note-link"
fi
else
bad "note-link is missing or is not a symbolic link" "ln -s source/note.txt note-link"
fi
for sub in rtl tb sim logs scripts; do
need_dir "project/$sub" "mkdir -p \"\$HOME/asic-linux-practice/project\"/{rtl,tb,sim,logs,scripts}"
done
need_file project/rtl/dut.sv "touch rtl/dut.sv tb/tb_top.sv inside project/"
need_file project/tb/tb_top.sv "touch rtl/dut.sv tb/tb_top.sv inside project/"
printf -- '-- practice (checks the example names from the Expected result)\n'
need_dir source-copy "the practice suggests a second directory named source-copy"
need_file source-copy/note-moved.txt "copy source/note.txt into source-copy/, then mv it to note-moved.txt"
}
check_03() {
printf '\n== 03 Viewing and processing text\n'
need_text logs/run.log '^PASS ' "has PASS lines" "printf 'PASS reset_test\\nFAIL write_test\\nPASS read_test\\n' > logs/run.log"
need_text logs/run.log '^FAIL ' "has a FAIL line" "recreate logs/run.log from the worked example"
need_text logs/failures.txt 'FAIL write_test' "holds the failing test" "grep -n '^FAIL ' logs/run.log | tee logs/failures.txt"
need_text logs/matches.txt 'logs/run\.log:[0-9]+:FAIL' "holds the match with its filename" \
"run the 'Separate output from errors' block"
if file_ok logs/grep-errors.txt && [ -s "$P/logs/grep-errors.txt" ]; then
ok "logs/grep-errors.txt captured the error message"
else
bad "logs/grep-errors.txt is missing or empty" "the 2> redirection should save grep's error about logs/missing.log"
fi
if file_ok logs/missing.log; then
bad "logs/missing.log exists" "the example relies on that file not existing; remove it yourself and rerun"
else
ok "logs/missing.log does not exist, as the example expects"
fi
}
check_04() {
printf '\n== 04 Permissions and ownership\n'
# Not "inside": a directory without u+x cannot be entered, and that is what this checks.
if [ -d "$P/scripts" ] && [ ! -L "$P/scripts" ]; then
if owner_can_execute scripts; then
ok "scripts/ is searchable by its owner ($(mode scripts))"
else
bad "scripts/ has no owner execute permission ($(mode scripts))" \
"the 'blocked path' example ends with: chmod u+x scripts"
fi
else
bad "directory scripts/ is missing" "mkdir -p \"\$HOME/asic-linux-practice/scripts\""
fi
if need_file scripts/run.sh "create it with the printf command in the worked example"; then
if head -n 1 "$P/scripts/run.sh" | grep -q '^#!'; then
ok "scripts/run.sh starts with a #! line"
else
bad "scripts/run.sh has no #! first line" "recreate it with the worked-example printf command"
fi
if grep -q 'run complete' "$P/scripts/run.sh"; then
ok "scripts/run.sh prints 'run complete'"
else
bad "scripts/run.sh does not contain 'run complete'" "recreate it with the worked-example printf command"
fi
if owner_can_execute scripts/run.sh; then
ok "scripts/run.sh is executable by its owner ($(mode scripts/run.sh))"
else
bad "scripts/run.sh is not executable by its owner ($(mode scripts/run.sh))" "chmod u+x scripts/run.sh"
fi
fi
printf -- '-- practice (report.txt in the practice directory)\n'
if file_ok report.txt; then
if [ "$(mode report.txt)" = "-rw-------" ]; then
ok "report.txt is owner read/write only (-rw-------)"
else
bad "report.txt mode is $(mode report.txt), expected -rw-------" "chmod 600 report.txt"
fi
else
bad "report.txt is missing" "create report.txt in the practice directory, then chmod 600 report.txt"
fi
}
check_05() {
printf '\n== 05 Environment and shell configuration\n'
need_dir project "mkdir -p \"\$HOME/asic-linux-practice/project\""
need_dir tools/bin "run the 'Add a tool directory safely' block"
info "variables such as PROJECT_HOME live in your shell, not in files, so check them yourself with printenv"
}
check_07() {
printf '\n== 07 Storage, archives and checksums\n'
if ! need_file evidence.tgz "cd into the practice directory and run: tar -czf evidence.tgz logs source"; then
return
fi
if ! have tar; then
skip "tar is not installed, so the archive contents were not listed"
elif members=$(tar -tzf "$P/evidence.tgz" 2>/dev/null); then
ok "evidence.tgz is a readable gzip tar archive"
if printf '%s\n' "$members" | grep -Eq '^(\./)?logs/run\.log$'; then
ok "evidence.tgz contains logs/run.log"
else
bad "evidence.tgz does not contain logs/run.log" "archive the logs and source directories from lessons 02-03"
fi
if printf '%s\n' "$members" | grep -Eq '^(\./)?source/note\.txt$'; then
ok "evidence.tgz contains source/note.txt"
else
bad "evidence.tgz does not contain source/note.txt" "archive the logs and source directories from lessons 02-03"
fi
if printf '%s\n' "$members" | grep -Eq '^/|(^|/)\.\.(/|$)'; then
bad "evidence.tgz has absolute or .. member paths" "create archives from relative paths"
else
ok "evidence.tgz member paths are relative"
fi
else
bad "evidence.tgz cannot be listed with tar -tzf" "recreate it with: tar -czf evidence.tgz logs source"
fi
if need_file evidence.tgz.sha256 "sha256sum evidence.tgz > evidence.tgz.sha256"; then
line=$(head -n 1 "$P/evidence.tgz.sha256")
recorded=$(printf '%s\n' "$line" | cut -d' ' -f1)
if ! printf '%s\n' "$line" | grep -Eq '^[0-9a-f]{64} [ *]evidence\.tgz$'; then
bad "evidence.tgz.sha256 is not one 'digest evidence.tgz' line" "sha256sum evidence.tgz > evidence.tgz.sha256"
elif ! actual=$(sha256_of "$P/evidence.tgz"); then
skip "no sha256sum or shasum command, so the checksum was not verified"
elif [ "$recorded" = "$actual" ]; then
ok "evidence.tgz matches its recorded SHA-256"
else
bad "evidence.tgz does not match evidence.tgz.sha256" "the archive changed after the checksum was saved; save it again"
fi
fi
}
check_12() {
printf '\n== 12 EDA tool environment and debugging\n'
if need_file evidence/iverilog-path.txt "run the worked example in lesson 12"; then
status_line=$(grep -E '^lookup-status=[0-9]+$' "$P/evidence/iverilog-path.txt" | tail -n 1)
if [ -n "$status_line" ]; then
ok "evidence/iverilog-path.txt records $status_line"
if [ "$status_line" = "lookup-status=0" ]; then
need_text evidence/iverilog-version.txt '^Icarus Verilog version' "starts with the version line" \
"iverilog -V > \"\$evidence/iverilog-version.txt\" 2>&1"
else
info "iverilog was not found when you recorded the evidence; that is a valid, truthful result"
fi
else
bad "evidence/iverilog-path.txt has no lookup-status=N line" "append it with printf 'lookup-status=%s\\n' \"\$lookup_status\""
fi
fi
if need_file evidence/checksums.txt "sha256sum \"\$evidence\"/*.txt > \"\$evidence/checksums.txt\""; then
good_lines=0
bad_lines=0
while IFS= read -r line || [ -n "$line" ]; do
[ -n "$line" ] || continue
if ! printf '%s\n' "$line" | grep -Eq '^[0-9a-f]{64} [ *].'; then
bad_lines=$((bad_lines + 1))
continue
fi
recorded=$(printf '%s\n' "$line" | cut -c1-64)
name=$(printf '%s\n' "$line" | cut -c67-)
base=${name##*/}
case $base in
checksums.txt) continue ;;
esac
if file_ok "evidence/$base" && actual=$(sha256_of "$P/evidence/$base") && [ "$actual" = "$recorded" ]; then
good_lines=$((good_lines + 1))
else
bad_lines=$((bad_lines + 1))
fi
done < "$P/evidence/checksums.txt"
if [ "$bad_lines" -eq 0 ] && [ "$good_lines" -gt 0 ]; then
ok "evidence/checksums.txt matches $good_lines evidence file(s)"
else
bad "evidence/checksums.txt has $bad_lines line(s) that do not match a file in evidence/" \
"the evidence changed after the checksums were saved; run the sha256sum line again"
fi
fi
}
check_13() {
printf '\n== 13 Linux editors\n'
rtl=editor-lab/rtl/counter.v
need_text "$rtl" '^[[:space:]]*module[[:space:]]+counter' "declares module counter" "save the module from the worked example with Vim"
need_text "$rtl" 'always[[:space:]]*@' "has an always block" "type the full module from the worked example"
need_text "$rtl" '^[[:space:]]*endmodule' "ends with endmodule" "type the full module from the worked example"
if ! file_ok "$rtl"; then
:
elif have iverilog; then
if iverilog -g2005 -tnull "$P/$rtl" >/dev/null 2>&1; then
ok "$rtl passes an iverilog syntax-only compile"
else
bad "$rtl does not compile" "run: iverilog -g2005 -tnull rtl/counter.v and read the first error"
fi
else
skip "iverilog is not installed, so $rtl was not compiled"
fi
printf -- '-- practice (editor-lab/tb/tb_counter.v)\n'
need_text editor-lab/tb/tb_counter.v '^[[:space:]]*module[[:space:]]+tb_counter' "declares module tb_counter" \
"create tb/tb_counter.v inside editor-lab/"
need_text editor-lab/tb/tb_counter.v '^[[:space:]]*endmodule' "ends with endmodule" "finish the testbench skeleton"
}
check_14() {
printf '\n== 14 RTL/DV project organization and Git\n'
proj=counter-project
for sub in rtl tb scripts filelists sim logs waves; do
need_dir "$proj/$sub" "run the 'Create the project safely' block in lesson 14"
done
need_file "$proj/rtl/counter.v" "create the source files from the worked example"
need_file "$proj/tb/tb_counter.v" "create the source files from the worked example"
need_text "$proj/filelists/rtl.f" '^rtl/counter\.v$' "lists rtl/counter.v" "printf 'rtl/counter.v\\n' > filelists/rtl.f"
need_text "$proj/filelists/sim.f" '^tb/tb_counter\.v$' "lists tb/tb_counter.v" "see the worked example for sim.f"
for ignored in sim/ logs/ waves/; do
need_text "$proj/.gitignore" "^${ignored}\$" "ignores $ignored" "see the worked example for .gitignore"
done
printf -- '-- practice (scripts/compile.sh placeholder)\n'
need_file "$proj/scripts/compile.sh" "the practice asks for a compile script placeholder such as scripts/compile.sh"
if [ ! -d "$P/$proj/.git" ] || ! inside "$proj/.git"; then
bad "$proj is not a Git repository" "cd into counter-project and run: git init"
return
fi
ok "$proj/.git exists"
if ! have git; then
skip "git is not installed, so staged files were not checked"
return
fi
for tracked in .gitignore rtl/counter.v tb/tb_counter.v filelists/rtl.f filelists/sim.f; do
if [ -n "$(git -C "$P/$proj" ls-files -- "$tracked" 2>/dev/null)" ]; then
ok "$tracked is staged or committed"
else
bad "$tracked is not staged" "git add $tracked"
fi
done
if [ -z "$(git -C "$P/$proj" ls-files -- sim logs waves 2>/dev/null)" ]; then
ok "nothing under sim/, logs/ or waves/ is tracked"
else
bad "generated files under sim/, logs/ or waves/ are tracked" "unstage them with git rm --cached and keep them in .gitignore"
fi
if [ -z "$(git -C "$P/$proj" remote 2>/dev/null)" ]; then
ok "the practice repository has no remote"
else
bad "the practice repository has a remote" "the lesson asks you not to connect it to a server: git remote -v shows it"
fi
}
check_15() {
printf '\n== 15 Icarus Verilog simulation workflow\n'
proj=counter-project
need_file "$proj/sim/counter_sim" "compile with the first worked-example block in lesson 15"
need_file "$proj/logs/compile.log" "the compile command saves its output to logs/compile.log"
need_text "$proj/logs/simulation.log" '^TEST PASS count=5' "records TEST PASS count=5" \
"run: vvp sim/counter_sim | tee logs/simulation.log (restore 4'd5 if you changed it)"
if file_ok "$proj/logs/simulation.log" && grep -q '^TEST FAIL' "$P/$proj/logs/simulation.log"; then
bad "$proj/logs/simulation.log contains TEST FAIL" "restore the expected count 4'd5 and rerun scripts/run_sim.sh"
elif file_ok "$proj/logs/simulation.log"; then
ok "$proj/logs/simulation.log has no TEST FAIL line"
fi
if file_ok "$proj/waves/counter.vcd" && [ -s "$P/$proj/waves/counter.vcd" ] \
&& grep -q 'enddefinitions' "$P/$proj/waves/counter.vcd"; then
ok "$proj/waves/counter.vcd is a non-empty VCD file"
else
bad "$proj/waves/counter.vcd is missing, empty or not a VCD" "check \$dumpfile and \$dumpvars in the testbench"
fi
if need_file "$proj/scripts/run_sim.sh" "save the script from 'Automate the complete run'"; then
if owner_can_execute "$proj/scripts/run_sim.sh"; then
ok "scripts/run_sim.sh is executable by its owner"
else
bad "scripts/run_sim.sh is not executable" "chmod u+x scripts/run_sim.sh"
fi
if ! have bash; then
skip "bash is not installed, so scripts/run_sim.sh syntax was not checked"
elif bash -n "$P/$proj/scripts/run_sim.sh" 2>/dev/null; then
ok "scripts/run_sim.sh has valid Bash syntax (bash -n)"
else
bad "scripts/run_sim.sh has a Bash syntax error" "run: bash -n scripts/run_sim.sh"
fi
fi
}
[ $# -ge 1 ] && [ $# -le 2 ] || usage
CHECK=$1
case $CHECK in
setup|02|03|04|05|07|12|13|14|15|all) ;;
*) usage ;;
esac
P_ARG=${2:-${HOME:-}/asic-linux-practice}
P=
if [ -d "$P_ARG" ]; then
P=$(cd -P -- "$P_ARG" && pwd -P) || P=
fi
if [ -n "$P" ]; then
home_real=
if [ -n "${HOME:-}" ] && [ -d "$HOME" ]; then
home_real=$(cd -P -- "$HOME" && pwd -P)
fi
if [ "$P" = / ] || [ "$P" = "$home_real" ]; then
printf 'Refusing to check %s: give your practice directory, such as %s/asic-linux-practice.\n' \
"$P" "${HOME:-~}" >&2
exit 2
fi
fi
printf 'ASIC Campus Linux self-check: %s, practice directory %s\n' "$CHECK" "${P:-$P_ARG}"
printf 'Read-only: this script does not change any file.\n'
case $CHECK in
setup) check_setup ;;
all)
check_setup
if practice_dir_needed; then
for lesson in 02 03 04 05 07 12 13 14 15; do
"check_$lesson"
done
fi
;;
*)
if practice_dir_needed; then
"check_$CHECK"
fi
;;
esac
printf '\npassed=%s failed=%s skipped=%s\n' "$passed" "$failed" "$skipped"
if [ "$failed" -gt 0 ]; then
printf 'Fix the FAIL lines in order, then run the check again. A check confirms files and settings; it does not mark your written answers.\n'
exit 1
fi
printf 'No failures. A check confirms files and settings; it does not mark your written answers.\n'
exit 0