Nothing Special   »   [go: up one dir, main page]

Shell Scripting: Priyanka Suryawanshi

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

SHELL SCRIPTING

Priyanka Suryawanshi
SHELL SCRIPTING
• Shell Scripting is an open-source computer program designed to be
run by the Unix/Linux shell.

• Shell Scripting is a program to write a series of commands for the


shell to execute.

• It can combine lengthy and repetitive sequences of commands into


a single and simple script that can be stored and executed anytime
which, reduces programming efforts.
WHAT IS SHELL?
• Shell is a UNIX term for an interface between a user and an operating
system service.

• Shell provides users with an interface and accepts human-readable


commands into the system and executes those commands which can
run automatically and give the program’s output in a shell script.

• An Operating is made of many components, but its two prime


components are –
• Kernel
• Shell
• A Kernel is at the nucleus of a computer. It makes the communication between
the hardware and software possible. While the Kernel is the innermost part of an
operating system, a shell is the outermost one.

• A shell in a Linux operating system takes input from you in the form of
commands, processes it, and then gives an output. It is the interface through
which a user works on the programs, commands, and scripts. A shell is accessed
by a terminal which runs it.

• When you run the terminal, the Shell issues a command prompt (usually
$), where you can type your input, which is then executed when you hit the Enter
key. The output or the result is thereafter displayed on the terminal.
• The Shell wraps around the delicate interior of an Operating system protecting it
from accidental damage. Hence the name Shell.
TYPES OF SHELL
• There are two main shells in Linux:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed
below:
• POSIX shell also is known as sh
• Korn Shell also knew as sh
• Bourne Again SHell also knew as bash (most popular)

2. The C shell: The prompt for this shell is %, and its subcategories are:
• C shell also is known as csh
• Tops C shell also is known as tcsh
HOW TO WRITE SHELL SCRIPT IN LINUX/UNIX

• Shell Scripts are written using text editors.

• On your Linux system, open a text editor program, open a new file to
begin typing a shell script or shell programming, then give the shell
permission to execute your shell script and put your script at the
location from where the shell can find it.
• File Execution permission

chmod +x filename.sh

Another method to run the script

./filename.sh
WHAT ARE SHELL VARIABLES?
• Variables store data in the form of characters and numbers.

• Similarly, Shell variables are used to store information and they can
use by the shell only.
SAMPLE SCRIPT
SHELL SCRIPT FILE PROGRAM
comm COMMAND
comm [options] file1 file2

The comm command compares 2 sorted files and produces 3 columns of output,
separated by tabs:

1. All lines that appear in file1 but not in file2


2. All lines that appear in file2 but not in file1
3. All lines that appear in both files
tr COMMAND
The tr command in UNIX is a command line utility for translating or deleting characters. It
supports a range of transformations including uppercase to lowercase, squeezing repeating
characters, deleting specific characters and basic find and replace. 

-s : replaces repeated characters listed in the set1 with single occurrence

How to use squeeze repetition of characters using -s


To squeeze repeat occurrences of characters specified in a set use the -s option. This removes
repeated instances of a character.
cut COMMAND
• The cut command in UNIX is a command for cutting out the sections from each line
of files and writing the result to standard output. It can be used to cut parts of a line
by byte position, character and field. Basically the cut command slices a line and
extracts the text. It is necessary to specify option with command otherwise it gives
error. If more than one file name is provided then data from each file is not
precedes by its file name.

• cut uses tab as a default field delimiter but can also work with other delimiter by
using -d option.

• To extract the useful information you need to cut by fields rather than columns.
• With the -l option, ls will list out files and directories in long list format.

• In command -t option is used to sort directories and files by time and date of
creation or modification.
head COMMAND

• The head command displays the starting content of a line.


• By default, it displays starting 10 lines of any line.
• Head command for multiple files is also possible.
• The head -n option displays specified number of lines.
SUMMARY:
• Kernel is the nucleus of the operating systems, and it communicates between
hardware and software
• Shell is a program which interprets user commands through CLI like Terminal
• The Bourne shell and the C shell are the most used shells in Linux
• Linux Shell scripting is writing a series of command for the shell to execute
• Shell variables store the value of a string or a number for the shell to read
• Shell scripting in Linux can help you create complex programs containing
conditional statements, loops, and functions
• Basic Shell Scripting Commands in Linux: cat, more, less, head, tail, mkdir, cp, mv,
rm, touch, grep, sort, wc, cut and, more.

You might also like