Learning Linux Shell Scripting - Ganesh Sanjiv Naik - E-Book

Learning Linux Shell Scripting E-Book

Ganesh Sanjiv Naik

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Linux is the one of the most powerful and universally adopted OSes. Shell is a program that gives the user direct interaction with the operating system. Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed on the keyboard. Shell scripting is used to automate day-to-day administration, and for testing or product development tasks.

This book covers Bash, GNU Bourne Again SHell, preparing you to work in the exciting world of Linux shell scripting. We start with an introduction to the Shell environment and explain basic commands used in Shell. Next we move on to check, kill, and control the execution of processes in Linux OS. Further, we teach you about the filter tools available in Linux and explain standard output and standard errors devices.
Then we will ensure you understand Shell’s interpretation of commands and get a firmer grasp so you use them in practice. Next, you’ll experience some real-world essentials such as debugging and perform Shell arithmetic fluently. Then you’ll take a step ahead and learn new and advanced topics in Shell scripting, such as starting up a system and customizing a Linux system. Finally, you’ll get to understand the capabilities of scripting and learn about Grep, Stream Editor, and Awk.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 285

Veröffentlichungsjahr: 2015

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Learning Linux Shell Scripting
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started and Working with Shell Scripting
Comparison of shells
Tasks done by shell
Working in shell
Learning basic Linux commands
Our first script – Hello World
Compiler and interpreter – difference in process
When not to use scripts
Various directories
Working more effectively with shell – basic commands
Working with permissions
Changing file permissions
Command chmod
Technique one – the symbolic method
Technique two – the numeric method
Setting umask
Setuid
Setgid
Sticky bit
Summary
2. Drilling Deep into Process Management, Job Control, and Automation
Introducing process basics
Monitoring processes using ps
Process management
Process monitoring tools – top, iostat, and vmstat
Understanding "at"
Understanding "crontab"
Summary
3. Using Text Processing and Filters in Your Scripts
Text filtering tools
Head and tail
The diff command
The cut command
The paste command
The join command
The uniq command
The comm command
The tr command
IO redirection
File descriptors
Redirection
Brace expansion
Pattern matching with the vi editor
Pattern searching using grep
Summary
4. Working with Commands
Learning shell interpretation of commands
Checking and disabling shell internal commands
The exit status
Command substitution
Command separators
Command1; command2
Command grouping
Logical operators
Command1 & command2
Command1 && command2
Command1 || command2
Pipes
Summary
5. Exploring Expressions and Variables
Understanding variables
Working with environment variables
The local variable and its scope
Exporting variables
Working with read-only variables
Working with command line arguments (special variables, set and shift, getopt)
Understanding set
Understanding shift
Resetting positional parameters
Understanding getopts
Understanding default parameters
Working with arrays
Creating an array and initializing it
Accessing array values
Summary
6. Neat Tricks with Shell Scripting
Interactive Shell scripts – reading user input
Summarizing the read command with options
The here document and the << operator
The here operator with the sort command
The here operator with the wc command
The utility ed and here operator
A script for sending messages to all logged-in users
Using the << here operator for FTP usage and data transfer
Turning off variable substitution
The here string and the <<< operator
File handling
Introducing file handling
Using exec to assign file descriptor (fd) to file
Understanding the opening, writing, and closing of a file
Understanding reading from a file
Understanding reading and writing to a file
Using command read on file descriptor (fd)
Reading from one file and writing to another file
Displaying the file descriptor information from the /proc folder
File handling – reading line by line
Executing the command and storing the results in a file
Summarizing usage of the exec command
Debugging
Debugging mode – disabling the shell (option -n)
Debugging mode – displaying commands (option -v)
Debugging mode – the tracing execution (option -x)
Summarizing the debugging options for the Bash shell
Using the set command
Summary of debugging options for set command
The vi editor setting for debugging
Good practices for Shell scripts
Summary
7. Performing Arithmetic Operations in Shell Scripts
Using a command declare for arithmetic
Listing integers
Using the let command for arithmetic
Using the expr command for arithmetic
Using an arithmetic expansion
Binary, octal, and hex arithmetic operations
A floating-point arithmetic
Summary
8. Automating Decision Making in Scripts
Checking the exit status of commands
Understanding the test command
Using the test command with single brackets
Using the test command with double brackets
String comparison options for the test command
Numerical comparison operators for the test command
File test options for the test command
File testing binary operators
Logical test operators
Conditional constructs – if else
Numerical handling if constructs
Using the command exit and the ? variable
String handling with the if construct
Checking for null values
File handling with the if command
Multiple test commands and if constructs
The if/elif/else command
The null command
Switching case
Implementing simple menus with select
Looping with the for command
Exiting from the current loop iteration with the continue command
Exiting from a loop with a break
Working with the do while loop
Using until
Piping the output of a loop to a Linux command
Running loops in the background
The IFS and loops
Summary
9. Working with Functions
Understanding functions
Displaying functions
Removing functions
Passing arguments or parameters to functions
Sharing the data by many functions
Declaring local variables in functions
Returning information from functions
Returning a word or string from a function
Running functions in the background
Command source and period (.)
Creating a library of functions
Summary
10. Using Advanced Functionality in Scripts
Understanding signals and traps
Using the trap command
Ignoring signals
Resetting signals
Listing traps
Using traps in function
Running scripts or processes even if the user logs out
Creating dialog boxes with the dialog utility
Creating a message box (msgbox)
Creating a message box (msgbox) with a title
The yes/no box (yesno)
The input box (inputbox)
The textbox (textbox)
A password box
The menu box (menu)
The checklist box (checklist)
The radiolist box (radiolist)
The progress meter box (gauge)
Summary
11. System Startup and Customizing a Linux System
System startup, inittab, and run levels
The kernel startup and init process
Understanding run levels
System initialization boot scripts
User initialization scripts
Systemwide settings scripts
User level settings – default files
Summary
12. Pattern Matching and Regular Expressions with sed and awk
The basics of regular expressions
sed – noninteractive stream editor
Understanding sed
Understanding regular expression usage in sed
Addressing in sed
How to modify a file with sed
Printing – the p command
Deleting – the d command
Substitution – the s command
Range of selected lines: the comma
Multiple edits – the e command
Reading from files – the r command
Writing to files – the w command
Appending – the a command
Inserting – the i command
Changing – the c command
Transform – the y command
Quit – the q command
Holding and getting – the h and g commands
Holding and exchanging – the h and x commands
sed scripting
Using awk
The meaning of awk
Using awk
Input from files
Input from commands
How awk works
awk commands from within a file
Records and fields
Records
The record separator
The $0 variable
The NR variable
Fields
Field separators
The input field separator
Patterns and actions
Patterns
Actions
Regular expressions
Writing the awk script file
Using variables in awk
Decision making using an if statement
Using the for loop
Using the while loop
Using the do while loop
Summary
Index

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2015

Production reference: 1211215

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78528-621-6

www.packtpub.com

Credits

Author

Ganesh Sanjiv Naik

Reviewers

Advait Deo

Eax Melanhovich

Shawn Solomon

Commissioning Editor

Nadeem Bagban

Acquisition Editor

Tushar Gupta

Content Development Editor

Nikhil Potdukhe

Technical Editor

Rupali R. Shrawane

Copy Editor

Charlotte Carneiro

Project Coordinator

Izzat Contractor

Proofreader

Safis Editing

Indexer

Rekha Nair

Production Coordinator

Manu Joseph

Cover Work

Manu Joseph

About the Author

Ganesh Sanjiv Naik is an author, consultant, and corporate trainer for embedded Android, embedded Linux, and Internet of Things related product development.

He completed his computer engineering in 1988. Since then, he has worked in this industry. He has worked on projects including micro-controller based projects to advanced Embedded Android projects. He has more than 20 years of professional experience and project accomplishment in information technology.

Ganesh has a passion and deep desire for teaching. He has trained 1,000 engineers in Linux and Android product development. He has developed a lot of training material as well as curriculum for various universities and training institutes.

He has an interest in spiritual study and practices such as meditation. He is a certified yoga teacher. His hobbies include yoga and martial arts.

He has worked as a corporate trainer for Indian Space Research Organization, Intel, GE, Samsung, Motorola, Penang Skill Development Center (Malaysia), various companies in Singapore as well as various other corporates in India and other countries. He has started a company called Levana Technologies, which works with the Penang Skill Development Center (Malaysia) for consulting and training activities. If you would like to send feedback, suggestions, or corrections in the book, he can be contacted at https://in.linkedin.com/in/naikganesh.

This book is his real-life experience….

He has worked as a consultant and corporate trainer in the following skills:

Internet of ThingsEmbedded Android, Android internals, and device driver developmentUSB and PCI device driver development in LinuxEmbedded Linux and device driver developmentUnix Shell scripting with sed and awkEmbedded C++ and C programmingOperating systems, software engineering, and networkingProblem solving—analysis, reasoning, and solution techniques for software engineers

Acknowledgments

I would like to thank my wife, Vishalakshi, for putting up with my late night writing sessions. Even though Vishalakshi is from microbiology background, on my request, she proofed all the chapters to spot language shortcomings. She patiently read my complete book, and even after not being a programmer, she gave me many valuable suggestions. Without her support and continuous motivation, this book would not have been possible. My professional friend, Kuldeep Vaity, has also read all chapters from a developer's perspective. His feedback was very valuable from technical angles, such as unclear code, insufficient explanation, and similar.

I feel proud to say that the book's cover picture was designed by my daughter, Roopali. She conceptualized the theme and took a lot of effort to properly convey the meaning of the book to readers.

My special thanks to the technical reviewers of the book. Their valuable suggestions have helped me add value to this book.

I would like to thank the entire Packt Publishing team. I would especially like to thank, Aditya Nair and Nikhil Potdukhe of Packt Publishing for the positive, motivating support given to me during the initial period to the end of the completion of this book. Nikhil was very helpful and patient in following up with me for chapters. Due to my busy schedule of consulting and training activities, I was not able to complete my chapters as per schedule; but Nikhil used to follow up very nicely, understandingly, and patiently for the chapter's completion. Without his patient follow up, either I would have completed this book very late, or I would have left the writing work incomplete.

About the Reviewers

Advait Deo has more than 10 years of experience in database domain, has spent time on many aspects of databases until now, starting from Oracle version 8 until 12c. He mainly focuses on database performance tuning, integrating database with front end application, scripting, and automation. He is currently working as a senior database administrator for (world leader in retail business).

Prior to this book, Advait has reviewed Oracle Database 11g R2 Performance Tuning Cookbook, Packt Publishing (ISBN 1849682607). He also publishes some of his work and learnings on his website at http://avdeo.com.

Eax Melanhovich is 27 and lives in Moscow, Russia. Most people don't know his real name or where he works since Eax is concerned about his privacy. Eax is an author of probably the most popular Russian standalone technical blog eax.me. He is also one of the co-hosts of the popular Russian IT podcast devzen.ru. Eax is an Ubuntu user and a functional programming enthusiast.

Shawn Solomon is a technology veteran with a broad background of experience from more than 20 years of pioneering in various technologies. While working in the ISP, VoIP, educational, open source development, and disaster recovery fields, his skillset has adapted and broadened over the years.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

I wish to dedicate this book to my Gurudev His Holiness Dr. Jayant Balaji Athavale. I wish to express gratitude for his guidance, which I have received for, how to become good human being, good professional and a seeker on the path of spiritual progress.

- Ganesh Sanjiv Naik

Preface

Shell scripts are an essential part of any modern operating system, such as UNIX, Linux, Windows, and similar. The scripting language or its syntax may vary from OS to OS; but the fundamental principles remain the same. I first encountered Linux Shell scripts during the development of embedded Linux product development. Shell scripts were initializing the complete product from the basic booting procedure until users logged in and a complete operating system was initialized. Another situation was in the automation of regular activities, such as the build and release management of source codes of very complex products, where more than 10,000 files were a part of a single project. Similarly, another very common requirement comes while using the make utility, which is used to compile and build complex product source codes.

Initially, I had learned scripts to solve practical problems and customize already existing products. This book is the summary of what I have learned over the years in Linux Shell scripting during project development work, consultancy, and corporate trainings and their Q&A sessions.

In this book, you will learn about the very basics of Shell scripting to complex, customized automation. By the end of this book, you will be able to confidently use your own Shell scripts for the real-world problems out there. The idea is to be as practical as possible and give you the look and feel of what real-world scripting looks like.

This book covers bash, the GNU Bourne-Again Shell scripting. You can use the knowledge gained by reading this book for any shell of any of the UNIX flavors or distributions. You will need to take care of a few syntax changes if you are working in other shells, such as Korn, and similar. You should be able to read this book cover to cover, or you can just pick it up and read anything that you might find interesting. But perhaps most importantly, if you have a question about how to solve a particular problem or you need a hint, you will find it easy to find the right solution or something close enough to save your time and energy.

What this book covers

Chapter 1, Getting Started and Working with Shell Scripting, you will learn different ways to write and run Shell scripts. You will also learn ways to handle files and directories, and you will learn about working with permissions.

Chapter 2, Drilling Deep into Process Management, Job Control, and Automation, you will learn about basic process management. You will learn about command ps and job management using commands such as jobs, fg, bg, kill, and pkill. Later on, you will learn about process monitoring tools: top, iostat, vmstat and sar.

Chapter 3, Using Text Processing and Filters in Your Scripts, you will learn about using more, less, head, and tail commands. You will also learn text processing tools such as, cut, paste, comm, and uniq. You will learn about standard input, output, and error. Later on, you will learn about metacharacters and pattern matching using vi and grep.

Chapter 4, Working with Commands, you will learn about how shell interprets any command entered on the command line. You will also learn command substitution, separators, and pipes in detail.

Chapter 5, Exploring Expressions and Variables, you will learn about variables—environment variables. This will include how to export environment variables, set, shift, read-only variables, command-line arguments, and about creating and handling arrays.

Chapter 6, Neat Tricks with Shell Scripting, you will learn about debugging, the here operator, interactive Shell scripts for taking input from the keyboard, and file handling.

Chapter 7, Performing Arithmetic in Shell Scripts, you will learn about doing arithmetic operations in various ways, such as using declare, let, expr, and arithmetic expressions. You will also learn about representing numbers in different bases, such as hex, octal, and binary. You will learn about using the bc utility for doing floating point or fractional arithmetic.

Chapter 8, Automating Decision Making in Scripts, you will learn about using decision making in scripts working with test, if…else, and switching case. You will also use select for loop with menu. For repeating tasks such as processing lists, you will learn about using for loop, while loop and do while. You will also learn about how to control loops using break and continue statements.

Chapter 9, Working with Functions, we will understand about functions in Shell scripts. You will learn about the definition and display of functions by removing the function from the shell. You will also learn about passing arguments to functions, sharing data between functions, declaring local variables in functions, returning results from functions, and running functions in background. You will finally learn about using source and . commands. We will use these commands to use the library of functions.

Chapter 10, Using Advanced Functionality in Scripts, you will learn about using traps and signals. You will also learn about creating menus with the help of the dialog utility.

Chapter 11, System Startup and Customizing a Linux System, you will learn about Linux system startup from power on until the user login and how to customize a Linux system environment.

Chapter 12, Pattern Matching and Regular Expressions with sed and awk, you will learn about regular expressions and using sed (stream editor) and awk for text processing. You will learn about the usage of various commands and options along with a lot of examples for using sed and awk.

What you need for this book

Any computer that has Linux OS installed on it will be sufficient for learning all the topics discussed in this book. I have personally tested all the commands and scripts in Ubuntu 12.10 distribution.

While learning, if you find that any utility has not installed alongside Ubuntu or any Debian-based distribution, then enter the following command to install this utility:

$ sudo apt-get update$ sudo apt-get install package-name

The Internet should be available for the previous commands to run.

In Red Hat or any other rpm-based distribution, enter the following commands:

$ sudo yum update$ sudo yum install package-name

If the Internet is connected, then using these commands, you can install any command or utility that is not already installed.

Who this book is for

This book is for the readers that are proficient at working with Linux, and who want to learn about Shell scripting to improve their efficiency and practical skills. The following are a few examples where we can use skills learned in this book:

Shell scripting is for automating tasks such as taking a periodic backupSystems administrationDatabase maintenance and backupTest processing and report generationThe customization of system initialization

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "In the output, 4d3 tells us that line number 4 is deleted in file2. Similarly, the change command will show us changes in the file as well."

A block of code is set as follows:

#!/bin/bash # This is comment line echo "Hello World" ls date

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

$ expr 4 * 10 expr: syntax error With command expr, we cannot use * for multiplication. We need to use \* for multiplication. $ expr "4 * 10" 4 * 10 $ expr 4 \* 10 40

Any command-line input or output is written as follows:

$ sed '1,3d' datafile > tempfile$ awk -F: '/Marie/{print $1, $2}' people1.txt

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/LearningLinuxShellScripting_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Getting Started and Working with Shell Scripting

Whoever works with Linux will come across shell as the first program to work with. The Graphical user interface (GUI) usage has become very popular due to the ease of use. Those who want to take advantage of the power of Linux will use the shell program by default.

Shell is a program which provides the user direct interaction with the operating system. Let's understand the stages in the evolution of the Linux operating system. Linux was developed as a free and open source substitute for UNIX OS. The chronology can be as follows:

The UNIX operating system was developed by Ken Thomson and Dennis Ritchie in 1969. It was released in 1970. They rewrote the UNIX using C language in 1972.In 1991, Linus Torvalds developed the Linux kernel for the free operating system.

In this chapter, we will cover the following topics:

Comparison of shellsWorking in shellLearning basic Linux commandsOur first script—Hello WorldCompiler and interpreter—difference in processWhen not to use scriptsVarious directoriesWorking more effectively with shell—basic commandsWorking with permissions

Comparison of shells

Initially, the UNIX OS used a shell program called Bourne Shell. Then eventually, many more shell programs were developed for different flavors of UNIX. The following is brief information about different shells:

Sh—Bourne ShellCsh—C ShellKsh—Korn ShellTcsh—enhanced C ShellBash—GNU Bourne Again ShellZsh—extension to Bash, Ksh, and TcshPdksh—extension to KSH

A brief comparison of various shells is presented in the following table:

Feature

Bourne

C

TC

Korn

Bash

Aliases

no

yes

yes

yes

yes

Command-line editing

no

no

yes

yes

yes

Advanced pattern matching

no

no

no

yes

yes

Filename completion

no

yes

yes

yes

yes

Directory stacks (pushd and popd)

no

yes

yes

no

yes

History

no

yes

yes

yes

yes

Functions

yes

no

no

yes

yes

Key binding

no

no

yes

no

yes

Job control

no

yes

yes

yes

yes

Spelling correction

no

no

yes

no

yes

Prompt formatting

no

no

yes

no

yes

What we see here is that, generally, the syntax of all these shells is 95% similar. In this book, we are going to follow Bash shell programming.

Tasks done by shell

Whenever we type any text in the shell terminal, it is the responsibility of shell to execute the command properly. The activities done by shell are as follows:

Reading text and parsing the entered commandEvaluating metacharacters such as wildcards, special characters, or history charactersProcess io-redirection, pipes, and background processingSignal handlingInitializing programs for execution

We will discuss the preceding topics in the subsequent chapters.

Working in shell

Let's get started by opening the terminal, and we will familiarize ourselves with the Bash Shell environment:

Open the Linux terminal and type in:
$ echo $SHELL/bin/bash
The preceding output in terminal says that the current shell is /bin/bash such as BASH shell:
$ bash --versionGNU bash, version 2.05.0(1)-release (i386-redhat-linux-gnu)Copyright 2000 Free Software Foundation, Inc.

Hereafter, we will use the word Shell to signify the BASH shell only. If we intend any other shell, then it will be specifically mentioned by name such as KORN and similar other shells.

In Linux, filenames in lowercase and uppercase are different; for example, the files Hello and hello are two distinct files. This is unlike Windows, where case does not matter.

As far as possible, avoid using spaces in filenames or directory names such as:

Wrong file name—Hello World.txtCorrect file name—Hello_World.txt or HelloWorld.txt

This will make certain utilities or commands fail or not work as expected, for example, the make utility.

While typing in filenames or directory names of the existing files or folders, use the tab completion feature of Linux. This will make working with Linux faster.

Learning basic Linux commands

The following table lists a few basic Linux commands:

Command

Description

$ ls

This command is used to check the contents of the directory.

$ pwd

This command is used to check the present working directory.

$ mkdir work

We will work in a separate directory called work in our home directory. Use this command to create a new directory called work in the current folder.

$ cd work

This command will change our working directory to the newly created directory work.

$ pwd

This command can be used to verify whether we moved to the expected directory.

$ touch hello.sh

This command is used to create a new empty file called hello.sh in the current folder.

$ cp hello.sh bye.sh

This command is used to copy one file into another file.

This will copy hello.sh as bye.sh.

$ mv bye.sh welcome.sh

This command is used to rename a file. This will rename bye.sh as welcome.sh.

$ ll

This command will display detailed information about files.

$ mv welcome.sh .welcome.sh

$ ls

Let's see some magic. Rename the file using the mv command and the run the ls command.

Now, the ls command will not display our file .welcome.sh. That file gets hidden. Any filename or directory name starting with "." (dot) becomes hidden.

$ ls -a

This command is used to see hidden files.

$ rm .welcolme.sh

This command is used to delete the file.

Note

If we delete any file from GUI such as Graphical User Interface, then it will be moved to the /home/user/.local/share/Trash/files/ all deleted files folder.

Our first script – Hello World

Since we learned basic commands to use Linux OS, we will now write our first Shell script called hello.sh. You can use any editor of your choice such as vi, gedit, nano, and other similar editors. I prefer to use the vi editor.

Create a new hello.sh file as follows:
#!/bin/bash# This is comment lineecho "Hello World"lsdate
Save the newly created file.

The #!/bin/bash line is called the shebang line. The combination of the characters # and ! is called the magic number. The shell uses this to call the intended shell such as /bin/bash in this case. This should always be the first line in a Shell script.

The next few lines in the Shell script are self explanatory.

Any line starting with #, will be treated as a comment line. An exception to this would be the first line with #!/bin/bashThe echo command will print Hello World on the screenThe ls command will display directory content on the consoleThe date command will show the current date and time

We can execute the newly created file by the following commands:

Technique one:
$ bash hello.sh
Technique two:
$ chmod +x hello.sh

By running any of the preceding commands, we are adding executable permission to our newly created file. You will learn more about file permissions in later in this same chapter.

$ ./hello.sh

By running the preceding command, we are executing hello.sh as the executable file. By technique one, we passed filename as an argument to Bash shell.

The output of executing hello.sh will be as follows:

Hello Worldhello.shSun Jan 18 22:53:06 IST 2015

Since we have successfully executed our first script, we will proceed to develop a more advanced script, hello1.sh. Please create the new script hello.sh as follows:

#!/bin/bash# This is the first Bash shell# Scriptname : Hello1.sh# Written by: Ganesh Naikecho "Hello $LOGNAME, Have a nice day !"echo "Your are working in directory `pwd`."echo "You are working on a machine called `uname -n`."echo "List of files in your directory is."ls # List files in the present working directoryecho "Bye for now $LOGNAME. The time is `date +%T`!"

The output of executing hello.sh will be as follows:

Hello student, Have a nice day !.Your are working in directory /home/student/work.You are working on a machine called ubuntu.List of files in your directory is.hello1.sh hello.shBye for now student. The time is 22:59:03!

You will learn about the LOGNAME, uname, and other similar commands as we go on with the book.

Compiler and interpreter – difference in process

In any program development, the following are the two options:

Compilation: Using a compiler-based language such as C, C++, Java, and other similar languagesInterpreter: Using interpreter-based languages such as Bash Shell scripting.

When we use a compiler-based language, we compile the complete source code, and as a result of compilation, we get a binary executable file. We then execute the binary to check the performance of our program.