0% found this document useful (0 votes)
59 views

UNIX SHELL PROGRAMMING Micro Complete

UNIX is a powerful multitasking operating system initially developed in 1970. It acts as a link between the computer and user and allows more than one task to run simultaneously. Some key advantages of UNIX include its efficient use of memory, ability to perform many tasks using fewer system resources through command line interfaces, treating all data as files, and its security which makes anti-virus software unnecessary on many UNIX-based systems.

Uploaded by

Satyajit Roul
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)
59 views

UNIX SHELL PROGRAMMING Micro Complete

UNIX is a powerful multitasking operating system initially developed in 1970. It acts as a link between the computer and user and allows more than one task to run simultaneously. Some key advantages of UNIX include its efficient use of memory, ability to perform many tasks using fewer system resources through command line interfaces, treating all data as files, and its security which makes anti-virus software unnecessary on many UNIX-based systems.

Uploaded by

Satyajit Roul
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
You are on page 1/ 21

UNIX OPERATING SYSTEM

 UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis Ritchie at AT&T
Bell laboratories in 1970.
 The Unix operating system is a set of programs that act as a link between the computer and the user.
 A UNIX operating system is a multitasking operating system that allows you to initiate more than
one task from the same terminal so that one task is performed as a foreground and the other task as
a background process.
Advantages of UNIX operating system
Ecommerce: - Many big online stores use UNIX or Linux servers to host their websites. UNIX has also
used to manage mobiles and electronic machines.
Memory usage: - UNIX use less memory while running sophisticated programs. UNIX OS can handle
virtual memory nicely. The virtual memory expands as more programs come into the main memory.
Most of the tasks in UNIX is done by using fewer resources.
Less code to execute: - In GUI (graphical user interface), we sometimes need many mouse clicks to
perform some specific task but in the case of UNIX we can simply write one command in CLI
(command line interface) and that task is done.
Files used everywhere: - All type of data is stored in files i.e. all devices and terminal are stored in
files. Working with files in OS becomes fast and can be easily managed by UNIX.
No anti-virus needed: - As you know that Chrome OS, macOS, Linux, Ubuntu and android are
developed by using UNIX OS. These OS are considered safe from any virus. You don’t need to install
any anti-virus in newly build Chrome OS.
User interactions: - There are many online servers, electric machines where user interaction is not
involved. UNIX is an ideal choice for machines and devices where no or fewer user interactions are
involved. UNIX can manage the automatic working of systems very well.
Safe and secure: - UNIX provides a safe and secure platform in which multiple users can interact with
the servers online without any security issues. The interaction with the UNIX servers is fast and
without any bugs.
Linux OS Unix OS

Linux is an open-source operating system. This OS is Unix is a powerful and multitasking


supported on several computer platforms and includes operating system that behaves like a
multiple software features that handle computer bridge between the user and the
resources, and allow you to do tasks. computer.

It has different distros like Ubuntu, Redhat, Fedora, etc It has also different distros like IBM AIX,
HP-UX and Sun Solaris.

Nowadays, Linux is in great demand. Anyone can use It was developed mainly for servers,
Linux whether a home user, developer or a student. workstations and mainframes.

Linux is used everywhere from servers, PC, smartphones, It is used in servers, workstations and PCs.
tablets to mainframes and supercomputers.

It is available in multiple languages. It is available in English.

Linux supports more file system than Unix. It also supports file system but lesser than
Linux.

Linux is a Unix clone,behaves like Unix but doesn't Unix contain a completely different coding
contain its code. developed by AT&T Labs.
UNIX OS Windows OS

It is a command-based operating system. It is a menu based operating system.

It is an open-source system which can be used to under It is a proprietary software owned by


General Public License. Microsoft.

It has a text base interface, making it harder to grasp for It has a Graphical User Interface,
newcomers. making it simpler to use.

It supports Multiprocessing. It supports Multithreading.

It uses File Allocation System (FAT32)


It uses Unix File System(UFS) that comprises STD.ERR
and New technology file
and STD.IO file systems.
system(NTFS).

It is more secure as all changes to the system require


It is less secure compared to UNIX.
explicit user permission.

It is tedious to create a backup and recovery system in It has an integrated backup and
UNIX, but it is improving with the introduction of new recovery system that make it simpler
distributions of Unix. to use.

Hardware support is limited in UNIX system. Some Drivers are available for almost all the
hardware might not have drivers built for them. hardware.

It is fully case-sensitive, and files can be considered


It has case sensitivity as an option.
separate files.

EXTERNAL & INTERNAL COMMAND


UNIX commands are classified into two types -
 Internal Commands - Ex: cd, source, fg
 External Commands - Ex: ls, cat
Internal Command:
Internal commands are something which is built into the shell. For the shell built in commands,
the execution speed is really high. It is because no process needs to be spawned for executing it. For
example, when using the "cd" command, no process is created. The current directory simply gets
changed on executing it.
External Command:
External commands are not built into the shell. These are executables present in a separate
file. When an external command has to be executed, a new process has to be spawned and the
command gets executed. For example, when you execute the "cat" command, which usually is at
/usr/bin, the executable /usr/bin/cat gets executed.
Internal vs External
The big difference in internal vs external command is performance. Internal command are
much much faster compared to external for the simple reason that no process needs to be spawned
for an internal command since it is all built-into the shell. So, as the size of a script gets bigger, using
external commands a lot does adds to its performance.
Features of UNIX Operating System:
Multitasking: A UNIX operating system is a multitasking operating system that allows you to initiate
more than one task from the same terminal so that one task is performed as a foreground and the
other task as a background process.
Multi-user: UNIX operating system supports more than one user to access computer resources like
main memory, hard disk, tape drives, etc. Multiple users can log on to the system from different
terminals and run different jobs that share the resources of a command terminal. It deals with the
principle of time-sharing. Time-sharing is done by a scheduler that divides the CPU time into several
segments also called a time slice, and each segment is assigned to each user on a scheduled basis.
Portability: This feature makes the UNIX work on different machines and platforms with the easy
transfer of code to any computer system. Since a significant portion of UNIX is written in C language,
and only a tiny portion is coded in assembly language for specific hardware.
File Security and Protection: Being a multi-user system, UNIX makes special consideration for file
and system security. UNIX has different levels of security using assigning username and password to
individual users ensuring the authentication, at the level providing file access permission viz. read,
write and execute and lastly file encryption to change the file into an unreadable format.
Command Structure: UNIX commands are easy to understand and simple to use. Example: "cp", mv
etc. While working in the UNIX environment, the UNIX commands are case-sensitive and are entered
in lower case.
Communication: In UNIX, communication is an excellent feature that enables the user to
communicate worldwide. It supports various communication facilities provided using the write
command, mail command, talk command, etc.
Open Source: UNIX operating system is open source it means it is freely available to all and is a
community-based development project.
Accounting: UNIX keeps an account of jobs created by the user. This feature enhances the system
performance in terms of CPU monitoring and disk space checking. It allows you to keep an account of
disk space used by each user, and the disk space can be limited by each other. You can assign every
user a different disk quota. The root user can perform these accounting tasks using various commands
such as quota, df, du, etc.

CREATION OF PARTITION IN OS
Partitioning a disk in Unix is a process of dividing a disk into one or more logical areas, known as
partitions, on which the user can work separately . The partition table stores the information about
the location and size of partitions. With the partition table, each partition can appear to the operating
system as a logical disk, and users can read and write data on those disks.
Here are the steps to create partitions in Unix:
1. Attach the disk to the proper port.
2. Create partitions in the disk.
3. Create a file system on the partition.
4. Mount the file systems.
To create a new partition, you can use the `fdisk` command with the name of the hard drive
you want to create partitions on . Once you enter the command mode, you can create a new partition
using the `n` command . After creating the partition, you can set the partition type using the `t`
command . Finally, you can save the changes using the `w` command .
Once you have created the partition, you can create a file system on the partition using the
`mkfs` command . For example, to create an ext4 file system on a specific partition, you can use the
following command: `sudo mkfs.ext4 /dev/sda1` .
After creating the file system, you can mount the file system using the `mount` command . For example,
to mount the file system on `/mnt/data`, you can use the following command: `sudo mount /dev/sda1
/mnt/data` .
PROCESSES & ITS CREATION PHASES – FORK , EXEC, WAIT, EXIT
In Unix, a process is an instance of a program that is being executed. When a command is issued
in Unix/Linux, it creates/starts a new process 1. The newly created process is called the child process,
and the process that initiated it (or the process when execution is started) is called the parent process .
 Fork : The creation of a process in Unix is achieved through the fork() system call 2. The fork()
system call creates a child process that is a copy of the current task 3. After the fork() system
call, we have two processes: the parent and child processes.
 Exec : The exec() system call is used to replace the current process image with a new process
image . The exec() system call is used after the fork() system call to execute a new program in
the child process . The exec() system call replaces the current process image with a new
process image, which is loaded from the executable file.
 wait : The wait() system call is used to wait for the child process to terminate 5. The wait()
system call suspends the execution of the parent process until the child process terminates 5.
 exit : The exit() system call is used to terminate the current process . The exit() system call is
used to terminate the current process and return the exit status to the parent process .

GRANTING RIGHTS
In Unix, file permissions are used to control access to files and directories. There are three types
of permissions: read, write, and execute. These permissions can be granted to three types of users:
owner, group, and others . To grant permissions to a file or directory, we can use the chmod
command followed by the permission code and the file or directory name.
The permission code is a three-digit number, where each digit represents the permission for each user
type. The first digit represents the permission for the owner, the second digit represents the
permission for the group, and the third digit represents the permission for others.
The permission codes are as follows:
4 for read permission
2 for write permission
1 for execute permission
For example, to grant read, write, and execute permissions to the owner, read and execute
permissions to the group, and read permission to others, you can use the following command:
chmod 751 filename
Here, the first digit (7) represents the permission for the owner, which is read, write, and execute.
The second digit (5) represents the permission for the group, which is read and execute. The third
digit (1) represents the permission for others, which is read only .
User management commands :
UNIX COMMAND - Unix commands are a set of commands that are used to interact with the Unix
operating system. Unix commands are entered at the command prompt in a terminal window, and
they allow users to perform a wide variety of tasks, such as managing files and directories, running
processes, managing user accounts, and configuring network settings.
TYPE OF USER MANAGEMENT COMMANDS -
useradd: This command is used to create a new user account. For example, to create a new user
named “johndoe”, you can use the following command: sudo useradd johndoe.
userdel: This command is used to delete an existing user account. For example, to delete the user
account “johndoe”, you can use the following command: sudo userdel johndoe.
passwd: This command is used to change the password of an existing user account. For example, to
change the password for the user account “johndoe”, you can use the following command: sudo
passwd johndoe.
usermod: This command is used to modify an existing user account. For example, to change the home
directory of the user account “johndoe”, you can use the following command: sudo usermod -d
/new/home/directory johndoe.
su: This command is used to switch to another user account. For example, to switch to the user
account “johndoe”, you can use the following command: su johndoe.
Unix Architecture

The Unix architecture has 4 layers.


Hardware: Hardware is the most simple and least powerful layer in the Unix Architecture. Hardware
is the components that are humanly visible. Whatever hardware is connected to a Unix operating
system-based machine, comes in the hardware layer.
Kernel: This is the most powerful layer of the Unix architecture. The kernel is responsible for acting
as an interface between the user and the hardware for the effective utilization of the hardware. The
kernel handles the hardware effectively by using the device drivers. The kernel is also responsible for
process management. So, the main 2 features of the kernel are process management and file
management.
 Process Management: The processes that execute within the operating system require a lot of
management in terms of memory being allocated to them, the resource allocation to the
process, process synchronization, etc. All this is done by the Kernel in Unix OS. This is done
using various Operating System Techniques like paging, framing, virtual memory, swapping,
context-switching, etc.
 File Management: File management involves managing the data stored in the files. This also
includes the transmission of data stored in these files to the processes as and when they
request it.
Shell: We understood the importance of the kernel and that it handles most of the important and
complex tasks of Unix OS. Since the kernel is such an important program of the Unix Operating System,
its direct access to the users can be dangerous. Hence, the Shell comes into the picture. Shell is an
interpreter program that interprets the commands entered by the user and then sends the requests to
the kernel to execute those commands. When the execution of the process is completed, the shell
again sends a request to the kernel to display the program/information on the screen to the user. So,
Kernel is an interface between the user and the hardware and the Shell is an interface between the
user and the Kernel. The shell can be used for opening a file, writing into the files, executing programs,
etc.
There are 3 types of shells in the Unix Operating system.
 Bourne Shell (sh): It is the most widely available shell on Unix OS devices across the world.
This was the first shell available in the Unix OS. It is simply called a shell.
 C Shell (csh): The University of California (Berkeley) developed C Shell is another Unix shell
that removes some of the obsolete features or problems from the Bourne Shell. So, it enhances
the performance of the Bourne Shell.
 Korn Shell (ksh): The name of the Korn Shell is based on its creator, David Korn. This shell
enhances the C shell further by removing the shortcomings of the C shell and also enhancing
the user interaction of the Bourne Shell.
Applications/Application Programs: The last layer of the Unix architecture is the Application
Program layer. As the name suggests, this outermost layer of Unix Architecture is responsible for
executing the application programs.
USER & TYPE OF USER/ACCOUNT IN UNIX or LINUX
USER - Users are accounts that can be used to login into a system. Each user is identified by
a unique identification number or UID by the system. All the information of users in a system are
stored in /etc/passwd file. The hashed passwords for users are stored in /etc/shadow file.
Users can be divided into two categories on the basis of the level of access:
 Superuser/root/administrator : Access to all the files on the system.
 Normal users : Limited access.
There are three types of accounts on a Unix system −
1. Root account
This is also called superuser and would have complete and unfettered control of the system. A
superuser can run any commands without any restriction. This user should be assumed as a system
administrator.
2. System accounts
System accounts are those needed for the operation of system-specific components for example mail
accounts and the sshd accounts. These accounts are usually needed for some specific function on your
system, and any modifications to them could adversely affect the system.
3. User accounts
User accounts provide interactive access to the system for users and groups of users. General users
are typically assigned to these accounts and usually have limited access to critical system files and
directories.
Unix supports a concept of Group Account which logically groups a number of accounts. Every
account would be a part of another group account. A Unix group plays important role in handling file
permissions and process management.
Managing Users and Groups
There are four main user administration files −
 /etc/passwd − Keeps the user account and password information. This file holds the majority
of information about accounts on the Unix system.
 /etc/shadow − Holds the encrypted password of the corresponding account. Not all the
systems support this file.
 /etc/group − This file contains the group information for each account.
 /etc/gshadow − This file contains secure group account information.

The following table lists out commands that are available on majority of Unix systems to create
and manage accounts and groups −
Sr.No. Command & Description

1 useradd - Adds accounts to the system

2 usermod - Modifies account attributes

3 userdel - Deletes accounts from the system

4 groupadd - Adds groups to the system

5 groupmod - Modifies group attributes

6 groupdel - Removes groups from the system


Discuss the login process in UNIX?
When we log into a Unix system, the login process begins. The first process that is executed with
your user ID when you log into an interactive session is called the login shell. The login shell is
responsible for creating a shell environment for you to work in. It reads environment variables and
other configurations from /etc/profile and a profile file in the home directory .
On switching in a UNIX environment, the Kernel program is first program that is loaded in the
computer’s memory. From the Hard-disk , this program triggers a chain of initialization processes at
the end of which the login prompt appears on the screen.
On entering a login name, the login program is activated, login points the password prompt on
he screen and turn off character echoing , so that the password entered is not displayed on the
terminal. Login compares the password entered with the /etc/passwd file. If they match, a program
named in the same file replaces the login program. The program named is usually BOURNE SHELL or
the C SHELL and is called login shell.

The login process in Unix systems involves the following steps :


 The first process in a Unix system starts a getty program. This program is responsible for
protecting the system from unauthorized access.
 When you try to connect to a Unix system either directly or via SSH, getty prompts you to enter
your username and password.
 Your entries are verified against the information stored in /etc/passwd file. The entries in the
/etc/passwd file have some information like username, userid, home directory, and the first
program that should be started after login succeeds.
 The first program to start when you log in is the login shell. For standard users in Unix, it is the
login shell. There could be other values in the field as well.

FILE SYSTEM MANAGEMENT


All data in Unix is organized into files. All files are organized into directories. These directories are
organized into a tree-like structure called the filesystem.
In Unix, there are three basic types of files −
 Ordinary Files − An ordinary file is a file on the system that contains data, text, or program
instructions. In this tutorial, you look at working with ordinary files.
 Directories − Directories store both special and ordinary files. For users familiar with
Windows or Mac OS, Unix directories are equivalent to folders.
 Special Files − Some special files provide access to hardware such as hard drives, CD-ROM
drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts
and enable you to access a single file using different names.

DISK QUOTA
File quota is a built-in feature found in the Linux kernel that determines the amount of space a file
should have to support user activities. The disk quotas also limit the number of files a user can create
on the system. File systems that support the quota system include ext2, ext3, ext4, and XFS .
SHELL
SHELL is a program which provides the interface between the user and an operating system. When
the user logs in OS starts a shell for user. Kernel controls all essential computer operations, and
provides the restriction to hardware access, coordinates all executing utilities, and manages
Resources between process. Using kernel only user can access utilities provided by operating
system.
Types of Shell in UNIX
 The Bourne Shell (sh)
Introduced with Version 7 UNIX in 1979, the Bourne Shell (often denoted as sh) has become
synonymous with UNIX Shell scripting. This pioneering Shell offered users a programming
environment, complete with variables, control structures, and built-in operators. Its simplicity was its
strength, ensuring widespread adoption in UNIX environments.
As the foundational Shell, sh paved the way for the development of subsequent Shells. Its scripts are
characterised by portability across various UNIX-based systems, making it the go-to choice for many
legacy applications even today.
 The C Shell (csh)
Emerging from the University of California, Berkeley in the late 1970s, the C Shell (csh) aimed to offer
an improved User Experience. Notably, its syntax mirrors that of the C programming language,
providing an intuitive environment for those familiar with C. Enhanced scripting features, including
job control and command history, make it quite interactive.
However, csh has faced criticism for certain scripting quirks and behaviours, making it less favoured
for scripting tasks compared to other Shells. Despite this, for interactive use, many found its features
compelling.
 The Korn Shell (ksh)
David Korn's brainchild in the early 1980s, the Korn Shell (ksh), brought the best of both
the Bourne and C Shells. Infusing the simplicity of sh with advanced features of csh, ksh introduced
associative arrays and advanced string manipulation capabilities.
The Korn Shell’s adaptability has seen it become the default Shell for many commercial UNIX systems.
Over the years, ksh has enjoyed several upgrades, each refining its capabilities and improving User
Experience. For scriptwriters seeking a balanced Shell, ksh often stands out as an optimal choice.
Unlock the power of UNIX with our UNIX Fundamentals Course – join today!
 The Bourne Again Shell (bash)
A product of the GNU project in 1989, the Bourne Again Shell (bash) sought to enhance
the Bourne Shell experience. Incorporating features from both ksh and csh, bash presents one of the
most comprehensive and user-friendly Shell environments.
The Bourne Again Shell’s ubiquity in the Linux realm is testament to its versatility and power. Being
the default Shell for most Linux distributions and even macOS, it offers users a consistent interface
packed with advanced features. From scripting to interactive usage, bash caters to both novices and
experts alike.
 The Tcsh Shell
An evolution of the C Shell, tcsh surfaced in the late 1980s, addressing many of csh's limitations.
Introducing command-line editing, autocompletion, and improved scripting capabilities, tcsh aimed to
give users a more rounded experience.
The enhancements made the Tcsh Shell a preferred choice for many who appreciated the csh lineage
but sought a richer feature set. While not as prominent as some of the other Shells, tcsh has its loyal
user base, especially among those who began their UNIX journey during its heyday.
 The Z Shell (zsh)
A relative newcomer, zsh, was released in 1990, it combined the best of bash, ksh, and tcsh. What
sets zsh apart is its customisability, advanced features such as shared command history, spelling
correction, and themeable prompts. It offers users a tailored Shell experience.
Over the years, zsh has grown in popularity, particularly among developers and power users. This
ascent was further cemented when macOS Catalina chose zsh as its default Shell. Its dynamic
community ensures regular updates, making zsh a compelling choice for modern users.
LINK
A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX
are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file.
Links allow more than one file name to refer to the same file, elsewhere.
There are two types of links :
1. Soft Link or Symbolic links
2. Hard Links
1. Soft Links / Symbolic Links
 A soft link is similar to the file shortcut feature which is used in Windows Operating systems.
Each soft linked file contains a separate Inode value that points to the original file. As similar to
hard links, any changes to the data in either file is reflected in the other. Soft links can be linked
across different file systems, although if the original file is deleted or moved, the soft linked file
will not work correctly (called hanging link).
 ls -l command shows all links with first column value l? and the link points to original file.
 Soft Link contains the path for original file and not the contents.
 Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling”
link which points to nonexistent file.
 A soft link can link to a directory.
 The size of the soft link is equal to the length of the path of the original file we gave. E.g if we link
a file like ln -s /tmp/hello.txt /tmp/link.txt then the size of the file will be 14bytes which is
equal to the length of the “/tmp/hello.txt”.
 If we change the name of the original file then all the soft links for that file become dangling i.e.
they are worthless now.
 Command to create a Soft link is:
$ ln -s [original filename] [link name]

2.Hard Links
 Each hard linked file is assigned the same Inode value as the original, therefore they reference
the same physical file location. Hard links more flexible and remain linked even if the original or
linked files are moved throughout the file system, although hard links are unable to cross
different file systems.
 ls -l command shows all the links with the link column shows number of links.
 Links have actual file contents
 Removing any link, just reduces the link count, but doesn’t affect other links.
 Even if we change the filename of the original file then also the hard links properly work.
 We cannot create a hard link for a directory to avoid recursive loops.
 If original file is removed then the link will still show the content of the file.
 The size of any of the hard link file is same as the original file and if we change the content in any
of the hard links then size of all hard link files are updated.
 The disadvantage of hard links is that it cannot be created for files on different file systems and it
cannot be created for special files or directories.
 Command to create a hard link is:
$ ln [original filename] [link name]
File permissions
File ownership is an important component of Unix that provides a secure method for storing files.
Every file in Unix has the following attributes −
 Owner permissions − The owner's permissions determine what actions the owner of the file
can perform on the file.
 Group permissions − The group's permissions determine what actions a user, who is a
member of the group that a file belongs to, can perform on the file.
 Other (world) permissions − The permissions for others indicate what action all other users
can perform on the file.
SYSTEM CALL
A system call is a procedure that provides the interface between a process and the operating
system. It is the way by which a computer program requests a service from the kernel of the
operating system.
System calls are divided into 5 categories mainly :
Process management system calls: These system calls provide a way to create, manage, and control
processes. Examples of process management system calls include fork(), exec(), wait(), and exit() .
File management system calls: These system calls provide a way for programs to interact with the
file system. Examples of file management system calls include open(), read(), write(), and close() .
Device management system calls: These system calls provide a way for programs to interact with
devices such as printers, disks, and network interfaces. Examples of device management system calls
include ioctl(), read(), and write() .
Network management system calls: These system calls provide a way for programs to interact with
the network stack. Examples of network management system calls include socket(), bind(), listen(),
and accept() .
System information system calls: These system calls provide a way for programs to obtain
information about the system. Examples of system information system calls include getpid(), getuid(),
and gettimeofday() .

PIPE & FILTER


Pipes are defined as the two commands that can be linked together to redirect input/output to a
named file.
Filters in UNIX
In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin,
perform some operations and write output to standard output stream i.e. stdout. The stdin and
stdout can be managed as per preferences using redirection and pipes.
Common filter commands are: grep, more, sort.
1. grep Command:
It is a pattern or expression matching command. It searches for a pattern or regular expression
that matches in files or directories and then prints found matches.
Syntax:
$grep[options] "pattern to be matched" filename
2. sort Command:
It is a data manipulation command that sorts or merges lines in a file by specified fields. In other
words it sorts lines of text alphabetically or numerically, default sorting is alphabetical.
Syntax:
$sort[options] filename
3. more Command:
It is used to customize the displaying contents of file. It displays the text file contents on the
terminal with paging controls. Following key controls are used:
 To display next line, press the enter key
 To bring up next screen, press spacebar
 To move to the next file, press n
 To quit, press q.
Syntax:
$more[options] filename
SHELL SCRIPT
A shell script is a type of computer program developed to be executed by a Unix shell, which is
also known as a command-line interpreter. Several shell script dialects are treated as scripting
languages. Classic operations implemented by shell scripts contain printing text, program execution,
and file manipulation. A script configures the environment, executes the program, and does necessary
logging or clean-up is known as a wrapper.
Shell scripting is a way to automate tasks and processes in Unix/Linux systems. It involves writing
a sequence of commands in a file and then executing them. This saves time and effort as you don’t
have to write the same commands again and again. You can perform daily tasks efficiently and even
schedule them for automatic execution. You can also set certain scripts to execute on startup such as
showing a particular message on launching a new session or setting certain environment variables .
Bash is the most commonly used shell scripting language in Unix/Linux systems. It is a powerful and
flexible language that can be used to write complex scripts .
Steps to write and execute a Shell script
o Open the terminal.
o Go to the directory where you want to create your script.
o Create a file with .sh extension.
o Write the script in the file using an editor.
o Make the script executable with command chmod +x <fileName>.
o Run the script using ./<fileName>.

Hello World script


Here we'll write a simple programme for Hello World.
First of all, create a simple script in any editor or with echo. Then we'll make it executable
with chmod +x command. To find the script you have to type the script path for the shell.

Look at the above snapshot, script echo Hello World is created with echo command
as hello_world. Now command chmod +x hello_world is passed to make it executable. We have
given the command ./hello_world to mention the hello_world path. And output is displayed.

SHELL VARIABLES
When a shell is running, three main types of variables are present −
 Local Variables − A local variable is a variable that is present within the current instance of the shell.
It is not available to programs that are started by the shell. They are set at the command prompt.
 Environment Variables − An environment variable is available to any child process of the shell.
Some programs need environment variables in order to function correctly. Usually, a shell script
defines only those environment variables that are needed by the programs that it runs.
 Shell Variables − A shell variable is a special variable that is set by the shell and is required by the
shell in order to function correctly. Some of these variables are environment variables whereas
others are local variables.

In shell scripting, you can define your own variables using the = operator. These variables are
called user-defined variables. They are deleted after the shell script executes .
Shell Variable is used in shell scripts for many functionalities like storing data and
information, taking input from users, printing values that are stored. They are also used for storing
data temporarily and storing output of commands.
Hard Links Soft Links / Symbolic Links

It is a copy of the original file that serves as a It is a short pointer file that links a filename
pointer to the same file, allowing it to be to a pathname. It's nothing more than a shortcut
accessed even if the original file is deleted or to the original file, much like the Windows OS's shortcut
relocated. option.

It has a similar inode number to the target file. It has a different inode number.

It is not allowed the relative path. It allows both relative and absolute paths.

It cannot be established outside the file system. It may be established in the file system.

It is faster. It is slower.

The "ln" command is used to make a hard link The "ln -s" command is used to make a soft link in Linux.
in Linux.

It may only link to a file. It may link both to a directory or a file.

It remains valid even if the target file is deleted. It becomes invalid when the originating file is deleted.

EDITOR
Editors or text editors are software programs that enable the user to create and edit text files. In the field
of programming, the term editor usually refers to source code editors that include many special features for
writing and editing code. Notepad, Wordpad are some of the common editors used on Windows OS and vi,
emacs, Jed, pico are the editors on UNIX OS.
There are several text editors available on UNIX systems -
1. vi: A visual editor that uses ed/ex line-mode commands for global file editing.
2. emacs: A powerful and extensible text editor that is highly customizable .
3. nano: A simple and user-friendly text editor that is easy to use.
4. Sublime Text: A cross-platform text editor that supports multiple programming languages.
5. Atom: A free and open-source text editor that is highly customizable .
VI EDITOR
The default editor that comes with the UNIX operating system is called vi (visual editor). Using vi editor,
we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file.
The advanced version of the vi editor is the vim editor.
How to open VI editor?
To open vi editors, we just need to type the command mentioned below.
vi [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-existing file.
Modes of Operation in the vi editor
There are three modes of operation in vi:
1. Command Mode: - When vi starts up, it is in Command Mode. This mode is where vi interprets any
characters we type as commands and thus does not display them in the window. This mode allows us to
move through a file, and delete, copy, or paste a piece of text. Enter into Command Mode from any other
mode, requires pressing the [Esc] key. If we press [Esc] when we are already in Command Mode, then vi
will beep or flash the screen.
2. Insert mode: - This mode enables you to insert text into the file. Everything that’s typed in this mode is
interpreted as input and finally, it is put in the file. The vi always starts in command mode. To enter text,
you must be in insert mode. To come in insert mode, you simply type i. To get out of insert mode, press
the Esc key, which will put you back into command mode.
3. Last Line Mode (Escape Mode): - Line Mode is invoked by typing a colon [:], while vi is in Command
Mode. The cursor will jump to the last line of the screen and vi will wait for a command. This mode
enables you to perform tasks such as saving files and executing commands.
CREATING A USER/ACCOUNT
syntax to create a user's account −
useradd -d homedir -g groupname -m -s shell -u userid accountname
The following table lists out the parameters −
Sr.No. Option & Description

1 -d homedir
Specifies home directory for the account

2 -g groupname
Specifies a group account for this account

3 -m
Creates the home directory if it doesn't exist

4 -s shell
Specifies the default shell for this account

5 -u userid
You can specify a user id for this account

6 accountname
Actual account name to be created

If you do not specify any parameter, then the system makes use of the default values.
The useradd command modifies the /etc/passwd, /etc/shadow, and /etc/group files and creates a
home directory.
Following is the example that creates an account mcmohd, setting its home directory
to /home/mcmohd and the group as developers. This user would have Korn Shell assigned to it.
$ useradd -d /home/mcmohd -g developers -s /bin/ksh mcmohd

Before issuing the above command, make sure you already have the developers group created
using the groupadd command.
Once an account is created you can set its password using the passwd command as follows −
$ passwd mcmohd20
Changing password for user mcmohd20.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

When you type passwd accountname, it gives you an option to change the password, provided
you are a superuser. Otherwise, you can change just your password using the same command but
without specifying your account name.
UNIX FILE SYSTEM
Unix File System is defined as the framework that organizes and stores a large amount of data that can
be handled easily. It consists of elements like a file, a collection of related data that can be viewed
logically as a stream of bytes (or characters). A file is also the smallest storage unit in the Unix file
system.
The file system consists of two main components which are files and directories. The entire file
system follows a hierarchy in which directories act as a special file containing multiple files. The
highest level directory in the entire hierarchical structure is known as the root. The root is denoted as
' / '. There can be many sub-directories under this directory.

Some directories and their descriptions are as follows:


 /bin: short for binaries, this is the directory where many commonly used executable commands
reside
 /dev: contains device-specific files
 /etc: contains system configuration files
 /home: contains user directories and files PAGE 5
 /lib: contains all library files
 /mnt: contains device files related to mounted devices
 /proc: contains files related to system processes
 /root: the root users' home directory ( note this is different than ‘ / ‘ )
 /sbin: binary files of the system reside here.
 /tmp: storage for temporary files that are periodically removed from the file system
 /usr: It is the directory holding user home directories, its use has changed, and it also contains
executable commands
 /var: It is a short form for ‘variable’, a place for files that may often change

FUNCTION
A function is a block of code that performs a specific task and can be called multiple times from
within a script. Functions provide modularity in the program and reduce the code length. They are
useful for performing repetitive tasks and for breaking down the overall functionality of a script into
smaller, logical subsections 12.
To declare a function, simply use the following syntax −
function_name () {
commands
}
Here, function_name is the name of the function, and commands are the commands that the
function will execute.
RECURSIVE FUNCTION - One of the more interesting features of functions is that they can call
themselves and also other functions. A function that calls itself is known as a recursive function.

EXAMPLE
#!/bin/sh
# Define your function here
Hello () {
echo "Hello World"
}
# Invoke your function
Hello

output −
$./test.sh
Hello World
Types of Unix File System
The Unix File Systems are classified into six types, that are:
1. Ordinary Files
The files in Unix, which include program instructions, texts, and data, these files are known as ordinary files.
Some of the characteristics of ordinary files are:
 There are no other files in ordinary files.
 They are always placed under the directory file.
 These files store the information of users. It may be some text or any image which we have to draw.
 We mainly work with ordinary files.
2. Directories
Directories store both kinds of files: special as well as ordinary files. Some of the characteristics of
directories are:
 Directories may contain other directories also.
 All these directories are the descendants of the root directory.
 We use directories to organize the collection of files.
 It doesn’t contain real information like text or images. PAGE 6
3. Special Files
These files represent physical devices like printers, tape drivers, or a terminal. Some of its
characteristics are:
 Special files represent input/output operations on Linux and Unix systems.
 There are mainly two special files for every device in the Unix system: block special and character special
files.
 Block special: Using it for device input/output will move the data to the higher fixed-size blocks. This kind
of access is also known as block device access.
 Character Special: This is one character at a time for the terminal devices. However, in the case of disk
devices, raw access means reading or writing across an entire section of data blocks that are native to our
disk.
4. Pipes
Pipes are used to link commands together to store both kinds. The following are the characteristics of
pipes:
 It is a temporary file that only holds the data from one command until another reads it.
 Unix pipes provide the one-way flow of data.
 Any command's output is assigned to its next command as an input.
5. Sockets
The files that enable advanced inter-process communication are called Sockets. It is also known as an inter-
process communication socket. Some of the characteristics of sockets are:
 Unix sockets are used in the framework of client-server applications.
 It is essentially a data stream, exactly the same as the network stream and network socket, except where
each transaction is local to the file system.
6. Symbolic Links
It is used to reference another file in the system. Characteristics of symbolic links are:
 It involves a text representation of the path to the file it refers to.
 To an end-user, a symbolic link will seem to have its name, yet when we need to write or read
data to this file, it will reference these tasks to the file it focuses on.
 If the source file is deleted or moved to a different location, the symbolic file will not function.

Advantages of the Unix file System


 Hierarchical organization: The hierarchical structure of the Unix file system makes it easy to organize
and navigate files and directories.
 Robustness: The Unix file system is known for its stability and reliability. It can handle large amounts of
data without becoming unstable or crashing.
 Security: The Unix file system uses a set of permissions that allows administrators to control who has
access to files and directories.
 Compatibility: The Unix file system is widely used and supported, which means that files can be easily
transferred between different Unix-based systems.
Utility programs (cut , paste, join ,tr ,uniq utilities) :
CUT
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.
Syntax:
cut OPTION... [FILE]...

PASTE
Paste command is one of the useful commands in Unix or Linux operating system. It is used to join
files horizontally (parallel merging) by outputting lines consisting of lines from each file specified,
separated by tab as delimiter, to the standard output. When no file is specified, or put dash (“-“)
instead of file name, paste reads from standard input and gives output as it is until a interrupt
command [Ctrl-c] is given.
Syntax:
paste [OPTION]... [FILES]...

JOIN
The join command in UNIX is a command line utility for joining lines of two files on a common field.
Suppose you have two files and there is a need to combine these two files in a way that the output
makes even more sense.For example, there could be a file containing names and the other
containing ID’s and the requirement is to combine both files in such a way that the names and
corresponding ID’s appear in the same line. join command is the tool for it. join command is used to
join the two files based on a key field present in both the files. The input file can be separated by
white space or any delimiter.
Syntax:
$join [OPTION] FILE1 FILE2

TR
The tr command is a UNIX 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. It can be used with UNIX pipes to support more
complex translation. tr stands for translate.
Syntax :
$ tr [OPTION] SET1 [SET2]

UNIQ
The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in
a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also
deletes the duplicate lines. uniq filters out the adjacent matching lines from the input file(that is
required as an argument) and writes the filtered data to the output file.
Syntax :
//...syntax of uniq...//
$uniq [OPTION] [INPUT[OUTPUT]]
Shell Scripting – Decision Making
In programming, Decision making is one of the important concepts. The programmer provides
one or more conditions for the execution of a block of code. If the conditions are satisfied then those
block of codes only gets executed. Two types of decision-making statements are used within shell
scripting. They are –
 If-else statement
 case-sac statement / Switch statement
1. If-else statement
If else statement is a conditional statement. It can be used to execute two different codes based on
whether the given condition is satisfied or not. There are a couple of varieties present within the if-
else statement. They are –
 if-fi
 if-else-fi
 if-elif-else-fi
 nested if-else

2. The case-sac statement


case-sac is basically working the same as switch statement in programming. Sometimes if we
have to check multiple conditions, then it may get complicated using if statements. At those
moments we can use a case-sac statement.
The syntax will be –
case $var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac

Example of case-sac statement


Name="Satyajit"
case "$Name" in
#case 1
"Rajib") echo "Profession : Software Engineer" ;;
#case 2
"Vikas") echo "Profession : Web Developer" ;;
#case 3
"Satyajit") echo "Profession : Technical Content Writer" ;;
esac
Output
Profession : Technical Content Writer

PAGE 3
The syntax of if-else statement Example of if-fi
will be – Name="Satyajit"
if [ "$Name" = "Satyajit" ]; then
echo "His name is Satyajit. It is true."
if-fi
fi
if [ expression ]; then
Output
statements
His name is Satyajit. It is true.
fi

if-else-fi Example of if-else-fi


Age=17
if [ expression ]
if [ "$Age" -ge 18 ]; then
then
echo "You can vote"
statement1
else
else
echo "You cannot vote"
statement2
fi
fi
Output
You cannot vote
if-elif-else-fi
if [ expression1 ]
Example of if-elif-else-fi
then
Age=17
statement1
if [ "$Age" -ge 18 ]; then
statement2
echo "You can vote"
.
elif [ "$Age" -eq 17 ]; then
.
echo "You can vote after one year"
elif [ expression2 ]
else
then
echo "You cannot vote"
statement3
fi
statement4
Output
.
You can vote after one year
.
else
statement5 Example of Nested if-else
fi echo "Enter subject"
read subject
if [ $subject == 'Linux' ]
nested if-else
then
if [ expression ]
echo "Enter Marks"
then
read marks
statement1
if [ $marks -ge 30 ]
if [ expression ]
then
then
echo "You passed"
statement
else
else
echo "You failed"
statement
fi
fi
else
else
echo "Wrong Subject"
statement2
fi
fi
Output
Enter subject
Linux
Enter Marks
PAGE 4 97
You passed
Pattern matching utility ( grep ) :
GREP stands for “Global Regular Expression Print”. Grep is a powerful pattern matching utility in
Unix/Linux systems that searches for a specified pattern in a file or a stream of data and prints the
matching lines. The pattern that is searched in the file is referred to as the regular expression.
The syntax of the grep command is -
grep [options] pattern [files]
Here,
[options]: These are command-line flags that modify the behavior of grep.
[pattern]: This is the regular expression you want to search for.
[file]: This is the name of the file(s) you want to search within.

Some of the commonly used options in grep command are -


Options Description

-c This prints only a count of the lines that match a pattern

-h Display the matched lines, but do not display the filenames.

–i Ignores, case for matching

-l Displays list of a filenames only.

-n Display the matched lines and their line numbers.

-v This prints out all the lines that do not matches the pattern

-E Treats pattern as an extended regular expression (ERE)

-w Match whole word

Print only the matched parts of a matching line, with each such part on a
-o separate output line.

-A n Prints searched line and nlines after the result.

-B n Prints searched line and n line before the result.

-C n Prints searched line and n lines after before the result.


LOOPS IN SHELL
There are total 3 looping statements that can be used in bash programming.
1. while statement
2. for statement
3. until statement

1.while statement 2.for statement


Here the command is evaluated and based on the The for loop operates on lists of items. It repeats
resulting loop will execute, if the command is a set of commands for every item in a list.
raise to false then the loop will be Here var is the name of a variable and word1 to
terminated that . wordN are sequences of characters separated by
Syntax- spaces (words). Each time the for loop executes,
while <condition> the value of the variable var is set to the next
do word in the list of words, word1 to wordN.
<command 1>
<command 2> Syntax:
<etc> for <var> in <value1 value2 ... valuen>
done do
<command 1>
EXAMPLE - Implementing while loop with <command 2>
continue statement <etc>
a=0 done
# -lt is less than operator
#Iterate the loop until a less than 10 EXAMPLE - Implementing for loop with break
while [ $a -lt 10 ] statement
do #Start of for loop
# Print the values for a in 1 2 3 4 5 6 7 8 9 10
echo $a do
# increment the value # if a is equal to 5 break the loop
a=`expr $a + 1` if [ $a == 5 ]
done then
break
Output: fi
$bash -f main.sh # Print the value
0 echo "Iteration no $a"
1 done
2
3 Output:
4 $bash -f main.sh
5 Iteration no 1
6 Iteration no 2
7 Iteration no 3
8 Iteration no 4
9

PAGE 1
3.until statement
The until loop is executed as many times as the condition/command evaluates too false. The loop
terminates when the condition/command becomes true.
Syntax:
until <condition>
do
<command 1>
<command 2>
<etc>
done

EXAMPLE - Implementing until loop with continue statement


a=0
# -gt is greater than operator
#Iterate the loop until a is greater than 10
until [ $a -gt 10 ]
do
# Print the values
echo $a
# increment the value
a=`expr $a + 1`
done

Output:
$bash -f main.sh
0
1
2
3
4
5
6
7
8
9
10

PAGE 2

You might also like