Shell 脚本中基于菜单的选择
Posted
技术标签:
【中文标题】Shell 脚本中基于菜单的选择【英文标题】:Menu based selection in Shell scripting 【发布时间】:2018-12-04 03:11:06 【问题描述】:我有一个 Shell 脚本可以在一个菜单中执行 4 个任务。 到目前为止我的代码是(我猜是基本的)没有菜单。我希望它在开始时添加菜单,并且我尝试了很多方法(使用 case 语句、if else 和 while 循环)。什么都没有解决。
#!/bin/bash
cd /
echo "1. Basic Details"
echo "2. CPU Information"
echo "3. Network information"
echo "4. All"
echo "5. Cancel"
read option
if [ $option -eq 1 ];then
echo ">>>>> Server Name, Date, UPtime <<<<<"
echo "====================================="
echo "Date :- `date`"
echo " "
echo "Host Name :- `hostname`"
echo " "
echo " OS Version"
oslevel -g
echo " "
echo " UPTIME :- "
uptime
echo " "
echo " "
elif [ $option -eq 2 ];then
echo ">>>>> CPU and Memory Info. <<<<<"
echo "====================================="
echo " "
echo
echo " CPU :- `lsdev | grep Processor | wc -l`"
echo " "
echo " Memory :- `lsattr -El mem0 | tail -1`"
echo " "
echo " "
echo "====================================="
echo ">>>>> Important Kernel Params. <<<<<"
echo "====================================="
echo " "
echo "****************************************"
echo " "
lsattr -El aio0
elif [ $option -eq 3 ];then
echo ">>>>> Memory Usage Information <<<<<"
um=`svmon -G | head -2|tail -1| awk 'print $3'`
um=`expr $um / 256`
tm=`lsattr -El sys0 -a realmem | awk 'print $2'`
tm=`expr $tm / 1024`
fm=`expr $tm - $um`
ump=`expr $um \* 100`
ump=`expr $ump / $tm`
echo " "
echo "Memory Used :- "$ump"%"
echo " "
echo "----------------------";
echo "Memory Information\n";
echo "total memory = $tm MB"
echo "free memory = $fm MB"
echo "used memory = $um MB"
echo "-----------------------\n";
echo " "
echo " "
else
echo "Enter correct option"
fi
请提出更正建议。未在脚本中添加选项 4(我需要它来显示所有信息)
【问题讨论】:
@RavinderSingh13 你能帮我吗 您可能会发现使用 shell 的select
..in
构造更容易。除此之外,当问题陈述很简单时,很难提供解决方案"it doesn't work"。请edit您的问题更完整地描述您预期会发生什么以及这与实际结果有何不同。请参阅 How to Ask 以获取有关什么是好的解释的提示。
看看dialog。
【参考方案1】:
这更像是一次代码审查。
对于菜单,使用select
使用函数来封装功能
将您的代码粘贴到http://www.shellcheck.net
#!/bin/bash
main()
cd /
PS3="Enter a choice: "
select ch in "Basic Details" "CPU Information" "Network Information" All Cancel
do
case $ch in
"Basic Details")
basicDetails
;;
"CPU Information")
cpuInfo
;;
"Network Information")
memUsage # you need to fix the menu text
;;
All)
basicDetails
cpuInfo
memUsage
;;
Cancel)
break
;;
esac
done
basicDetails()
echo ">>>>> Server Name, Date, UPtime <<<<<"
echo "====================================="
echo "Date :- $(date)"
echo " "
echo "Host Name :- $(hostname)"
echo " "
echo " OS Version"
oslevel -g
echo " "
echo " UPTIME :- "
uptime
echo " "
echo " "
cpuInfo()
echo ">>>>> CPU and Memory Info. <<<<<"
echo "====================================="
echo " "
echo
echo " CPU :- $(lsdev | grep -c Processor)"
echo " "
echo " Memory :- $(lsattr -El mem0 | tail -1)"
echo " "
echo " "
echo "====================================="
echo ">>>>> Important Kernel Params. <<<<<"
echo "====================================="
echo " "
echo "****************************************"
echo " "
lsattr -El aio0
memUsage()
echo ">>>>> Memory Usage Information <<<<<"
um=$(svmon -G | head -2|tail -1| awk 'print $3')
um=$(( um / 256 ))
tm=$(lsattr -El sys0 -a realmem | awk 'print $2')
tm=$(( tm / 1024 ))
fm=$(( tm - um ))
ump=$(( um * 100 / tm ))
echo " "
echo "Memory Used :- $ump%"
echo " "
echo "----------------------"
echo "Memory Information"
echo
echo "total memory = $tm MB"
echo "free memory = $fm MB"
echo "used memory = $um MB"
echo "-----------------------"
echo
echo " "
echo " "
main "$@"
【讨论】:
谢谢格伦。我需要脚本来提示选择(1.基本细节 2.CPU 使用 3.内存使用 4.全部 5.取消)。帮助我。在提供的代码中提示菜单在哪里? 这正是select
所做的。你试过这个代码吗?
我希望你没有理解我的意思。我的需要是它应该在我执行脚本后立即显示菜单,只需提供选项编号即可提供输出 Example :从菜单中输入您的选择:1.基本详细信息 2.CPU 使用情况 3.内存 4 .ALL 5.Cancel 1 (enter) --> 应该显示Basic info 2(enter) --> 应该显示CPU,并且一旦函数执行,它应该再次返回菜单。希望这能解决问题。
你测试过我给你的代码吗?如果是的话,我不知道你为什么还在跟我解释这个。如果没有,为什么不呢?
尝试上面的代码不断询问“输入选择”并且不调用该方法【参考方案2】:
我很不明白“在开始时添加菜单”,在你的脚本本身菜单是第一个打印的语句。这是重复菜单的示例,其中退出程序取决于用户输入
#!/bin/sh
#method to just print your menu
printmenu()
echo "1. Operation 1"
echo "2. Operation 2"
echo "3. Exit"
operation1()
echo "Operation 1 is performed..!"
operation2()
echo "Operation 2 is performed..!"
while [ 1 ]
do
printmenu # printing menu
read u_option
#case handling the user inputs
case $u_option in
1) operation1;; # calling function for operation1
2) operation2;; # calling function for operation2
3) echo "Terminating the program as per your request"
exit;;
*) echo "Invalid option, provide only valid integers"
esac
done
【讨论】:
【参考方案3】:bash 有 select
内置函数:
select val in cherry banana apple ; do
echo "$val"
# do other things with $val
# ...
break # Leave this out if you want to run
# the menu forever in an endless loop
done
【讨论】:
以上是关于Shell 脚本中基于菜单的选择的主要内容,如果未能解决你的问题,请参考以下文章