OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Options

read supports the following options:


Table 28-1: read Options


Option Description

Option Description

-a array Assign the input to array, starting with index zero. We

will cover arrays in Chapter 35.


image

-d delimiter The first character in the string delimiter is used to

indicate end of input, rather than a newline character.


image

-e Use Readline to handle input. This permits input editing in the same manner as the command line.


image

-i string Use string as a default reply if the user simply presses

Enter. Requires the -e option.


image

-n num Read num characters of input, rather than an entire line.


image

-p prompt Display a prompt for input using the string prompt.


image


image

-r Raw mode. Do not interpret backslash characters as escapes.


image

-s Silent mode. Do not echo characters to the display as they are typed. This is useful when inputting passwords and other confidential information.


image

-t seconds Timeout. Terminate input after seconds. read returns a

non-zero exit status if an input times out.


image

-u fd Use input from file descriptor fd, rather than standard input.


image


Using the various options, we can do interesting things with read. For example, with the

-p option, we can provide a prompt string:


#!/bin/bash


# read-single: read multiple values into default variable read -p "Enter one or more values > "

echo "REPLY = '$REPLY'"

#!/bin/bash


# read-single: read multiple values into default variable read -p "Enter one or more values > "

echo "REPLY = '$REPLY'"


With the -t and -s options we can write a script that reads “secret” input and times out if the input is not completed in a specified time:



#!/bin/bash


# read-secret: input a secret passphrase


if read -t 10 -sp "Enter secret passphrase > " secret_pass; then echo -e "\nSecret passphrase = '$secret_pass'"

else

echo -e "\nInput timed out" >&2 exit 1

fi

#!/bin/bash


# read-secret: input a secret passphrase


if read -t 10 -sp "Enter secret passphrase > " secret_pass; then echo -e "\nSecret passphrase = '$secret_pass'"

else

echo -e "\nInput timed out" >&2 exit 1

fi


The script prompts the user for a secret passphrase and waits 10 seconds for input. If the entry is not completed within the specified time, the script exits with an error. Since the

-s option is included, the characters of the passphrase are not echoed to the display as they are typed.


It's possible to supply the user with a default response using the -e and -i options to- gether:



#!/bin/bash

# read-default: supply a default value if user presses Enter key. read -e -p "What is your user name? " -i $USER

echo "You answered: '$REPLY'"

#!/bin/bash

# read-default: supply a default value if user presses Enter key. read -e -p "What is your user name? " -i $USER

echo "You answered: '$REPLY'"


In this script, we prompt the user to enter his/her user name and use the environment vari- able USER to provide a default value. When the script is run it displays the default string and if the user simply presses the Enter key, read will assign the default string to the REPLY variable.


[me@linuxbox ~]$ read-default What is your user name? me You answered: 'me'

[me@linuxbox ~]$ read-default What is your user name? me You answered: 'me'


Top OS Cloud Computing at OnWorks: