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

Hello World


To create a simple Perl script, we can use the text editor of choice. For short scripts vi or vim works well, as does gedit if you want to work in GUI. For larger projects an IDE may help. Often, the IDE will allow you to change the object name easily throughout the script and provide expansion of object names. In this chapter, we will make use of very simple scripts so we will continue to use vi.

We will create a $HOME/bin/hello.pl file to produce the output we want:

#!/usr/bin/perl
print("Hello World\n");

The file still needs to be in a directory within our PATH variable; hence, we create $HOME/bin. If it is not in the PATH variable then we will need to specify the full or relative path of the file, as with bash.

The file will need to be set with the execute permission. We can do this with the following command:

$ chmod u+x $HOME/bin/hello.pl

We can run the script with the following command:

$ hello.pl

We can see that the code that we have added is the same as the one we ran the...