- read - read user input from stdin.
- -r This option will set read to treat backslashes as regular characters.
Without it, backslashes will quote the next character.
- echo - Before using the read command to read input from a user we should prompt the user so they
know what to do. By defaut, echo will add a newline at the end of it's output.
echo has character sequences, called escape characters, that have special meaning. These are listed
on page 212. One useful one is \c.
It will prevent the linefeed at the end of the echo command. The bash shell has a -n option
that does the same thing. So, both of the following do the same thing:
# echotest - show how to supress lf
echo -e "Enter data: \c"
read a
echo -n "Enter data: "
read b
Note: The textbook assumes that escape characters will work by default.
The default configuration for bash shuts off escape characters. Adding the -e option will enable it.
If you would like to enable it all the time you can use the command shopt -s xpg_echo to enable it.
- Yes/No - This can be used to ask the user if they want to do something.
The following script is an example of this.
# ask - an example of using read to ask a user if they what to do something.
echo -n "Do you want to format the hard drive? (yes/no): "
read answer
if [ "$answer" = yes ]
then
echo "Okay. Here goes nothing..."
else
echo "Good idea."
fi
- Menu - We can expand this further to give the user several choices of input.
This is known as a menu driven program.
# mymenu - an example of a menu driven program
echo What would you like to do?
echo " 1) Delete all files"
echo " 2) Format the hard drive"
echo " 3) Send spam to all my freinds"
echo " 4) Exit"
echo -n "Choice: "
read answer
case $answer in
1) echo "Deleting all files..." ;;
2) echo "Formating hard drive..." ;;
3) echo "Sending spam..." ;;
*) exit ;;
esac
- $$ variable - This special variable contains the PID (process id) number. Each process has a unique
PID which can be used to prevent multiple copies of a script from overwriting each other's temporary files. Temp
files should be stored in the /tmp/ directory. In the following example, a temp file is created using
the $$ variable to make a unique filename.
# rfile - a script that creates a temp file
tempfile="/tmp/rfile$$"
# echo PID and temp file name just so
# you can see that it changes every time
echo "PID is $$"
echo "Temp file is $tempfile"
ls /etc/ > $tempfile
ls /bin/ >> $tempfile
grep 'sh' $tempfile
rm -f $tempfile
- reading files - Because read gets its data from standard input (stdin), files can be redirected
to it for reading. The most common way to do this is to redirect stdin on a loop. The read command will always
have an exit status of zero unless it gets an end of file (EOF) character (Ctl-D). Therefore, if the read is
the condition of a while loop, it will loop for each line of the file. The following example will take pairs
of numbers from a file and add them together.
# addnums
# add pairs of numbers from the file numfile
while read a b
do
echo "$a plus $b equals $((a+b))"
done < numfile
numfiles would simply contain the numbers 2 per line:
$ cat numfile
4 6
45 7
32 89
44 213
- printf - provides a way to control the exact format of the output.
This is especially useful when you need to output columns. The format is:
printf "format" arg1 arg2 ...
Any text included in format will be displayed on stdout except for conversion
specifications and escape codes. The escape codes are the same as for echo command
except that they work by default in Linux. Formatting conversions begin with a percent
sign (%) and are of the format:
%[flag][width][.precision][type]
This looks more complicated than it is.
- type - The type defines what kind of value we are dealing with.
The most commonly used ones are s and d. s specifies a string and
d specifies an integer. (see page 229)
- width - specifies the width to use to display the value. Extra space will be padded with spaces.
- flag - For a string, - will left justify text, For a number, + will put a plus sign
before positive numbers. There are others, but these are the most common (see page 232).
- precision - Minimum number of digits for integers, maximum number of characters for strings.