【问题标题】:Creating a Bash File With a Looped Menu (Linux)使用循环菜单创建 Bash 文件 (Linux)
【发布时间】:2017-09-27 16:41:41
【问题描述】:

*已编辑以更新状态

我正在尝试制作一个 bash 文件,该文件将允许您选择一个选项、输出答案,然后返回到菜单选择。截至目前,我的代码是:

#!/bin/sh
echo off
echo "........................................."
echo "Select the process you would like to run."
echo.
echo "1 - Free Disk Space Available"
echo "2 - List Directories"
echo "3 - Show Running Processes"
echo "4 - TCP Connection"
echo "5 - Network Interfaces"
echo "6 - List Directories"
echo "7 - Show Computer Hostname"
echo "8 - Routing Table"
echo "9 - Computer Uptime"
echo "10 - Available Block Devices"
echo "11 - Mount Device"
echo "........................................."

echo -n "Input desired function number and press ENTER: "
read selection
while [ {selection} ]
do
    if [ $selection == 1 ]; then echo df -h
        break
    elif [ $selection == 2 ]; then echo ls -l /home
        break
    elif [ $selection == 3 ]; then echo ps -A
        break
    elif [ $selection == 4 ]; then echo netstat -a
        break
    elif [ $selection == 5 ]; then echo ifconfig
        break
    elif [ $selection == 6 ]; then echo $PATH
        break
    elif [ $selection == 7 ]; then echo hostname
        break
    elif [ $selection == 8 ]; then echo route
        break
    elif [ $selection == 9 ]; then echo uptime
        break
    elif [ $selection == 10 ]; then echo blkid
        break
    elif [ $selection == 11 ]; then echo mount
        break
    else
        echo "Please enter a valid option"
    fi
done

我得到的输出是一个无限循环的错误

[: 2: 意外操作符

如果有人可以帮助我或指导我正确的方向,我将不胜感激。

【问题讨论】:

  • 那个编辑完全改变了这个问题。可以更新问题,但请考虑在对您有帮助的答案中添加评论,说明您遇到了另一个问题。
  • 请注意,echo off 打印出单词“off”。 bash 不是批处理。

标签: linux bash loops while-loop menu


【解决方案1】:

对于菜单,使用select 命令。在这里,我使用bash 将选项存储到一个数组中。

#!/bin/bash

echo ".............................................................................."
echo "Select the process you would like to run by pressing the corresponding number."

choices=(
    "Free Disk Space Available"
    "List Directories"
    "Show Running Processes"
    "TCP Connection"
    "Network Interfaces"
    "List Directories"
    "Show Computer Hostname"
    "Routing Table"
    "Computer Uptime"
    "Available Block Devices"
    "Mount Device"
)

PS3="Input desired function number and press ENTER: "
select selection in "${choices[@]}"
do
    case "$selection" in 
        "${choices[0]}" ) echo df -h ;;
        "${choices[1]}" ) echo ls -l /home ;;
        "${choices[2]}" ) echo ps -A ;;
        "${choices[3]}" ) echo netstat -a ;;
        "${choices[4]}" ) echo ifconfig ;;
        "${choices[5]}" ) echo $PATH ;;
        "${choices[6]}" ) echo hostname ;;
        "${choices[7]}" ) echo route ;;
        "${choices[8]}" ) echo uptime ;;
        "${choices[9]}" ) echo blkid ;;
        "${choices[10]}" ) echo mount ;;
    esac
done

如果你想跳出菜单,你会这样做

        "${choices[10]}" ) echo mount; break ;;

或添加“退出”选项

【讨论】:

  • 如果我想重新打印菜单也是一个有效的选择,我该怎么办
  • 将select语句放入while true循环中,并在esac之后添加break
【解决方案2】:

不久前,一个对 Unix 没有多少经验的队友要求我给他一个 Bash 菜单的例子。所以我给他发了这个。希望它可以帮助你。如果递归地链接函数,则不需要循环。

function Main_Menu() {
    clear
    echo "Select an option ..."
    printf "\n"
    echo "1 - Check CPU"
    echo "2 - Check disk utilization"
    echo "3 - About"
    echo "4 - Exit"

    read -n 1 user_input    
    if [[ "$user_input" == "1" ]]
    then
        Check_CPU
    elif [[ "$user_input" == "2" ]]
    then
        Check_disk_utilization
    elif [[ "$user_input" == "3" ]]
    then
        About
    elif [[ "$user_input" == "4" ]]
    then
        clear
        echo "Chau!!"
    else
        Main_Menu
    fi
}
function Check_CPU(){
    clear

    cpu_values=$( top -b -n2 -p 1  )

    echo "****************************"
    echo "****************************"
    echo "**Current CPU values*"
    echo "****************************"
    echo "****************************"
    echo "$cpu_values"
    echo "****************************"

    echo "Press ESC to go back or any other key to update..."
    printf "\n"

    read -n 1 user_input    
    if [[ "$user_input" == $'\e' ]]
    then
        Main_Menu

    else
        Check_CPU
    fi

}
function Check_disk_utilization(){
    clear

    disk_utilization=$( df -h  )

    echo "****************************"
    echo "****************************"
    echo "**Current Disk Utilization*"
    echo "****************************"
    echo "****************************"
    echo "$disk_utilization"
    echo "****************************"

    echo "Press ESC to go back or any other key to update..."
    printf "\n"

    read -n 1 user_input    
    if [[ "$user_input" == $'\e' ]]
    then
        Main_Menu

    else
        Check_disk_utilization
    fi

}
function About(){
    clear

    echo "--------------------------------------------------"
    printf "\n"
    echo "This script was written by XXXXX"
    echo "2017, Montevideo"
    printf "\n"
    echo "--------------------------------------------------"
    echo "Press ESC to go back..."
    printf "\n"

    read -n 1 user_input    
    if [[ "$user_input" == $'\e' ]]
    then
        Main_Menu

    else
        About
    fi

}

Main_Menu

问候!

【讨论】:

    【解决方案3】:

    第一次编辑前的解决方案

    bash 中的命令区分大小写。有一个echo 命令,但没有EchoECHO 命令。 if 等也是如此。

    只需将所有ECHO 替换为echo,将IF 替换为if,将ELIF 替换为elif

    第一次编辑后的解决方案

    while [ {selection} ] 看起来很奇怪。作为一个简单的测试,将其删除(包括dodone)而不进行替换。

    (可能)即将进行的修改的解决方案

    • echo df -h 确实运行df -h 命令,但只打印文字字符串df -h。只需写df -h 不带echo 即可运行它。

    • while-loop 应该包含read 命令,以便用户可以在之前的选择无效时做出新的选择。在循环内移动read 命令并调整循环条件。以目前的结构,while true 就可以了。

    【讨论】:

      【解决方案4】:

      不要使用引号,您必须使用反引号 `$() 来作为命令。

      'ps -A' 应该是 ps -A$(ps -a)

      【讨论】:

        猜你喜欢
        • 2015-02-02
        • 2016-04-05
        • 2017-08-12
        • 1970-01-01
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多