Book Image

Mastering Linux Shell Scripting

By : Andrew Mallett
Book Image

Mastering Linux Shell Scripting

By: Andrew Mallett

Overview of this book

Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences complex problems can be solved with ease, from text processing to backing up sysadmin tools. In this book, you’ll discover everything you need to know to master shell scripting and make informed choices about the elements you employ. Get to grips with the fundamentals of creating and running a script in normal mode, and in debug mode. Learn about various conditional statements' code snippets, and realize the power of repetition and loops in your shell script. Implement functions and edit files using the Stream Editor, script in Perl, program in Python – as well as complete coverage of other scripting languages to ensure you can choose the best tool for your project.
Table of Contents (21 chapters)
Mastering Linux Shell Scripting
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using the test shell builtin


It is probably time for us to pull over to the side of the scripting highway and look a little more at this command test. This is both a shell builtin and a file executable in its own right. Of course, we will have to hit the built-in command first, unless we specify the full path to the file.

When the test command is run without any expressions to evaluate, then the test will return false. So, if we run the test as shown in the following command:

$ test

The exit status will be 1, even though no error output is shown. The test command will always return either True or False or 0 or 1, respectively. The basic syntax of test is:

test EXPRESSION

Or, we can inverse the test command with:

test ! EXPRESSION

If we need to include multiple expressions, these can be AND or OR together using the -a and -o options, respectively:

test EXPRESSION -a EXPRESSION
test EXPRESSION -o EXPRESSION

We can also write in a shorthand version replacing the test with square brackets to surround...