Linux and Networking

Linux and Networking

4.9. Interaction with the user.

Scripts, as a general rule, are not very interactive, but in some cases, such as menus, it is necessary to ask the user to choose an option, request information, etc.

 

We do these interactive readings with users using the read command. Let’s look at a very simple example of a script with a read command:

 

#!/bin/bash

echo Please enter your name

read NAME

echo “Hello $NAME, how are you?”

 

Even if you want, you can put several readings in the same order read:

 

#!/bin/bash

echo Please enter your first and last name

read NAME SURNAME

echo “Hello $NAME $LAST NAME, how are you?”

 

There is a slightly more professional way to read variables and that is to add the -n option.

 

echo -n «Enter a parameter:»

read VAR

echo “The parameter value is $VAR”

 

This way it reads the parameter on the same line that writes the text and not on the next one.

The Demeter Project