About the Author
Dan Barrett has been immersed in Internet technology since 1985. Currently working as a software engineer, Dan has also been a heavy metal singer, Unix system administrator, university lecturer, web designer, and humorist. He has written several O'Reilly books, as well as monthly columns for Compute! and Keyboard Magazine. Dan and his family reside in Boston.
Excerpt. © Reprinted by permission. All rights reserved.
Earlier when we covered the shell (bash), we said it had a programming language built in. In fact, you can write programs, or shell scripts, to accomplish tasks that a single command cannot. Like any good programming language, the shell has variables, conditionals (if-then-else), loops, input and output, and more. Entire books have been written on shell scripting, so well be covering the bare minimum to get
you started. For full documentation, run info bash.
Whitespace and Linebreaks
bash shell scripts are very sensitive to whitespace and linebreaks. Because the "keywords" of this programming language are actually commands evaluated by the shell, you need to separate arguments with whitespace. Likewise, a linebreak in the middle of a command will mislead the shell into thinking the command is incomplete. Follow the conventions we present here and you should be fine.
We described variables earlier:
$ echo $MYVAR
All values held in variables are strings, but if they are numeric the shell will treat them as numbers when appropriate.
$ expr $NUMBER + 5
When you refer to a variables value in a shell script, its a good idea to surround it with double quotes to prevent certain runtime errors. An undefined variable, or a variable with spaces in its value, will evaluate to something unexpected if not surrounded by quotes, causing your script to malfunction.
$ FILENAME="My Document" Space in the name
$ ls $FILENAME Try to list it
ls: My: No such file or directory Oops! Ls saw 2 arguments
ls: Document: No such file or directory
$ ls -l "$FILENAME" List it properly
My Document ls saw only 1 argument
If a variable name is evaluated adjacent to another string, surround it with curly braces to prevent unexpected behavior:
$ echo "The plural of $HAT is $HATs"
The plural of fedora is Oops! No variable "HATs"
$ echo "The plural of $HAT is s"
The plural of fedora is fedoras What we wanted
Input and Output
Script output is provided by the echo and printf commands, which we described in "Screen Output" on page 144:
$ echo "Hello world"
$ printf "I am %d years old\n" `expr 20 + 20`
I am 40 years old
Input is provided by the read command, which reads one line from standard input and stores it in a variable:
$ read name
$ echo "I read the name $name"
I read the name Sandy Smith
Booleans and Return Codes
Before we can describe conditionals and loops, we need the concept of a Boolean (true/false) test. To the shell, the value 0 means true or success, and anything else means false or failure.
Additionally, every Linux command returns an integer value, called a return code or exit status, to the shell when the command exits. You can see this value in the special variable $?:
$ cat myfile
My name is Sandy Smith and
I really like Fedora Linux
$ grep Smith myfile
My name is Sandy Smith and A match was found...
$ echo $?
0 ...so return code is "success"
$ grep aardvark myfile
$ echo $? No match was found...
1 ...so return code is "failure"
The return codes of a command are usually documented on its manpage.