0% found this document useful (0 votes)
16 views37 pages

Introduction to Linux Shell Scripting

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

Introduction to Linux Shell Scripting

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SCRIPTING

Introduction to Linux Shell and Shell Scripting


• If we are using any major operating system, we are indirectly interacting with the
shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are
interacting with the shell by using the terminal.
• 3 terminologies:
• Kernel
• Shell
• Terminal
WHAT IS KERNEL?

• The kernel is a computer program that is the core of a computer’s operating system,
with complete control over everything in the system. It manages the following
resources of the Linux system –
• File management
• Process management
• I/O management
• Memory management
• Device management etc.
LINUS TORVALDS

• has developed Linux OS, but actually, he is only responsible for the development of
the Linux kernel.
Complete Linux system = Kernel + GNU system utilities and libraries + other
management scripts + installation scripts.
WHAT IS SHELL?
• is a special user program that provides an interface for the user to use operating
system services. Shell accepts human-readable commands from users and converts
them into something which the kernel can understand. It is a command language
interpreter that executes commands read from input devices such as keyboards or
from files. The shell gets started when the user logs in or starts the terminal.
SHELL IS BROADLY CLASSIFIED INTO TWO
CATEGORIES –

• Command Line Shell


• Graphical shell
COMMAND LINE SHELL
• Shell can be accessed by users using a command line interface. A special program
called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided to
type in the human-readable commands such as “cat”, “ls” etc. and then it is being
executed. The result is then displayed on the terminal to the user.
COMMAND LINE SHELL

A terminal in Ubuntu 16.4 system


• “ls” command with “-l” option is executed. It will list all the files in the current
working directory in a long listing format.
• Note: Working with a command line shell is a bit difficult for beginners because it’s hard to
memorize so many commands. It is very powerful; it allows users to store commands in a file
and execute them together. This way any repetitive task can be easily automated. These files
are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.
GRAPHICAL SHELLS
• provide means for manipulating programs based on the graphical user interface
(GUI), by allowing for operations such as opening, closing, moving, and resizing
windows, as well as switching focus between windows. Window OS or Ubuntu OS can
be considered as a good example which provides GUI to the user for interacting with
the program. Users do not need to type in commands for every action.
A typical GUI in the Ubuntu system –
THERE ARE SEVERAL SHELLS ARE AVAILABLE FOR
LINUX SYSTEMS LIKE –

• BASH (Bourne Again Shell) – It is the most widely used shell in Linux systems. It is
used as default login shell in Linux systems and in macOS. It can also be installed on
Windows OS.
• CSH (C Shell) – The C shell’s syntax and its usage are very similar to the C
programming language.
• KSH (Korn Shell) – The Korn Shell was also the base for the POSIX Shell standard
specifications etc.
WHAT IS A TERMINAL?
• A program which is responsible for providing an interface to a user so that he/she
can access the shell. It basically allows users to enter commands and see the output
of those commands in a text-based interface. Large scripts that are written to
automate and perform complex tasks are executed in the terminal.
To access the terminal, simply search in search box “terminal” and double-click it.
SHELL SCRIPTING
• are interactive, which means they accept commands as input from users and execute
them. However, sometimes we want to execute a bunch of commands routinely, so
we have to type in all commands each time in the terminal.

• As a shell can also take commands as input from file, we can write these commands
in a file and can execute them in shell to avoid this repetitive work. These files are
called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-
DOS. Each shell script is saved with `.sh` file extension e.g., [Link].
A SHELL SCRIPT COMPRISES THE FOLLOWING ELEMENTS –

• Shell Keywords – if, else, break etc.


• Shell commands – cd, ls, echo, pwd, touch etc.
• Functions
• Control flow – if..then..else, case and shell loops etc.
WHY DO WE NEED SHELL SCRIPTS?

• There are many reasons to write shell scripts:


• To avoid repetitive work and automation
• System admins use shell scripting for routine backups.
• System monitoring
• Adding new functionality to the shell etc.
SOME ADVANTAGES OF SHELL SCRIPTS

• The command and syntax are exactly the same as those directly entered in the
command line, so programmers do not need to switch to entirely different syntax
• Writing shell scripts are much quicker
• Quick start
• Interactive debugging etc.
SOME DISADVANTAGES OF SHELL SCRIPTS
• Prone to costly errors, a single mistake can change the command which might be
harmful.
• Slow execution speed
• Design flaws within the language syntax or implementation
• Not well suited for large and complex task
• Provide minimal data structure unlike other scripting languages. etc.
WHAT IS A BASH SCRIPT?

• A bash script is a series of commands written in a file. These are read and executed
by the bash program. The program executes line by line.
HOW DO YOU IDENTIFY A BASH SCRIPT?

• File extension of .sh.


• By naming conventions, bash scripts end with a .sh. However, bash scripts can run
perfectly fine without the sh extension.
EXECUTION RIGHTS

• Scripts have execution rights for the user executing them.


• An execution right is represented by x. In the example below, my user has the rwx
(read, write, execute) rights for the file test_script.sh\
CREATE YOUR FIRST BASH SCRIPT
Thank you for listening! I hope you learned
from these topics!

You might also like