You are on page 1of 51

3CS4-06: Linux and Shell Programming

Linux operating system

An operating system can be described as an interface among the computer hardware


and the user of any computer. It is a group of software that handles the resources of the
computer hardware and facilitates basic services for computer programs.

An operating system is an essential component of system software within a computer system.


The primary aim of an operating system is to provide a platform where a user can run any
program conveniently or efficiently.

On the other hand, Linux OS is one of the famous versions of the UNIX OS. It is developed
to provide a low-cost or free OS for several personal computer system users. Remarkably, it
is a complete OS Including an X Window System, Emacs editor, IP/TCP, GUI (graphical
user interface), etc.

Linux operating system history


In 1991, the Linux history started with the starting of a particular project by the
Finland student Linus Torvalds for creating a new free OS kernel. The final Linux
Kernel was remarked by continuous development throughout the history since then.

o Linux was proposed by the Finland student Linus Torvalds in 1991.


o HP-UX (Hewlett Packard) 8.0 version was published.
o Hewlett Packard 9.0 version was published in 1992.
o FreeBSD 1.0 version and NetBSD8 version was released in 1993.
o Red Hat Linux was proposed in 1994. Caldera was detected by Ransom love and
Bryan Sparks and NetBSD 1.0 version published.
o HP-UX 10.0 version and FreeBSD 2.0 version was released in 1995.
o K Desktop Environment was established by Matthias Ettrich in 1996.
o HP-UX 11.0 version was released in 1997.
o The IRIX 6.5 version, i.e., the fifth SGI UNIX generation, Free BSD 3.0 version, and Sun
Solaris 7 OS was released in 1998.
o The Caldera System agreement with professional services division and SCO server
software division was released in 2000.
o Linus Torvalds published the Linux version 2.4 source code in 2001.
o Microsoft filed the Trademark collection against Lindows.com in 2001.
o Lindows name was modified to Linspire in 2004.
o The first publication of Ubuntu was published in 2004.
o The openSUSE project started a free distribution from the community of Novell In
2005.
o Oracle published its Red Hat distribution in 2006.
o Dell begun laptop distribution with Ubuntu which was pre-installed on it in 2007.
o Linux kernel version 3.0 was released in 2011.
o Linux-based android of Google insisted 75% of the market share of the Smartphone,
based on the number of phones exported in 2013.
o Ubuntu insisted on 20000000+ users in 2014.

Architecture of Linux system

The Linux operating system's architecture mainly contains some of the


components: the Kernel, System Library, Hardware layer, System, and Shell
utility.

1. Kernel:- The kernel is one of the core section of an operating system. It is


responsible for each of the major actions of the Linux OS. This operating system
contains distinct types of modules and cooperates with underlying hardware directly.
The kernel facilitates required abstraction for hiding details of low-level hardware or
application programs to the system. There are some of the important kernel types
which are mentioned below:

o Monolithic Kernel
o Micro kernels
o Exo kernels
o Hybrid kernels

2. System Libraries:- These libraries can be specified as some special functions.


These are applied for implementing the operating system's functionality and don't
need code access rights of the modules of kernel.

3. System Utility Programs:- It is responsible for doing specialized level and


individual activities.

4. Hardware layer:- Linux operating system contains a hardware layer that consists
of several peripheral devices like CPU, HDD, and RAM.

5. Shell:- It is an interface among the kernel and user. It can afford the services of
kernel. It can take commands through the user and runs the functions of the kernel.
The shell is available in distinct types of OSes. These operating systems are
categorized into two different types, which are the graphical shells and command-
line shells.

The graphical line shells facilitate the graphical user interface, while the command
line shells facilitate the command line interface. Thus, both of these shells implement
operations. However, the graphical user interface shells work slower as compared to
the command-line interface shells.

There are a few types of these shells which are categorized as follows:

o Korn shell
o Bourne shell
o C shell
o POSIX shell

Linux Operating System Features


Some of the primary features of Linux OS are as follows:
o Portable: Linux OS can perform different types of hardware and the kernel of
Linux supports the installation of any type of hardware environment.
o Open source: Linux operating system source code is available freely and for
enhancing the capability of the Linux OS, several teams are performing in
collaboration.
o Multiprogramming: Linux OS can be defined as a multiprogramming system.
It means more than one application can be executed at the same time.
o Multi-user: Linux OS can also be defined as a multi-user system. It means
more than one user can use the resources of the system such as application
programs, memory, or RAM at the same time.
o Hierarchical file system: Linux OS affords a typical file structure where user
files or system files are arranged.
o Security: Linux OS facilitates user security systems with the help of various
features of authentication such as controlled access to specific files, password
protection, or data encryption.
o Shell: Linux operating system facilitates a unique interpreter program. This
type of program can be applied for executing commands of the operating
system. It can be applied to perform various types of tasks such as call
application programs and others.
Drawbacks of Linux

o Hardware drivers: Most of the users of Linux face an issue while using Linux.
Various companies of hardware prefer to build drivers for Mac or Windows
due to they contain several users than Linux. Linux has small drivers for
peripheral hardware than windows.
o Software alternative: Let's take the Photoshop example which is a famous
tool for graphic editing. Photoshop exists for Windows; however, it is not
available in Linux. Also, there are some other tools for photo editing but the
Photoshop tool is more powerful as compare to others. Another example is
MS office which is not present for Linux users.
o Learning curve: Linux isn't a very user-friendly operating system. Hence, it
might be confusing for many beginners. Getting begun with Windows is
efficient and easy for many beginners; however, understanding Linux working
is complex.
We have to understand the command line interface and finding for newer
software is a little bit complex as well. When we face any issue in the OS, the
searching solution is very problematic. Also, there are various experts for Mac
and Windows as compare to Linux.
o Games: Several games are developed for Windows but unfortunately not for
Linux. Because the platform of Windows is used widely. So, the developers of
the games are more interested in windows.
File system of Linux is as follows:

General files: General files or ordinary files are files that contain images, text, or a
program. These files are in ASCII text or Binary format. The General files are the most
common in any Linux system.

Directory files: Directory files are the depository for other files. A directory can have
a subdirectory file within it. For Windows, we can understand them as folders.

Device files: Windows represents the external devices (Pendrives, hard drives, and
CD-ROM) as letters such as E: F: But, Linux represents devices as files, such as the
hard drive's partitions are represented as dev/sda1, dev/sda2, and more (depends
upon the number of partitions). All the device files take place within the directory
/dev.

o Naming Conventions for file

Linux files are case sensitive; therefore, we can have two files having the same name;
one in upper case and other in lower case. Comparatively, the Windows files are not
case sensitive; we cannot have two files with the same name.

o Users

Comparatively, Linux supports three types of users:

o Regular
o Administrative(root)
o Service
Linux File Ownership
Every Linux system have three types of owner:

1. User: A user is the one who created the file. By default, whosoever, creates the file becomes
the owner of the file. A user can create, delete, or modify the file.
2. Group: A group can contain multiple users. All the users belonging to a group have same
access permission for a file.
3. Other: Any one who has access to the file other than user and group comes in the category
of other. Other has neither created the file nor is a group member.

Users and groups can be locally managed in /etc/psswd or /etc/group.

Syntax:

1. ls -lh

Look at the above snapshot, all the listed files and directories have the same user and group
that is sssit.First sssit column denotes the user and second column denotes the group.
Listing User Accounts
To know the local users account, following command can be used. It list out all the local
users from the system.

Syntax:

1. cut -d: -f1 /etc/passwd | column

Linux chgrp: change group


The chgrp command can be abbreviated as change group. You can change the group owner
of the file using chgrp command.

Syntax:

1. chgrp <newGroup> <fileName>

Example:

1. chgrp php file

Look at the above snapshot, earlier 'file' group was sssit. But after passing the
command "chgrp php file" , we have changed the group to php (we have highlighted php
just to show you).
Note: Only root user have the permissison to change the owner or group of the files in the system.

Linux chown: change owner


Command chown is used to change the owner of the file.

Syntax:

1. chown <newOwner> <fileName>

Example:

1. chown jtp list

Look at the above snapshot, we have changed the owner of the file list from sssit to jtp.

Command chown can also be used to change both user owner and group.

Syntax:

1. chown <newOwner:newGroup> <fileName>

Example:

1. chown jtp:php msg.txt

Look at the above snapshot; both user owner and group are changed
to jtp and php respectively.

AD
List of Special Files
When we type ls -l command, ten characters are displayed before user owner and group. First
character tells us about the type of the file.

First Character File Type

- Normal file

d Directory

l Symbolic link

p Named pipe

b Blocked device

c Character device

s Socket

Following are the file types:

Examples of file type:

Look at the above snapshot, first letter c denotes the character device and b denotes the blocked
device
Look at the above snapshot, first letter (-) denotes the normal file and d denotes the directory.

File Permissions
All the three owners (user owner, group, others) in the Linux system have three types
of permissions defined. Nine characters denote the three types of permissions.

1. Read (r) : The read permission allows you to open and read the content of a file. But
you can't do any editing or modification in the file.
2. Write (w) : The write permission allows you to edit, remove or rename a file. For
instance, if a file is present in a directory, and write permission is set on the file but
not on the directory, then you can edit the content of the file but can't remove, or
rename it.
3. Execute (x): In linux type system, you can't run or execute a program unless execute
permission is set.But in Windows, there is no such permission available.

Permissions are listed below:

permission on a file on a directory

r (read) read file content (cat) read directory content (ls)

w (write) change file content (vi) create file in directory (touch)

x (execute) execute the file enter the directory (cd)


Permission Set

Look at the above snapshot, there are ten characters (-rw-rw-r--) before the user
owner. We'll describe these ten characters here.

File permissions for (-rw-rw-r--)

position characters ownership

1 - denotes file type

2-4 rw- permission for user

5-7 rw- permission for group

8-10 r-- permission for other

When you are the User owner, then the user owner permission applies to you. Other
permissions are not relevant to you.

When you are the Group then the group permission applies to you. Other
permissions are not relevant to you.

When you are the other, then the other permission applies to you. User and group
permissions are not relevant to you.

Permission Example

Now we'll show some examples how permissions can be seen for a file or directory.
Look at the above snapshot, different directories and files have different permissions.

First letter (-) or d represents the files and directories respectively.

Now, from remaining nine letters, first triplet represents the permission for user
owner. Second triplet represents the permission for group owner. Third triplet represents
the permission for other .

Setting Permissions With chmod


You can change the permissions with chmod command accordingly to your need.
Below are some examples to change the permissions for different groups.

To add permissions to a group.

Syntax:

1. chmod <groupName>+<permissionName> <fileName>

Example:

1. chmod u+x file

Look at the above snapshot, permission to execute is added to the user owner group.

To remove permissions from a group

Syntax:

1. chmod <groupName>-<permissionName> <fileName>

Example:

1. chmod g-x file


2. chmod u-w file
Look at the above snapshot, permission to execute is removed from the group and
permission to write is removed from the user owner.

To add permission to all the groups together

AD

Syntax:

1. chmod a+<permissionName> <fileName>

Example:

1. chmod a+w file

Look at the above snapshot, we have given permission to write for all the groups.

Note: Similarly, you can also remove the permission for all the groups.

To add permission to all the groups without typing a

Syntax:

1. chmod +<permissionName> <fileName>

Example:

1. chmod +w file
Look at the above snapshot, this example is same as the earlier one only difference is
that we haven't typed a in this.

To set explicit permission

Syntax:

1. chmod <groupName>=<permissions> <fileName>

Example:

1. chmod o=rw file

Look at the above snapshot, we have set explicit permission to read and write for
others.

To set explicit permissions for different groups

Syntax:

1. chmod <groupName>=<permissions> <fileName>

Example:

1. chmod u=rwx,g=rw,o=r file


Look at the above snapshot, we have set permissions for all the three groups.

Setting Octal Permissions


Octal permissions can also be set for the groups.

For example, to set r octal will be 4, to set w octal will be 2, to set x octal will be 1.

Octal Table:

binary octal permissions

000 0 ---

001 1 --x

010 2 -w-

011 3 -wx

100 4 r--

101 5 r-x

110 6 rw-

111 7 rwx

From this we can conclude that,

1. 777 = rwxrwxrwx
2. 765 = rwxrw-r-x
3. 654 = rw-r-xr--

and so on.

Look at the above snapshot, we have shown some random octal examples with the
numbers 777, 274 and 111.

umask
While creating a file or directory, by default a set of permissions are applied. These
default permissions are viewed by umask command.

For safety reasons all Unix systems doesn't provide execution permission to newly
created files.

Adding execution permission is upto you.

mkdir -m

The 'mkdir -m' command can be used to set the mode.

Syntax:

1. mkdir -m <mode> <fileName>

Example:

1. mkdir -m 777 new1


2. mkdir -m 000 new2

Look at the above snapshot, we have created two files new1 and new2 with
mode 777 and 000 respectively.

cp -p

The 'cp -p' command preserves the permissions and time stamps from source files.

Syntax:

1. cp -p <sourceFile> <destinationFile>

Example:

1. cp -p list dupli.txt

Look at the above snapshot, earlier permissions for files list and dupli.txt were
different. But after passing the command "cp -p list dupli.txt", both the files have
same permissions.

Linux Directories
What are Commands

A command is an instruction given to our computer by us to do whatever we want. In


Mac OS, and Linux it is called terminal, whereas, in windows it is called command
prompt. Commands are always case sensitive.

Commands are executed by typing in at the command line followed by pressing


enter key.
This command further passes to the shell which reads the command and execute it.
Shell is a method for the user to interact with the system. Default shell in Linux is
called bash (Bourne-Again Shell).

There are two types of shell commands:

o Built-in shell commands: They are part of a shell. Each shell has some built in
commands.
o External/Linux commands: Each external command is a separate executable
program written in C or other programming languages.

Linux Directory Commands

Directory Description
Command

pwd The pwd command stands for (print working directory). It displays the current
working location or directory of the user. It displays the whole working path
starting with /. It is a built-in command.

ls The ls command is used to show the list of a folder. It will list out all the files
in the directed folder.

cd The cd command stands for (change directory). It is used to change to the


directory you want to work from the present directory.

mkdir With mkdir command you can create your own directory.

rmdir The rmdir command is used to remove a directory from your system.

Linux Home Directory

The Linux home directory is a directory for a particular user of the system and consists of
individual files. It is also referred to as the login directory. This is the first place that occurs
after logging into a Linux system. It is automatically created as "/home" for each user in the
directory'. It is a standard subdirectory of the root directory. The root directory contains all
other directories, subdirectories, and files on the system. It is denoted by a forward slash (/).

The home directory can be said as a personal working space for all the users except root.
There is a separate directory for every user. For example, two users 'jtp1' and 'jtp2' will have
directories like "/home/jtp1" and "/home/jtp2". These users will have all the rights under their
directory files.

The root (administrative) user is the only user who has its home directory in a different
location by default. The path of the root user is '/root/', where it has control under all the
directories and files.
Shell Programming in Linux
What is a shell?
It is an interface between the user and operating system service. A shell provides
users with an interface that not only accepts human-readable commands but also
executes them.

An operating system consists of many parts, but the 2 core parts are the kernel and
shell, we know that the kernel is like the atom of the operating system, the sole
heart of the operating system. The kernel works a mediator between the software
and hardware.

The shell on the other hand is more like the right hand of the kernel, it takes input
from you in the form of commands, processes it, and then gives an output. The
shell is retrieved via the terminal that runs it. Shell also provides an interface
through which a user works on programs and scripts.
Types of shell

Shell is broadly classified into 2 categories:

1. Command line shell


The shell can be accessed by using a command-line interface, and we all know
what a command line is. It is nothing but the terminal in Linux – the lean, mean
coding machine. The terminal is a special program to type in the human-readable
commands such as “cat”, “ls”, “cd”, “pwd”, “mkdir”, and a thousand more!

When you execute the commands, the result is then displayed on the terminal to
the user. The terminal is powerful and allows users to store commands in a file and
execute them together. Linux is not the only operating system with a command
line, even windows had the command prompt and so doe mac OS

The command line is a boon for developers and administrators, but it may not be
everyone‟s cup of coffee as memorizing so many command sand and an even
longer list of options is not an easy task. That is why there are 2 options for the
shell so that users can use what they prefer most.

2. Graphical shell
The graphical shell is nothing but the GUI – Graphical interface design. The
graphical shell provides an interface for manipulating programs based on the
graphical user interface. The GUI allows users to do bath tasks that the terminal
can and can not do.

For example, you can do tasks like allowing for operations such as opening,
closing, moving, and resizing windows, as well as switching focus between
windows and more which the terminal can‟t. The main advantage of the GUI is
that users need not type commands for every small task.

There are various shells that are available for Linux based operating systems, let us
look at some of them:

1. BASH (Bourne Again SHell)


The bash shell is the most loved and commonly used shell in Linux-based
operating systems. It is a default login shell in Linux systems and in macOS,
however, it can also be installed on Mi9crosoft Windows.

The prompt for the bash shell is a dollar sign ($). The bash shell is a derivative of
the Bourne shell, other derivatives of the Bourne shell include POSIX and Korn.

2. KSH (Korn SHell)


The Korn shell is also derived from the Bourne shell but is the base for POSIX. It
was written, designed, and developed by David Korn in the early 1980s Some
features of KSH are:

a. It provides object-oriented programing since ksh93t.


b. It has many mathematical and dynamic search functions.
c. it is very similar to C language and even has C-language-like expressions
d. You get a choice of 3 command line editing styes: vi, Emacs, and Gosling.
e. KSH includes reference variables and hierarchically nested variables.
f. It has associative arrays and built-in floating-point arithmetic operators
g. the Korn shell provides process substitution and process redirection.

3. CSH (C shell)
The C shell was written, designed, and developed at the University of California by
Bill Joy. J8ust like bash has the prompt “$”, chs has the prompt “%”. The C shell
was developed to be very similar to the C programming language, it also includes
useful programming features like in-built support for arithmetic operations

The C shell feature „aliases‟ and command history which was missing in different
types of shells in Linux like the Bourne shell. The complete pathname for the C
shell is /bin/sch.

There are many other shells available like:

 ZSH (Z shell)
 Fish Shell
 POSIX
 TENEX
What is shell scripting?
We saw that shells are interactive, they accept commands as input from users and
execute them. But if we want to execute a bunch of commands again and again
(like a loop), we have to type in all commands each time in the terminal.

To avoid this, the shell can also take in the commands as the input from a file,
where we can compile all of these commands and execute them. These files are
called Shell programs or shell scripts and have the extension of “.sh”.

Shell programs, just like any other programming language in the world, have a
specific syntax and a set of rules. A shell script contains the following elements:

 Keywords: if, else, break continue, etc.


 Commands: cd, ls, echo, touch, cat, pwd, mkdir, etc.
 Loops: if…then…else, while…do, do…while, case, etc.
 Functions
We will look at each of the above-mentioned and more in detail in the practical
part.

Why use shell programming?


There are a gazillion reasons why you should use shell programming, here are a
few:

1. Avoids repetitive work and automation


2. Helps in system monitoring
3. Used for routine backups
4. Adding new functionality to shell
5. Creating custom filters
7. Helps in automating various tasks
8. Changing time and date
9. Printing current working directory
10. Scheduling jobs

Advantages of shell programming


Again, if we start writing the advantages of the list advantages of shell
programming, it may never end, nonetheless here are 8 advantages of it:

 Easy to use
 Portable
 Quick start
 Interactive debugging
 Same syntax as that of command line
 Avoids repetitive work and automation
 Shell scripting is much quicker than writing commands in the terminal
 Simple up learn
Disadvantages of shell programming
They say that every good thing has its negatives, which also hold true for shell
programming, here are some of its disadvantages:

 Slow execution speed


 Not suited for large and complex tasks
 Design flaws within the language syntax
 Provides minimal data structure, unlike other scripting languages
 Prone to costly errors

STANDARD INPUT AND STANDARD OUTPUT,STANDARD ERROR:


There is a decent chance that if you have used Linux operating systems then
you might have encountered the three famous data streams known as stdin,
stderr and stdout. All these are different in their functions and have their own
uses but one thing common between all three of them is that they are data
streams that bash creates.
Let‟s understand more about what data streams actually mean and how they are
beneficial. In terms of computing, a data stream is something that gives us the
ability to transfer data from a source to an outflow and vice versa. The source
and the outflow are the two end points of the data stream. It might be
interesting for you to know that whatever command you are running in your
Linux terminal, it will either be at one of these end points.
Now we know a little about the data streams, let‟s know more about the three
famous data streams.
 stdin − It stands for standard input, and is used for taking text as an input.
 stdout − It stands for standard output, and is used to text output of any
command you type in the terminal, and then that output is stored in
the stdout stream.
 stderr − It stands for standard error. It is invoked whenever a command
faces an error, then that error message gets stored in this data stream.
It should be noted that in Linux all these streams are treated as if they were
files. Also, linux assigns unique values to each of these data streams.

0 = stdin
 1 = stdout
 2 = stderr
Now let‟s consider a few examples of these three data streams.
The example shown below depicts a typical stdin stream.
Command
read

Output
mmukul@192 Docs-Linux % read
This is to stdin
In the above command, we are providing an input to the stream and the read tool is
getting the input from stdin.
Now, an example of stdout is shown below −

Command
ls -ltr

Output
immukul@192 Downloads % ls -ltr
total 1085456
drwxr-xr-x@ 13 immukul staff 416 Dec 7 2019 source-code-pro-release
-rw-r--r--@ 1 immukul staff 350337 Dec 22 2019 messi.jpg
-rw-r--r--@ 1 immukul staff 5953321 Dec 22 2019 927225.png
-rw-r--r--@ 1 immukul staff 601852 Dec 22 2019 238870.jpg
.
We know that we make use of the ls command with the -ltr flag to list all the files in
a certain sorted fashion, where the lastly updated file is shown at the bottom. The
list is sent to the stdout data stream and the terminal then simply prints it out.
Now, an example of stderr is shown below −

Command
ls -ltr printit
The above command is invalid as I don’t have any directory named printit and it will
generate an error message that will be sent to stderr and then the terminal will print
it.

Output
immukul@192 Downloads % ls -ltr printit
ls: printit: No such file or directory
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner
CamScanner

You might also like