暂停执行并等待用户输入

暂停执行并等待用户输入

我正在编写一个脚本,但遇到了一个问题:我想暂停执行并等待用户输入。我以为我可以使用命令来解决此问题,read -p -n 1 $foo但系统无法执行此命令。这是我当前的脚本:

#!/bin/sh

# Ititialization

mainmenu () {
  echo "Press 1 to update your system"
  echo "Press 2 to install samba"
  echo "Press 3 to install vsFTPd"
  echo "Press 4 to install the current version of Webmin"
  echo "Press 5 to configure samba for Active Directory"
  echo "Press x to exit the script"
  read -n 1 -p "Input Selection:" mainmenuinput
  if [ "$mainmenuinput" = "1" ]; then
            updatesystem
        elif [ "$mainmenuinput" = "2" ]; then
            installsamba
        elif [ "$mainmenuinput" = "3" ]; then
            installvsftpd
        elif [ "$mainmenuinput" = "4" ]; then
            installwebmin
        elif [ "$mainmenuinput" = "5" ]; then
            configuresambaforactivedirectory
        elif [ "$mainmenuinput" = "x" ];then
            quitprogram
        elif [ "$mainmenuinput" = "X" ];then
            quitprogram
        else
            echo "You have entered an invallid selection!"
            echo "Please try again!"
            echo ""
            echo "Press any key to continue..."
            read -n 1
            clear
            mainmenu
        fi
}

# This builds the main menu and routs the user to the function selected.

mainmenu

# This executes the main menu function.
# Let the fun begin!!!! WOOT WOOT!!!!

您可能会注意到mainmenu函数中的read -n 1 -p "text goes here"条目。根据 ubuntu,这就是我遇到的问题。有人能告诉我出了什么问题吗?谢谢!

答案1

应该:

read  -n 1 -p "Input Selection:" mainmenuinput

需要n在后面放置标志,因为这是告诉 read 在之后执行字符输入后,无需等待整行。检查help read详细信息请点击此处

相关内容