Lesson 6 | Identifying common errors |
Objective | Examine errors involving input from the user. |
Identifying Common Errors
Two common user input problems can be solved using the techniques listed below.
Spaces in the input
Description: If your user includes spaces in their input, your command may fail when the input is used in a command.
Example: In the code below, if the user enters a space into his or her value, the if
statement will fail.
echo "Enter a value: \c"
read value
if [ $value = "quit" ]
then exit
else echo
"your value was $value"
fi
Solution: Put double quotes around user variables. If you surround the $input
variable with double quotes in the
if
statement, it will not fail. The double quotes tell the shell to treat the input as a single value.
echo "Enter an input: \c"
read input
if [ " $input" = "quit" ]
then exit
else echo "your input was $input"
fi
Characters in the input
Click the Exercise link below to practice finding syntax errors in a script.
Identifying Common Errors-Exercise
The next lesson demonstrates the use of echo statements to mark stages of success in a script.