*编辑更新状态
我试图做一个bash文件,将允许您select一个选项,输出答案,然后循环回菜单select。 截至目前,我拥有的代码是:
#!/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:意外的操作员
重复循环每'x'毫秒
批处理脚本“继续”在循环?
通过registryPython循环
如果有人能够帮助我,或指引我朝着正确的方向,我将不胜感激。
Windows批处理循环尽pipevariables与dynamic令牌计数
windows批处理,在嵌套循环内设置一个variables
C#循环来填充多个DataGridView
对于菜单,使用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 ;;
不久之前,一些在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
问候!
第一次编辑之前的解决方案
在bash命令中区分大小写。 有一个echo命令,但没有Echo或ECHO命令。 if等等的if也是一样的。
简单地用echo替换所有的ECHO用IF替换IF用ELIF 。
第一次编辑后的解决方案
while [ {selection} ]看起来很奇怪。 作为一个简单的测试,删除它(包括do和done )而不需要更换。
解决方案(可能)即将到来的编辑
echo df -h不运行df -h命令,但只打印文字字符串df -h 。 只需编写df -h而不需要echo就可以运行它。
while -loop应包含read命令,以便用户可以进行新的选择,前面的选择无效。 在循环内移动read命令并调整循环条件。 按照目前的结构, while true会没事的。
不要使用引号,你必须使用反引号`或$()作为命令。
'ps -A'应该是ps -A或$(ps -a) 。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。