如何实现完美设备匿名:终极硬件信息保护指南
2026/6/11 10:10:28
在编程中,流程控制是非常重要的一部分。之前我们可能会使用一系列的if命令来处理用户的选择,不过很多编程语言(包括 shell)都提供了更简洁的多选择决策流程控制机制,在 shell 中就是case命令。
case命令的语法如下:
case word in [pattern [| pattern]...) commands ;;]... esac我们来看一个之前使用if语句处理菜单选择的示例:
#!/bin/bash # read-menu: a menu driven system information program clear echo " Please Select: 1. Display System Information 2. Display Disk Space 3. Display Home Space Utilization 0. Quit " read -p "Enter selection [0-3] > " if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 0 ]]; then