실행 일시 정지 및 사용자 입력 대기


29

스크립트를 작성 중이며 문제가 있습니다. 실행을 일시 중지하고 사용자 입력을 기다립니다. 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!!!!

메인 메뉴 기능에서 -n 1 -p "text goes here"항목을 볼 수 있습니다. 우분투에 따르면 문제가 발생하는 곳입니다. 누군가 무엇이 잘못되고 있는지 말해 줄 수 있습니까? 감사!


1
shebang이 잘못되었습니다. bash 기능을 사용하고 있으므로 shebang을 #!/usr/bin/env bash또는 로 설정해야합니다 #!/bin/bash.
geirha

답변:


37

해야한다:

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

필요는 넣어 n, 후 플래그를 그 후에 실행 읽어 말하고 그대로 N 개의 문자를 입력, 전체 라인을 기다리지 않습니다. 확인 help read자세한 내용은이를 .


2
나는 그것을 알아 냈습니다! 여기에 올바른 코드는 다음과 같습니다 read -n 1 -p "Input Selection:" "mainmenuinput"그것은 지금 :-) (가) / 리턴 키를 입력의 언론 기다리지 않습니다
엘리엇 연구소 LLC

1
"불법 옵션 -n"
ses

1
실행을 일시 중지하고 for 루프에서 계속 대기하려면 다음을 수행하십시오.for $whatever; do $whatever; read -n 1 -p Continue?; done
rekciltnuc

왜 프로그램이 아니기 help read때문에 왜 작동 하는지 설명 할 수 있습니까 help?
lindhe

분명히 Bash 일 것입니다. 더 많은 정보를 원하시면 :help help
lindhe
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.