使用循环菜单创建Bash文件(Linux) [英] Creating a Bash File With a Looped Menu (Linux)

查看:90
本文介绍了使用循环菜单创建Bash文件(Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*已编辑以更新状态

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

I am trying to make a bash file that will allow you to select an option, output the answer and then loop back to the menu selection. As of right now, the code I have is:

#!/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

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

The output I get is an error that infinitely loops with

[:2:意外的运算符

[: 2: unexpected operator

如果有人能帮助我或指导我朝正确的方向前进,我将不胜感激.

If anyone could help me out or guide me in the right direction I would greatly appreciate it.

推荐答案

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

For menus, use the select command. Here, I'm using bash to store the options into an array.

#!/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

如果要退出菜单,可以这样做

If you want to break out of the menu, you would do

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

或添加退出"选择

这篇关于使用循环菜单创建Bash文件(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆