Bash 04 – Input from User

Jarret B

Active Member
Staff member
Joined
May 22, 2017
Messages
235
Reaction score
199
Credits
4,755
A very important aspect of scripts is getting input from the user. If you think of most commands used in your system from a command-line, they mostly use input parameters. Parameters are used to specify what the command is to do.

For example, to delete a file, you need to specify which file to delete.

Interacting with the user can be very important for a script to work properly. There are multiple ways to interact with the user:
  • positional parameters
  • special parameters
  • read command
  • select command
Let’s look at these.

Positional Parameter

Positional Parameters are parameters that are based on their position on the command line. For example, let’s say we had a script that required two parameters. The first parameter after the command is a source file. The second parameter would be the destination. Look at the following:

filecopy.sh $HOME/file1 $HOME/files/file1

In this example, the command is ‘filecopy.sh’. Parameter 1 is ‘$HOME/file1’ and Parameter 2 is ‘$HOME/files/file1’.

Since the two parameters can vary every time, the command is used, we can’t easily create a script with constants as the two parameters.

Within the script, parameter 1 can be referred to as ‘$1’ and parameter 2 is ‘$2’. More parameters can be used and the variable to represent the parameters would be incremented accordingly.

NOTE: The parameter ‘$0’ is the command name.

Say we had a script called ‘addnum.sh’. The script took 5 variables, all numbers. If a letter would be used in one or more of the parameters, it would be dealt with as a zero. If less than 5 parameters were specified, an error would occur. The main line of the script is:

echo "The answer is: " $(($1 + $2 + $3 + $4 + $5))

If we wanted to make the default value for each positional parameter be zero, we can use ‘${1:-0}’ which sets the value of the first parameter to zero if it is not specified. We would have to do the same for all the parameters:

echo "The answer is: " $((${1:-0} + ${2:-0} + ${3:-0} + ${4:-0} + ${5:-0}))

Now we can just type the command, enter no parameters, and not get an error.

Special Parameters

There are five special parameters we will look at in this section. They are:
  1. $#
  2. [email protected]
  3. [email protected]
  4. $*
  5. “$*”
The first parameter is ‘$#’. The value for the parameter is the number of parameters in the command line, or basically, the highest numbered parameter in the list.

For example, the command ‘addnum.sh 1 2 3 4 5 6 7 8’ would cause ‘$#’ to have a value of 8. Another example, ‘list a b c d e f g h i j’ would be 10. The values are shown below:

  • 0 list
  • 1 a
  • 2 b
  • 3 c
  • 4 d
  • 5 e
  • 6 f
  • 7 g
  • 8 h
  • 9 i
  • 10 j
This is useful if you need to check that a specific number of parameters have been entered.

The next parameter is the ‘[email protected]’. The result of this will give you the parameter list, not counting the command itself, separated by spaces.

The parameter can allow you to word split the list. It works well unless any parameter is two words, which will then be counted as two separate parameters.

If you have a list of parameters and you need them double-quoted, then use the parameter ‘“#@”’. If you put double-quotes around the parameter, then double-quotes are placed around the individual parameters in the list. For example, if the command is ‘list oranges cherries bananas’ then the following occurs:

[email protected] oranges cherries bananas
[email protected]” “oranges” “cherries” “bananas”

The fourth parameter is the ‘$*’ which represents everything in the parameter list. Unquoted, it returns the same values as ‘[email protected]’. When we quote it, ‘“$*’”, something different happens.

The quoted value will separate the parameter list items with the default IFS value. By default, the default IFS value is the space, but that can be changed. Let’s say we have a list of number sent as a command’s parameter list. We need them separated by a semicolon. The following script would do it:

IFS=;
echo “$*”


READ

The ‘read’ command lets you get input directly from the user separately from the command-line. With no parameters for the ‘read’ command, the line is blank with no prompt. The user’s response is placed into a default variable of ‘$REPLY’.

If you want the response to be placed into a specific variable, then name the variable after the ‘read’ command. But, if you want multiple responses to be placed into multiple variables, list the variables after the ‘read’ command in the order you expect them. If you wanted a user to enter their birthdate in the order of month, day and year, the command would be:

read bmonth bday byear

If you want a simple prompt, then use the parameter ‘-p’ followed by the prompt you wish displayed.

read -p “Enter your birth date by month, day and year (numbers only): ” bmonth bday byear

The prompt will remain until an entry is made and the user presses the enter key. If you want to have the prompt timeout after a number of seconds, then use the ‘-t’ parameter followed by the number of seconds to wait. For example, to wait 30 seconds, the command is:

read -t 30 -p “Enter your birth date by month, day and year (numbers only): ” bmonth bday byear

The last parameter for the ‘read’ command is to specify that the input is secret, such as a password. Any value input is not secure and passwords should not be asked for in this manner. If the parameter ‘-s’ is used, then any input by the user is blank on the screen. No characters of any sort are shown to determine how many characters have been entered.

Select Command

The ‘select’ command allows you to display a list and get a selection by the user.

We use the ‘select’ command followed by the variable to contain the user’s selection. We then use the word ‘in’ and then enter the list of options followed by a semicolon (;). The list is separated by spaces.

The next line will contain only the word ‘do’. The next line is the word ‘break’. The last line of the ‘select’ command is the word ‘done’. After the ‘done’ line, we can place whatever lines we want.

The variable we specify will contain the value in the list, not the number selected.

If the ‘break’ was not used, then the ‘select’ statement would loop forever until you press CTRL+C. Try the following script:

#!/bin/bash
echo "Select the month of your birth: "
select month in January February March April May June July August September October November December;
do
break
done
echo "You were born in $month."
exit 0


Make sure that the single line with ‘December’ is included in the list above it. You can place any lines of commands between ‘do’ and ‘done’ or even after ‘done’.

If you choose an invalid number, one that is past the range of items in the list, then the variable will be empty.

In the example, we placed a line to display instructions before the ‘select’ command. We can remove the line and place the prompt in the ‘PS3’ variable, which is used by default with the ‘select’ command. If you use multiple select structures, be sure to change the ‘PS3’ variable as needed. If you use the PS3, then the prompt is placed at the end of the list and not before.

If an item in the list has multiple words with spaces, the item must be placed in double-quotes or it will be split into multiple items.

Conclusion

I hope this helps with making your BASH scripts a little more useful to you. There are still things we need to cover to make your BASH ability very useful.

These parameters will definitely help as we move along and learn more commands. Once we get to the ability to check input values, you should see the usefulness of these parameters.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!


Top