gpt4 book ai didi

bash - 菜单不想使用功能

转载 作者:行者123 更新时间:2023-12-04 19:03:53 25 4
gpt4 key购买 nike

我正在做一个基于菜单的程序,我试图调用一个函数作为菜单选项之一。在这种情况下,它是首选。但是,如果我按 1 没有任何 react ,但脚本有效(我已经测试过)。当我按 2 程序存在并且如果我按任何不同于 1 或 2 的数字时,它会发出警告,表明它不是一个有效的选择。

你们能帮忙吗?
谢谢你

#!/bin/bash


one() {
who |
awk '
{ User [$1]++; }
BEGIN { printf "|%-15s| |%15s|\n\n", "Username", "Session Count" }
END { for (i in User) printf "|%-15s| |%15s|\n", i, User [i] }
'
}


while [ 1 ]
do
clear
echo "1. Display current users with session counts"
echo "2. Exit"
read -p "Enter your menu choice [1 - 2]:" choice
case $choice in
1)
one;;
2)
exit 0;;
*) read -p "Wrong selection!!! Press [Enter] to continue..." dummyChoice;;
esac
done

最佳答案

你的脚本很好。只是 clear您的语句会清除屏幕,因此您看不到与案例 1 对应的输出.

删除 clear或者

添加 read继续循环之前的语句:

while [ 1 ]
do
clear
echo "1. Display current users with session counts"
echo "2. Exit"
read -p "Enter your menu choice [1 - 2]:" choice
case $choice in
1)
one;;
2)
exit 0;;
*) read -p "Wrong selection!!! Press [Enter] to continue..." dummyChoice;;
esac
read # so you can see the output before the next iteation
done

关于bash - 菜单不想使用功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16265810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com