Collecting User Input in a Bash Shell Script

by mike on February 1, 2011

The read command is designed to read and then use in the script input from the user.  The input that is provided by the user is stored as a variable.  This is a builtin variable that will store one line of input from the user in one or more variables.  The read command is valuable as it is a major way to input information into a shell script.  Lines are read into the script with standard input and split via the $IFS variable.  This stands for internal field separator.    The first word is assigned the first variable, the second word the second variable, etc.

Read options

-a  name        assign each work in an array of the name
-d delim         use a deliminator to terminate input instead of new line
-e            input from keyboard uses Readline Library, The Readline Library is a set of library routines                 that controls the way keys function when typing text at the command line.
-n num            reads number of characters and then returns
-p prompt        displays prompt on errors
-r            ignore the backslash
-s            do not echo characters entered, silent to screen
-u n            use the integer n as the file descriptor

#!/bin/bash

echo “Please provide you login name”
read name
echo “Thanks…Processing Your Information Now”
sleep 3
grep $name /etc/passwd

The user is asked to input their username which becomes the variable $name.  A sleep command is entered which just causes the script to stop for 3 seconds to act as if processing is occurring in a database and then the line that matches the user is printed.
End of Build Your Knowledge

Variables and Text
Since the variable that is used can only have one value, quotes are used to set this specific value. The shell does not care what kind of strings you place inside the quotes.  So you could say;

REPORT=”This is the 33rd folder I’ve created today, what’s up?”

The Bash shell does not require that you declare your values, however it just provides an empty string if you do not declare them.  So in the following script the STATUS variable just outputs an empty string.

#!/bin/bash
#
REPORT=”This the 33rd folder I’ve have created today, what’s up?”
echo $REPORT
echo $STATUS

It is important to start and end your variables so the shell can tell where the variable ends.  In this example,  the variable FOLDER is confused with “_for_mike” because the shell cannot tell where the variable ends.  So it is important to add braces around the variable in this situation.

#!/bin/bash
#
echo “What folder do you want to create today?”
read FOLDER
echo “You got it, $FOLDER_for_mike about to be created.”
mkdir “$FOLDER_for_mike”
REPORT=”The $FOLDER_for_mike folder has been created.”
echo $REPORT

Now in this example the script works because of the curly braces that are placed around the variable.

#!/bin/sh
#
echo “What folder do you want to create today?”
read FOLDER
echo “You got it, ${FOLDER}_for_mike about to be created.”
mkdir “${FOLDER}_for_mike”
REPORT=”The ${FOLDER}_for_mike folder has been created.”
echo $REPORT

Comments on this entry are closed.

Previous post:

Next post: