3

Я написал скрипт оболочки bash (код приведен ниже), который дает пользователю 4 варианта. Однако у меня возникли небольшие проблемы с кодом. Прямо сейчас, когда они выбирают вариант 3, чтобы показать дату. Это повторяется снова и снова. Я должен закрыть окно терминала, чтобы остановить его, потому что это бесконечный цикл. Как бы мне предотвратить это? Также, похоже, quit не работает.

Если кто-то может мне помочь, спасибо.

#!/bin/bashe
 echo -n "Name please? "
 read name
 echo "Menu for $name
    1. Display a long listing of the current directory
    2. Display who is logged on the system
    3. Display the current date and time
    4. Quit "
read input
input1="1"
input2="2"
input3=$(date)
input4=$(exit)
while [ "$input" = "3" ]
do
echo "Current date and time: $input3"
done

while [ "$input" = "4" ]
do
echo "Goodbye $input4"
done

3 ответа3

9

Компактная версия:

options=(
    "Display a long listing of the current directory"
    "Display who is logged on the system"
    "Display the current date and time"
    "Quit" 
)

PS3="Enter a number (1-${#options[@]}): "

select option in "${options[@]}"; do
    case "$REPLY" in 
        1) ls -l ;;
        2) who ;;
        3) date ;;
        4) break ;;
    esac
done
3

Я предполагаю , что это из - за while цикла;)

Значение $input не изменяется внутри цикла. Либо вы вставляете что-то вроде read input для чтения в цикл, либо изменяете while ... do в if ... then . Если я правильно понимаю намерения вашего сценария, вам не нужны те циклы while где они сейчас находятся. Вам нужно один большой в while цикл охватывает все от echo "Menu for $name до конца.

Моя версия сценария будет:

#!/bin/bash
echo -n "Name please? "
read name
input=""
while [ "$input" != "4" ]; do
  echo "Menu for $name
    1. Display a long listing of the current directory
    2. Display who is logged on the system
    3. Display the current date and time
    4. Quit "
  read input
  input1="1"
  input2="2"
  input3=$(date)
  input4=$(exit)
  if [ "$input" = "3" ]; then
    echo "Current date and time: $input3"
  fi

  if [ "$input" = "4" ]; then
    echo "Goodbye $input4"
  fi
done
0

дело здесь, чтобы помочь вам, попробуйте

 # cat list.sh
 #!/bin/bash
 echo -n "Name please? "
 read name
 echo "Menu for $name"
 echo -e "Enter ( 1 ) to Display a long listing of the current directory \nEnter ( 2 )             to Display who is logged on the system\nEnter ( 3 ) to Display the current date and     time\nEnter ( 4 ) to Quit"
 read en
 case "$en" in
 1)ls -lR;;
 2)who;;
 3)date;;
 4)echo "Good Bye..."
 sleep 0;;
 *)echo "Wrong Option selected" ;;
 esac

Выход:

 # ./list.sh
 Name please? Ranjith
 Menu for Ranjith
 Enter ( 1 ) to Display a long listing of the current directory
 Enter ( 2 ) to Display who is logged on the system
 Enter ( 3 ) to Display the current date and time
 Enter ( 4 ) to Quit

Если вы введете другие параметры, появится меню, затем

 # ./list.sh
 Name please? Ranjith
 Menu for Ranjith
 Enter ( 1 ) to Display a long listing of the current directory
 Enter ( 2 ) to Display who is logged on the system
 Enter ( 3 ) to Display the current date and time
 Enter ( 4 ) to Quit
 5
 Wrong Option selected

...

Всё ещё ищете ответ? Посмотрите другие вопросы с метками .