Linux Pocket Guide (Pocket Guide: Essential Commands) and over 2 million other books are available for Amazon Kindle . Learn more

Buy Used
Used - Good See details
Price: £1.92

or
 
   
Have one to sell? Sell yours here
Start reading Linux Pocket Guide (Pocket Guide: Essential Commands) on your Kindle in under a minute.

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

Linux Pocket Guide (Pocket Guide: Essential Commands) [Paperback]

Daniel J. Barrett
4.6 out of 5 stars  See all reviews (23 customer reviews)

Available from these sellers.


‹  Return to Product Overview

Product Description

Review

Cleverly avoiding the unnecessary bloat associated with titles that share the same subject matter -- Linux User & Developer

Book Description

Essential Commands

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.

Programming with Shell Scripts

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 we’ll 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.

Variables
We described variables earlier:

$ MYVAR=6
$ echo $MYVAR
6

All values held in variables are strings, but if they are numeric the shell will treat them as numbers when appropriate.

$ NUMBER="10"
$ expr $NUMBER + 5
15

When you refer to a variable’s value in a shell script, it’s 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:

$ HAT="fedora"
$ 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"
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
Sandy Smith
$ 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.

‹  Return to Product Overview