Interactive Shell Scripts - Read Command

The read command accepts input from terminal or a file until the input reaches a newline.

Examples

read
reads from the std input and assigns it to variable REPLY

read NAME
reads from the std input and assigns it to variable NAME

read x y
reads the first std input till the first space and assignes it to x. the rest of the input (till new line) is assigned to y.

read ANSWER?"Choose a number"
Displays "Choose a number", reads the input and assigns it to ANSWER.

Important option flags

-A                    input is stored as an array
-d char             uses specified char as delimiter, instead of the default delimiter - newline
-t num_sec       waits for num_sec seconds for a response
-un                   reads from file descriptor n

A known problem is discussed here. For some reason if the read command doesn't work use the following example:
read RESPONSE < /dev/tty


Further Reading

Check out Wicked Cool Shell Scripts by Dave Taylor.
Just reading through the table of contents is inspiring and intriguing; did you know you could write a script to retrieve movie info from IMDb? or track the value of your stock portfolio? or that you can use a very simple script to check spelling on your web pages?

Comments

Popular posts from this blog

OS/DB Migration - CMD. STR, TOC, EXT, R3load, DDLDBS.TPL and more

Fixing Inconsistent Table - Table activation fails due to inconsistency between DD and DB

301 Redirect Using SAP Web Dispatcher