> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/python/python-xiao-ji-qiao/shi-yong-shell-he-python-fen-bie-shi-xian-jian-dan-cai-dan-gong-neng.md).

# 使用shell和python分别实现简单菜单功能

### shell 方式

```bash
#!/bin/bash
 
User ()
{
       echo "用户登录记录："
       echo "`last`"
}
 
Ifcfg ()
{
        echo "本机网卡信息如下：" 
        echo "`ifconfig`"
}
 
Netstat ()
{
        echo "http的并发请求数及其TCP连接状态:" 
        echo "`netstat -n | awk '/^tcp/ {++S$NF]} END {for(a in S) print a, S[a]}'`" 
}
 
Nfsstat ()
{
   echo " NFS 状态:" 
   echo "`nfsstat -cn`" 
}
 
System ()
{
   echo " 系统版本:"
   echo "`lsb_release -a`"
}
 
Disk ()
{
   echo " 磁盘分区使用情况:"
   echo "`df -h`"
}
 
IO ()
{
   echo " 磁盘I/O:"
   echo "`iostat -dx`"
}
 
Top ()
{
   echo " cpu、内存使用情况:"
   echo "`vmstat 5`"
}
 
PS3="请输入您的选择:"
A="查看当前用户:查看当前网卡参数:查看http并发数:查看NFS状态:系统版本:磁盘分区使用情况:磁盘IO
:cpu内存使用情况:退出脚本" && IFS=:
  select i in ${A};do
    case $i in
          查看当前用户)
           User
          ;;
          查看当前网卡参数)
           Ifcfg
          ;;
          查看http并发数)
           Netstat
          ;;
          查看NFS状态)
           Nfsstat
          ;;
          系统版本)
           System
          ;;
          磁盘分区使用情况)
           Disk
          ;;
          磁盘IO)
           IO
          ;;
          cpu内存使用情况)
           Top
          ;;
          退出脚本)
           exit 0 ;;
          *)
          echo "没有正确的选择"; exit 1;;
    esac
done

```

### python 方式

```python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Filename: systemstatus.py
 
import os
# swich case 实现
# This class provides the functionality we want. You only need to look at
# this if you want to know how this works. It only needs to be defined
# once, no need to muck around with its internals.
class switch(object):
    def __init__(self, value):
        self.value = value
        self.fall = False
 
    def __iter__(self):
        """Return the match methd once, then stop"""
        yield self.match
        raise StopIteration
 
    def match(self, *args):
        """Indicate whether or not to enter a case suite"""
        if self.fall or not args:
            return True
        elif self.value in args: # changed for v1.5, see below
            self.fall = True
            return True
        else:
            return False
 
def print_menu():
    menu = {
        1: "查看当前用户",
        2: "查看当前网卡参数",
        3: "查看http并发数",
        4: "查看nfs状态",
        5: "系统版本",
        6: "磁盘分区使用情况",
        7: "磁盘IO",
        8: "cpu内存使用情况",
        9: "打印这个菜单"
    }
    print('系统信息查看工具菜单：')
    # 打印整个菜单
    for i in menu:
        print(i, menu[i])
    return
lst = print_menu()
print(lst)
# while True 的部分实现了一个永远不会自己停止的循环。但是在循环内部的if语句中加入条件是可以的，在条件满足时调用break语句。
# 这样一来就可以早循环内部任何地方而不是只在开头（像普通的while循环一样）终止循环。
# if/break 语句自然地将循环分为两个部分：第一部分负责初始化（在普通的while循环中，这部分需要重复），第二部分则在循环条件
# 为真的情况下使用第一部分内初始化好的数据。
while True:
    item = input('请输入菜单编号(1-9，留空退出程序):')
    if not item: break
    for case in switch(item):
        if case('1'):
            print('当前用户信息:')
            os.system('last')
            break
        if case('2'):
            print('当前网卡参数:')
            os.system('ifconfig')
            break
        if case('3'):
            print('http并发数:')
            os.system("netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'")
            break
        if case('4'):
            print('nfs状态:')
            os.system('nfsstat -cn')
            break
        if case('5'):
            print('系统版本:')
            os.system('lsb_release -a')
            break
        if case('6'):
            print("磁盘分区使用情况")
            os.system('df -h')
            break
        if case('7'):
            print('磁盘IO:')
            os.system('iostat -dx')
            break
        if case('8'):
            print('cpu内存使用情况:')
            os.system('vmstat 5')
            break
        if case('9'):
            lst = print_menu( )
            print(lst)
            break
        if case(''):
            print("退出!")
            exit(1)

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/python/python-xiao-ji-qiao/shi-yong-shell-he-python-fen-bie-shi-xian-jian-dan-cai-dan-gong-neng.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
