AzLearn

تمرين إعادة البناء

Command Router

أعد كتابة سكربت Bash يستخدم case لتوجيه أوامر فرعية مثل help وstatus.

bash ~13 دقيقة متوسط
أعد بناء الكود Rebuild

هذا هو الكود. اكتبه بنفسك.

الكود المرجعي
#!/usr/bin/env bash
set -euo pipefail

show_help() {
	printf 'Commands:\n'
	printf '  help      Show this message\n'
	printf '  status    Print application status\n'
	printf '  greet     Print a greeting\n'
}

show_status() {
	printf 'azlearn-bash: ready\n'
}

greet() {
	local name="${1:-student}"
	printf 'Welcome, %s.\n' "$name"
}

command_name="${1:-help}"
shift || true

case "$command_name" in
	help) show_help ;;
	status) show_status ;;
	greet) greet "$@" ;;
	*)
		printf 'Unknown command: %s\n' "$command_name" >&2
		show_help
		exit 1
		;;
esac
اكتب هنا