本文目录导读:

我来为您介绍几种快速构建菜单的脚本方法,涵盖不同场景和语言。
Shell脚本 - 交互式菜单
#!/bin/bash
# 简单的交互式菜单
show_menu() {
clear
echo "=========================="
echo " 系统管理菜单"
echo "=========================="
echo "1. 查看系统信息"
echo "2. 查看磁盘使用"
echo "3. 查看内存使用"
echo "4. 查看进程"
echo "0. 退出"
echo "=========================="
}
while true; do
show_menu
read -p "请输入选项: " choice
case $choice in
1) echo "系统信息:"; uname -a;;
2) df -h;;
3) free -h;;
4) ps aux;;
0) echo "再见!"; exit 0;;
*) echo "无效选项";;
esac
read -p "按回车继续..."
done
Python - 使用curses库
#!/usr/bin/env python3
import curses
import subprocess
def main(stdscr):
# 初始化颜色
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
menu_items = [
"系统信息",
"磁盘使用",
"内存状态",
"网络配置",
"退出"
]
current_row = 0
while True:
stdscr.clear()
stdscr.addstr(0, 0, "=== 系统工具菜单 ===", curses.color_pair(1))
for idx, item in enumerate(menu_items):
if idx == current_row:
stdscr.addstr(idx + 2, 0, f"> {item}", curses.color_pair(2))
else:
stdscr.addstr(idx + 2, 0, f" {item}")
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(menu_items) - 1:
current_row += 1
elif key == ord('\n'):
if current_row == len(menu_items) - 1:
break
elif current_row == 0:
stdscr.addstr(10, 0, subprocess.getoutput("uname -a"))
stdscr.refresh()
curses.endwin()
if __name__ == "__main__":
curses.wrapper(main)
Bat脚本 - Windows菜单
@echo off chcp 65001 >nul :menu cls echo ========================== echo Windows系统工具 echo ========================== echo 1. 查看IP配置 echo 2. 磁盘清理 echo 3. 系统信息 echo 4. 进程列表 echo 0. 退出 echo ========================== set /p choice=请输入选项: if "%choice%"=="1" ipconfig if "%choice%"=="2" cleanmgr if "%choice%"=="3" systeminfo if "%choice%"=="4" tasklist if "%choice%"=="0" exit /b pause goto menu
JavaScript - Web菜单
<!DOCTYPE html>
<html>
<head>
<style>
.menu-container {
max-width: 300px;
margin: 20px auto;
}
.menu-item {
display: block;
width: 100%;
padding: 12px;
margin: 5px 0;
background: #f0f0f0;
border: 1px solid #ddd;
cursor: pointer;
transition: background 0.3s;
}
.menu-item:hover {
background: #e0e0e0;
}
.menu-item.active {
background: #007bff;
color: white;
}
</style>
</head>
<body>
<div class="menu-container">
<h2>快速任务菜单</h2>
<button class="menu-item" data-action="info">系统信息</button>
<button class="menu-item" data-action="backup">数据备份</button>
<button class="menu-item" data-action="clean">清理缓存</button>
<button class="menu-item" data-action="report">生成报告</button>
</div>
<script>
document.querySelectorAll('.menu-item').forEach(item => {
item.addEventListener('click', function() {
// 高亮当前选中
document.querySelectorAll('.menu-item').forEach(i =>
i.classList.remove('active')
);
this.classList.add('active');
// 根据动作执行对应功能
const action = this.dataset.action;
switch(action) {
case 'info':
alert('获取系统信息...');
break;
case 'backup':
alert('开始备份...');
break;
case 'clean':
alert('清理缓存...');
break;
case 'report':
alert('生成报告...');
break;
}
});
});
</script>
</body>
</html>
PowerShell - 动态菜单
function Show-Menu {
param(
[string]$Title = "系统管理"
)
Clear-Host
Write-Host "======================" -ForegroundColor Cyan
Write-Host " $Title" -ForegroundColor Yellow
Write-Host "======================" -ForegroundColor Cyan
Write-Host "1. 查看系统信息"
Write-Host "2. 网络诊断"
Write-Host "3. 服务管理"
Write-Host "4. 事件日志"
Write-Host "Q. 退出"
Write-Host "======================" -ForegroundColor Cyan
}
do {
Show-Menu
$selection = Read-Host "请选择"
switch ($selection) {
'1' {
Get-ComputerInfo | Select-Object WindowsVersion, WindowsBuildLabEx
pause
}
'2' {
Test-NetConnection google.com
pause
}
'3' {
Get-Service | Where-Object {$_.Status -eq "Stopped"} |
Select-Object Name, Status, DisplayName
pause
}
'4' {
Get-EventLog -LogName System -Newest 10 |
Format-Table TimeGenerated, EntryType, Message -Wrap
pause
}
'q' {
break
}
'Q' {
break
}
}
} while ($true)
快速构建技巧
模板化菜单
# 定义菜单项数组
menu_items=(
"功能1:command1"
"功能2:command2"
"功能3:command3"
)
# 通用菜单生成器
generate_menu() {
local title=$1
shift
local items=("$@")
echo "=== $title ==="
for i in "${!items[@]}"; do
echo "$((i+1)). ${items[$i]%%:*}"
done
echo "0. 退出"
}
JSON配置文件
{
"menu": {
"title": "系统工具",
"items": [
{"name": "系统信息", "command": "uname -a"},
{"name": "磁盘使用", "command": "df -h"},
{"name": "内存状态", "command": "free -h"}
]
}
}
选择哪种方法取决于您的具体需求:
- 命令行环境:Shell/Bat脚本
- 跨平台:Python
- Web界面:JavaScript
- Windows管理:PowerShell
希望这些示例能帮您快速构建所需的菜单系统!