Learning Linux Shell Scripting - Sample Chapter
Learning Linux Shell Scripting - Sample Chapter
pl
$ 49.99 US
31.99 UK
C o m m u n i t y
P U B L I S H I N G
Sa
m
ee
D i s t i l l e d
E x p e r i e n c e
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.
Internet of Things
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.
Preface
Preface
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.
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.
Comparison of shells
Working in shell
Learning basic Linux commands
Our first scriptHello World
Compiler and interpreterdifference in process
When not to use scripts
Various directories
Working more effectively with shellbasic commands
Working with permissions
[1]
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:
ShBourne Shell
CshC Shell
KshKorn Shell
Tcshenhanced C Shell
Pdkshextension to KSH
Bourne
TC
Korn
Bash
Aliases
no
yes
yes
yes
yes
Command-line editing
no
no
yes
yes
yes
no
no
no
yes
yes
Filename completion
no
yes
yes
yes
yes
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.
[2]
Chapter 1
Signal handling
Working in shell
Let's get started by opening the terminal, and we will familiarize ourselves with the
Bash Shell environment:
1. Open the Linux terminal and type in:
$ echo $SHELL
/bin/bash
2. The preceding output in terminal says that the current shell is /bin/bash
such as BASH shell:
$ bash --version
GNU 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:
[3]
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.
Description
$ pwd
$ mkdir work
$ cd work
$ pwd
$ touch hello.sh
$ cp hello.sh bye.sh
$ ll
$ mv welcome.sh .welcome.sh
$ ls
$ rm .welcolme.sh
[4]
Chapter 1
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.
The date command will show the current date and time
Technique one:
$ bash hello.sh
Technique two:
$ chmod +x hello.sh
[5]
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 Naik
hello.sh
You will learn about the LOGNAME, uname, and other similar commands as we go on
with the book.
[6]
Chapter 1
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.
On the contrary, when we develop the Shell script, such as an interpreter-based
program, every line of the program is input to Bash shell. The lines of Shell script are
executed one by one sequentially. Even if the second line of a script has an error, the
first line will be executed by the shell interpreter.
Every line in Shell script creates a new process in the operating system.
When we execute the compiled program such as C program, it runs as a
single process for the complete program.
Since every command creates a new process, Shell scripts are slow as
compared to compiled programs.
Shell scripts are not suitable if heavy math operations are involved.
[7]
Various directories
We will explore the directory structure in Linux so that it will be useful later on:
/boot/: The files required for the operating system startup are stored here.
/cdrom/: When CD-ROM is mounted, the CD-ROM files are accessible here.
/dev/: The device driver files are stored in this folder. These device driver
/home/: This folder contains a home folder of all users except the administrator.
/media/: External media such as a USB pen drive is mounted in this folder.
/proc/: This contains files which give information about kernel and every
process running in OS.
/var/: This contains variable data such as http, tftp, and similar other.
[8]
Chapter 1
Enter the following command. It will show the various types of manual
pages displayed by the man command:
$ man man
From the following table, you can get an idea about various types of man
pages for the same command:
Section number
Subject area
User commands
System calls
Library calls
Special files
File formats
Games
Miscellaneous
System admin
Kernel routines
Suppose we need to know more about the passwd command, which is used
for changing the current password of a user, you can type the command
as follows:
$ man command
man -k passwd
man K passwd
$ man passwd
[9]
The preceding command will give information about the file passwd, which
is stored in /etc /passwd.
Output:
passwd (1ssl)
passwd (1)
passwd (5)
The preceding line tells us that the binary file of the passwd command is
located in the /usr/bin/passwd folder.
We can get complete information about the binary file location as well as
manual page location of any command by following:
$ whereis passwd
The su command (switch user) will make the user as the administrator;
but, you should know the administrators, password. The sudo command
(superuser do) will run the command with administrator's privileges. It is
necessary that the user should have been added in the sudoers list.
# who am i
This command will show the effective user who is working at that moment.
# exit
[ 10 ]
Chapter 1
Many a times, you might need to create new commands from existing
commands. Sometimes, existing commands have complex options to
remember. In such cases, we can create new commands as follows:
$ alias ll='ls l'
$ alias copy='cp rf'
We can check about the operating system details such as UNIX/Linux or the
distribution that is installed by the following command:
$ uname
Output:
Linux
Output:
3.13.0-32-generic
To get all the information about a Linux machine, use the following command:
$ uname a
Output:
Linux ubuntu 3.13.0-32-generic #57~precise1-Ubuntu SMP Tue Jul 15
03:50:54 UTC 2014 i686 i686 i386 GNU/Linux
The following commands will give you more information about the
distribution of Linux:
$ cat /proc/version
$ cat /etc/*release
# lsb_release -a
The command cat is used for reading files and displayed on the
standard output.
[ 11 ]
-s
To learn about the type of file, you can use the command file. In Linux,
various types of files exist. Some examples are as follows:
Directory (d)
file file_link
fil_name
Printing some text on the screen for showing results to the user or to ask
details is an essential activity.
>
But this is very rarely used, as many powerful editors are already existing,
such as vi or gedit.
The following command will copy the string Hello World to the
hello.c file:
[ 12 ]
hello.c
Chapter 1
The command echo with > overwrites the content of the file. If
content already exists in the file, it will be deleted and new content
will be added in the file. In a situation, when we need to append the
text to the file, then we can use the echo command as follows:
$ echo
The following command will display the content of the file on screen:
$ cat hello.c
Read permission: The user can read or check the content of the file
[ 13 ]
Command chmod
We can change the file or directory permissions by the following two ways:
The file permission 777 can be understood as 111 111 111, which corresponds
to the rwx.rwx.rwx permissions.
Setting umask
We will see how Linux decides the default permissions of the newly created file
or folder:
$ umask
0002
The meaning of the preceding output is that, if we create a new directory, then from
the permissions of +rwx, the permission 0002 will be subtracted. This means that for
a newly created directory, the permissions will be 775 or rwx rwx r-x. For a newly
created file, the file permissions will be rw- rw- r--. By default, for any newly
created text file, the execute bit will never be set. Therefore, the newly created text
file and directory will have different permissions even though the umask is same.
[ 14 ]
Chapter 1
Setuid
Another very interesting functionality is the setuid feature. If the setuid bit is set
for a script, then the script will always run with the owner's privileges irrespective
of which user is running the script. If the administrator wants to run script written
by him by other users, then he can set this bit.
Consider either of the following situations:
$ chmod u+s file_name
$ chmod 4777 file
The file permissions after any of the preceding two commands will be drwsrwxrwx.
Setgid
Similar to setuid, the setgid functionality gives the user the ability to run scripts
with group owner's privileges, even if it is executed by any other user.
$ chmod g+s filename
File permissions after any of the preceding two commands will be drwxrwsrwtx.
Sticky bit
Sticky bit is a very interesting functionality. Let's say, in the administration
department there are 10 users. If one folder has been set with sticky bit, then all other
users can copy files to that folder. All users can read the files, but only the owner of
the respective file can edit or delete the file. Other user can only read but not edit or
modify the files if the sticky bit is set.
$ chmod +t filename
File permissions after any of the preceding two commands will be drwxrwxrwt.
[ 15 ]
Summary
In this chapter, you learned different ways to write and run Shell scripts. You also
learned ways to handle files and directories as well as work with permissions.
In the next chapter, you will learn about process management, job control,
and automation.
[ 16 ]
www.PacktPub.com
Stay Connected: