read timeout in scripting

The command read is used in scripting to accept a value for a variable from the user.

The default operation of read is to wait infinetly for the user to enter a value. This behavour can be changed and read can be made to wait for a specific time only and abort after that.
This can be done by using the option "-t" with read i.e.

This will make read to wait for 5 seconds before aborting.
The script below shows how to use the option.

The above script waits for 5 seconds for the user to enter a value,if the user fails to do so the script will continue to run and throw an error message.

Save it,give it execute permissions and execute it

If we do not enter any value at the read prompt, after 5 seconds we should see the following message.


Follow by Email