0

I am a beginner in bash scripting. and i look for similar questions but none gave me the results am looking for.

I want to create a shell that keep giving the user some options to choose from while the choice is not exit

Here is my code

choice=0

while [$choice!=4]
do
    echo "please choose the operation you want!"
    echo "1) Delete Data"
    echo "2) Insert Data"
    echo "3) Get Number of customers"
    echo "4) Exit"
    choice=$1
    if [$choice=1]; then
       echo "you have chosen to Delete Data"
    elif [$choice=2]; then
       echo "you have chosen to Insert Data"
    elif [$choice=3]; then
       echo "you have chosen to Get number of customers"
    fi  # EOF if
done

As you can see that i want my code to keep asking for the user options until the answer is not exit. My Question is this code gives me an infinite loop.

Youn Tivoli
  • 230
  • 2
  • 13
BDeveloper
  • 1,175
  • 6
  • 24
  • 44

3 Answers3

2

Bash, ksh or zsh provides you with the select command that is exactly designed to present numbered options menu:

To get the usage of the select command, type help select in your bash terminal.

select: select NAME [in WORDS;] do COMMANDS; done

Select words from a list and execute commands.

The WORDS are expanded, generating a list of words. The set of expanded words is printed on the standard error, each preceded by a number. If in WORDS is not present, in "$@" is assumed. The PS3 prompt is then displayed and a line read from the standard input. If the line consists of the number corresponding to one of the displayed words, then NAME is set to that word. If the line is empty, WORDS and the prompt are redisplayed. If EOF is read, the command completes. Any other value read causes NAME to be set to null. The line read is saved in the variable REPLY. COMMANDS are executed after each selection until a break command is executed.

Exit Status:
Returns the status of the last command executed.

Here is an implementation of it with your selection menu:

#!/usr/bin/env bash

choices=('Delete Data' 'Insert Data' 'Get Number of customers' 'Exit')

PS3='Please choose the operation you want: '

select answer in "${choices[@]}"; do
  case "$answer" in
    "${choices[0]}")
      echo 'You have chosen to Delete Data.'
      ;;

    "${choices[1]}")
      echo 'You have chosen to Insert Data.'
      ;;

    "${choices[2]}")
      echo 'You have chosen to Get number of customers.'
      ;;

    "${choices[3]}")
      echo 'You have chosen to Exit.'
      break
      ;;

    *)
      printf 'Your answer %q is not a valid option!\n' "$REPLY"
      ;;
  esac
done
unset PS3

Another implementation of the select command, using arguments rather than an array of choices:

#!/usr/bin/env ksh

PS3='Please choose the operation you want: '

set -- 'Delete Data' 'Insert Data' 'Get Number of customers' 'Exit'

select answer; do
  case "$answer" in
    "$1")
      echo 'You have chosen to Delete Data.'
      ;;

    "$2")
      echo 'You have chosen to Insert Data.'
      ;;

    "$3")
      echo 'You have chosen to Get number of customers.'
      ;;

    "$4")
      echo 'You have chosen to Exit.'
      break
      ;;

    *)
      printf 'Your answer %q is not a valid option!\n' "$REPLY"
      ;;
  esac
done
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
1

If you want to use while loop with conditional 'case' syntax, try this:

while true
do
    echo "1) Delete Data"
    echo "2) Insert Data"
    echo "3) Get Number of customers"
    echo "4) Exit"
    
    echo -ne "Enter your choice [0-4] > \c"
    read choice

    case "$choice" in
        1) echo "you have chosen to Delete Data" ; break
           ;;

        2) echo "you have chosen to Insert Data"; break
           ;;

        3) echo "you have chosen to Get number of customers"; break
           ;;

        4) exit
           ;;
           
        *) clear
           ;;
    esac
done
mchelabi
  • 154
  • 5
-1

i found the error. it was statement typing issue not in the algorithm my new code is

    while [ "$(read choice)"!="4" ]
    do
    if [ "$choice"="1" ]; then
       echo "you have chosen to Delete Data"
    elif [ "$choice"="2" ]; then
       echo "you have chosen to Insert Data"
    elif [ "$choice"="3" ]; then
       echo "you have chosen to Get number of customers"
    fi  # EOF if

    echo "please choose the operation you want!"
    echo "1) Delete Data"
    echo "2) Insert Data"
    echo "3) Get Number of customers"
    echo "4) Exit"
done
BDeveloper
  • 1,175
  • 6
  • 24
  • 44
  • 1
    This will not work right, since `$(read choice)` reads into the `choice` variable in a subshell, and variable assignments that happen in a subshell don't affect the main shell. Also, `read choice` doesn't *print* anything (it just sets a variable), `$(read choice)` will give the empty string rather than the read value, so the comparison to "4" won't work. And there's another reason it won't work: you need spaces around the operator in a comparison (that is, `[ "a" != "b" ]` rather than `[ "a"!="b" ]`). [shellcheck.net](https://www.shellcheck.net) will spot many of these common mistakes. – Gordon Davisson Aug 09 '20 at 18:07