Simple Menu for User Input

This script will create an input screen in BASH that allows the user to enter information just as they would in a window. This is to show the use of simple functions as well as the tput command. The command “tput cup” allows the developer to place the cursor anywhere on the screen. There are many more commands that tput recognizes. See the main page for more information.


#!/bin/bash

## Clears the screen and builds the user input screen

function screen() {
clear
cat < ####################################################################
## Main Screen for Input ##
####################################################################
## ##
## User Name: _________________________________________ ##
## Directory: _________________________________________ ##
## Group: _________________________________________ ##
## ##
####################################################################
## Preferences ## Other Info ##
####################################################################
## Show Email? _ ## Receive News? _ ##
## Show Address? _ ## Receive RSS? _ ##
####################################################################
EOF
}

## This will echo out the contents of the helpString variable
## below the screen
function showHelp() {
tput cup 14 0
## Clear out the last information
echo " "
tput cup 14 0
echo $helpString
}
## Display the screen
screen

## Set up the help information, and call the showHelp function
## that displays it
helpString="Enter the user or press ENTER to quit"
showHelp
tput cup 4 18
read username
## If they don't enter a username, quit
if [ "$username" = "" ]
then
exit
fi

## Now go through and get the other data
helpString="Enter Home Directory"
showHelp
tput cup 5 18
read directory

helpString="What group does $username belong to?"
showHelp
tput cup 6 18
read group

helpString="Do you want to show the Email Address to others? [y/n"
showHelp
tput cup 11 19
read showEmail

helpString="Do you want to show your Home Address to others [y/n]"
showHelp
tput cup 12 19
read showAddress

helpString="Would you like to receive the News Letter?"
showHelp
tput cup 11 54
read getNews

helpString="Would you like to receive the RSS Feed?"
showHelp
tput cup 12 54
read getRSS

helpString="Thank you for your input"
showHelp
echo ""

The Conversation

Follow the reactions below and share your own thoughts.

  • Sunny

    when i just copy paste your script and try to run, i encountered with below error, please suggest what is wrong… and thx for your script…
    ===============
    ERROR:
    user-input.sh: line 7: syntax error near unexpected token `newline’
    user-input.sh: line 7: `cat

    • LukeS

      there are sever problems in his, try this, its still not perfect, but it works:
      #!/bin/bash

      ## Clears the screen and builds the user input screen

      function screen() {
      clear
      cat

  • Deepa Ca

    `newline or ;’ is not expected.
    at the end of file

  • Metafaniel

    There’s an error in the syntax near the first EOF. The function should look like this:
    function screen(){
    clear
    cat

  • Metafaniel

    Something went wrong when posting the function, it didn’t displayed all the text I wrote, maybe that happened to LukeS back in 2009, so I’ll explain it!

    1) Write the word “function” (without any quotation marks of course!) followed by it’s name, in this example it’s “screen” followed by parenthesis and an open bracket as in the original example:
    2) Write the word “clear” as in the example.
    3) Here’s the original error! Correct it this way: Write the word “cat” followed by an empty space, then two “less than” signs

  • Metafaniel

    4) Copy and paste all the signs and text in the example to create the input form.
    5) Write down “EOF”
    6) Finish the function with a closing bracket }
    7) Copy and paste the rest of the example and try it ;)

    I hope this help you all!

  • Metafaniel

    This blog have issues when posting less than signs…
    My third step, the most important it’s incomplete because I wrote those 2 signs and the rest of the step was deleted. Here it goes again:

    3) Here’s the original error! Correct it this way: Write the word “cat” followed by an empty space, then two “less than” signs then immediately “EOF” (no space between both less than signs and the EOF)

    This will solve the problem =P