skip to main content

SAP NetWeaver Newbie

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?

2 comments:

  1. read ANSWER?"Choose a number" does not work on /bin/sh on Solaris.

    ReplyDelete
    Replies
    1. That's right. You can use echo and read to have similar output as with ?

      Example:
      echo "Enter your name: \c"
      read ANSWER

      Delete

Email Subscription

Get every new post into your inbox by subscribing us.

Want a reason to subscribe?
1. This sitemap might convince you to subscribe.
2. We do not misuse email IDs. We respect privacy.

© 2008 - 2017 sapnwnewbie. All rights reserved.