0% found this document useful (0 votes)
40 views158 pages

Linux Systems Slides Part1

This document provides an overview of Linux systems and their components. It discusses what an operating system is and why they are needed. It then describes Linux, including what it is, why it is used, and its key properties like multitasking and protected memory. The document outlines Linux's directory structure and components. It also discusses the user interfaces for Linux including graphical user interfaces, command line interfaces, and an introduction to shells as the command interpreter.

Uploaded by

Sajith
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)
40 views158 pages

Linux Systems Slides Part1

This document provides an overview of Linux systems and their components. It discusses what an operating system is and why they are needed. It then describes Linux, including what it is, why it is used, and its key properties like multitasking and protected memory. The document outlines Linux's directory structure and components. It also discusses the user interfaces for Linux including graphical user interfaces, command line interfaces, and an introduction to shells as the command interpreter.

Uploaded by

Sajith
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/ 158

Linux Systems

Getting started with setting up an Embedded platform

Team Emertxe
Overview of Linux Systems
Linux Systems
Introduction - Let us ponder ...

What exactly is an Operating System (OS)?

Why do we need OS?

How would the OS would look like?

Is it possible for a team of us (in the room) to create an
OS of our own?

Is it necessary to have an OS running in a Embedded
System?

Will the OS ever stop at all?
Linux Systems
Introduction - Operating System

Humans

Program
Interface

User
Programs
Compiler Assembler Text Editor Database
OS
System and Application Programs Interface

Operating
System Operating System
HW
Interface/
Privileged
Instr
Hardware
Linux Systems
Introduction - What is Linux?

Linux is a free and open source operating system that is
causing a revolution in the computer world

Originally created by Linus Torvalds with the assistance of
developers called community

This operating system in only a few short years is
beginning to dominate markets worldwide

Today right from hand-held devices (ex: Android)
to high end systems (ex: Stock exchange servers)
use Linux
Linux Systems
Introduction - Why use Linux?

Free & Open Source –GPL license, no cost

Reliability –Build systems with 99.999% upstream

Secure –Monolithic kernel offering high security

Scalability –From mobile phone to stock market servers

The word 'Free' in Open Source should be interpreted as in


'Freedom' not as 'Free Beer'. This also explains the spirit of
creating Open Source software.
Linux Systems
Introduction - What is Open Source?

Freedom of
software

Freedom of Freedom of
use redistribute

Freedom of Freedom of
modify copy
Linux Systems
Introduction - Open Source - How it all started?

With GNU (GNU is not UNIX)

Richard Stallman made the initial announcement in 1983, Free
Software Foundation (FSF) got formed during 1984

Volunteer driven GNU started developing multiple projects,
but making it as an operating system was always a challenge

During 1991 a Finnish Engineer Linus Torvalds developed core
OS functionality, called it as “Linux Kernel”

Linux Kernel got licensed under GPL, which laid strong
platform for the success of Open Source

Rest is history!
Linux Systems
Introduction - Open Source - How it evolved?

Multiple Linux distributions started emerging around the Kernel

Some applications became platform independent

Community driven software development started picking up

Initially seen as a “geek-phenomenon”, eventually turned out to be
an engineering marvel

Centered around Internet

Building a business around open source started becoming viable

Redhat set the initial trend in the OS business
Kernel
Applications
Customization
Linux Systems
Introduction - Open Source - Where it stands now?
OS Databases Server/Cloud Enterprise

Consumer Education CMS eCommerce


Linux Systems
Introduction - Open Source vs Freeware

OSS Freeware
 Users have the right to access &  Freeware is usually distributed in a
modify the source codes form of binary at ‘Free of Charge’,
 In case original programmer but does not open source codes
disappeared, users & developer itself.
group of the S/W usually keep its  Developer of freeware could
support to the S/W. abandon development at any time
 OSS usually has the strong users & and then final version will be the
developers group that manage and last version of the freeware. No
maintain the project enhancements will be made by
others.
 Possibility of changing its licensing
policy
Linux Systems
Introduction - GPL

Basic rights under the GPL – access to source code, right
to make derivative works

Reciprocity/Copy-left

Purpose is to increase amount of publicly available
software and ensure compatibility

Licensees have right to modify, use or distribute
software, and to access the source code
Linux Systems
Introduction - GPL - Issues

Linking to GPL programs

No explicit patent grant

Does no discuss trademark rights

Does not discuss duration

Silent on sub-licensing

Relies exclusively on license law, not contract
Linux Systems
Introduction - Linux Properties
What has made Linux so popular to scale from mobile devices to
powering 90% of world’s super computer? Here are the key
properties of Linux

Multitasking
– Ability to handle multiple tasks across single / multiple processors

Multi-user
– Have got users with different level of privileges for secured access

Protected Memory
– Clear distinction called ‘user-space’ and ‘kernel’ space thereby having protected
memory access. This makes Linux Super secure comparing with other operating systems

Hierarchical File System
– Well organized file system that handles various types of files. This also makes handling
various inputs very simple
Linux Systems
Introduction - Linux Components

Hardware Controllers: This subsystem is
comprised of all the possible physical
devices in a Linux installation - CPU,
User
Application
memory hardware, hard disks

User Space

Linux Kernel: The kernel abstracts and
mediates access to the hardware
GNU resources, including the CPU. A kernel is
C
Library the core of the operating system
Linux


O/S Services: These are services that are
System Call Interface typically considered part of the operating
system (e.g. windowing system, command
Kernel Space

shell)
Kernel

User Applications: The set of applications
Architecture Dependent in use on a particular Linux system (e.g.
Kernel Code
web browser)
Hardware Platform
Linux Systems
Introduction - Linux Directory Structure
/ bin Essential user command binaries
boot Static boot-able images
dev Device files
etc Host specific configuration
home User home directories
lib Essential shared libraries and kernel modules
media Mount point for removable media
mnt Mount point for temporarily mounted file systems
proc Virtual FS documenting kernel and process status
root Root user's home directory
sbin Essential super user command binaries
opt Add-on application software packages
tmp Temporary files
usr Multi user utilities and application
var Variable files (Logs)
User Interfaces
Linux Systems
User Interface - GUI


In graphical mode the user will be given a GUI using which he / she will be
able to use the system using mouse

Similar to windows based system that exist in other operating systems like
MS Windows & Apple MAC OS
Linux Systems
User Interface - CLI


Textual mode used to execute requested commands

Our focus is to be in the CLI mode by executing various commands by invoking shells.
We will also create programs using this environment called ‘Shell scripts’
Linux Systems
User Interface - The Shell - Introduction


Shell is an application, works as a command interpreter

Gets a command from user, gets it executed from OS

Gives a programming environment to write scripts using
interpreted language

It has been inherited from UNIX operating system, which was
predecessor to Linux
Linux Systems
User Interface - The Shell - Types


Login
– Starts after a successful login
– It is executed under user ID during login process
– It picks up user specific configuration and loads them
Linux Systems
User Interface - The Shell - Types


Non Login
– A Non login shell is started by a program without a login
– In this case, the program just passes the name of the shell
executable
– For example, for a Bash shell it will be simply bash
– Following are examples of Non-login shells:

sh

bash

ksh

csh
Linux Systems
User Interface - The Shell - Invocation


The main task of a shell is providing a user environment

Input
(ls)

shell

List all
Error
the
report
files
Linux Systems
User Interface - The Shell - Bash


Bash – The command interpreter

GNU Project's shell

Bash is the Bourne Again Shell

Some features of bash are
– Command line editing
– Unlimited size command history
– Job Control
– Shell Functions and Aliases
– Indexed arrays of unlimited size
– Integer arithmetic in any base from two to sixty-four
Shell Usage and Basic Commands
Linux Systems
Basic Shell Commands – Points to be Noted

It is assumed you follow the slides sequence, since some
of the commands explained assumes, you have executed
them before trying the next one

After typing the command its expected you press an
enter key
Linux Systems
Basic Shell Commands - pwd

As soon as we open an terminal we are taken to the users
home directory

This can be known with the pwd command
user@user:~] pwd
/home/user
user@user:~]


Basically, the pwd command tells you about current
working directory
Linux Systems
Basic Shell Commands - ls

Well, we come to know where we are, how do we know
what do we have there?
user@user:~] ls
Desktop Downloads Pictures Templates Videos hello.c
Documents Music Public Test1 bin test2
user@user:~]


The list command ls helps us here. The ls display the
contents of the current directory.

Well one question should pop up about the category of
these files?

From the above image, can tell which is a directory, file
etc..?
Linux Systems
Basic Shell Commands - man

So how do we come to know about different listing
options?
user@user:~] man ls

You may use man command, which stands for manual

The most useful command which acts as reference
manual if you work in Linux system

Almost all the installed applications, libraries and all
would have its own manual entry

Even man has its own man page!
user@user:~] man man

This helps us to know how read man pages and different
sections in it
Linux Systems
Basic Shell Commands - man

Hey, what? It looks too complex!!

Well, yes as starter most of the things looks complex,
that too a technical documentation

Habit of reading it would certainly make it easy.
MAN(1) ⇐ This is a man page section, There is chance of a command MAN(1)
and libray function have a same name, so section identifies it

NAME
The name of command / function is described here

SYNOPSIS
How to use the command gets described here

DESCRIPTION
Description of the command and function comes here

OTHER SUBSECTION ⇐ Based on the man page different sub section come
below here
Linux Systems
Basic Shell Commands - man

So from man page of ls we get the following option
user@user:~] ls -F
Desktop/ Downloads/ Pictures/ Templates/ Videos/ hello.c
Documents/ Music/ Public/ Test1/ bin/ test2@
user@user:~]


Where
/ → Directory
@ → Symbolic Link
* → Executable
| → Pipe
and more
Linux Systems
Basic Shell Commands – Anatomy of a Command
user@user:~] command_name [arguments]

[arguments]

A command may have multiple arguments

Arguments could be options to the command, file paths or arguments itself
options starts with – which is called as short options which has single letter or –– called as
long options with a word

Some of the arguments are optional which is mentioned within [ ]

The below example shows the contents of Documents directory with color option enabled
user@user:~] ls -l --color Documents/
command_name

A command which gets interpreted by shell

Could be a super user command which gets executed with sudo

The below is an example shows how to install a package in Ubuntu
user@user:~] sudo apt install vim
user@user:~]

Command prompt, which could be customized

Default prompt after installation would look like as shown below
user@user:~$
Linux Systems
Basic Shell Commands – Types of commands

An executable program like all those files can have in
/usr/bin.

A command built into the shell itself. bash provides a
number of commands internally called shell built-ins The
cd command, for example, is a shell built-in

A shell function. These are miniature shell scripts
incorporated into the environment.

An alias. Commands that you can define yourselves, built
from other commands.
Linux Systems
Basic Shell Commands – Types of commands

To know the type of a command, you may try the
following
user@user:~] type <command_name>


Few examples
user@user:~] type ls
ls is aliased to `ls -v --color=auto'
user@user:~] type pwd
pwd is a shell builtin
user@user:~] type clear
clear is /usr/bin/clear
user@user:~]
Linux Systems
Basic Shell Commands - cd

cd to change directory. A shell built-in command
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates Videos
user@user:~] cd Documents/
user@user:Documents]


The above example changes the directory to Documents

Now how to go back?!

We need to understand the concept of the path, which is
explained in the next slide
Linux Systems
Basic Shell Commands - Path

Path is the location where a particular file is located in
the directory (tree) structure

It starts with the root (‘/’) directory and goes into
appropriate directory

The path depends on the reference point from where you
take it up:
– Absolute Path: Specifies the location with reference from
root directory
– Relative Path: Specifies the location with reference to
present working directory (pwd)

As the name says relative path will vary depending on your
present working directory
Linux Systems
Basic Shell Commands - Path
/ user@user:~] ls
Desktop Documents Downloads Music Pictures
bin Public Templates Videos
boot user@user:~]

dev
etc
home user
lib
Desktop
media Documents
mnt Downloads
proc Music
root Pictures
sbin Public
sys Templates
tmp Videos
usr
var
Linux Systems
Basic Shell Commands - Path - Relative
/ user@user:~] ls
Desktop Documents Downloads Music Pictures
bin Public Templates Videos
boot user@user:~] cd Documents/
user@user:Documents]
dev
etc
home user
lib
Desktop
media Documents
mnt Downloads
proc Music
root Pictures
sbin Public
sys Templates
tmp Videos
usr
var
Linux Systems
Basic Shell Commands - Path - Relative
/ user@user:~] ls
Desktop Documents Downloads Music Pictures
bin Public Templates Videos
boot user@user:Documents] cd ..
user@user:~]
dev
etc
home user
lib
Desktop
media Documents
mnt Downloads
proc Music
root Pictures
sbin Public
sys Templates
tmp Videos
usr
var
Linux Systems
Basic Shell Commands - Path - Absoulte
/ user@user:~] ls
Desktop Documents Downloads Music Pictures
bin Public Templates Videos
boot user@user:Documents] cd /home/user/
user@user:~]
dev
etc
home user
lib
Desktop
media Documents
mnt Downloads
proc Music
root Pictures
sbin Public
sys Templates
tmp Videos
usr
var
Linux Systems
Basic Shell Commands - mkdir

mkdir to create directories
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates Videos
user@user:~] mkdir TBD # TBD - ToBeDeleted
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~]


Creates the directory(ies), if they do not already exists
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~] mkdir TBD
mkdir: cannot create directory ‘TBD’: File exists
user@user:~]


Well, # represents start of comment in shell, anything
written after that would be seen as comment!
Linux Systems
Basic Shell Commands - rmdir

rmdir to remove empty directories
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~] rmdir TBD
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates Videos
user@user:~]


Removes the directory(ies), if they are empty
user@user:~] mkdir TBD # TBD - ToBeDeleted
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~] cd TBD
user@user:TBD] mkdir Test
user@user:TBD] cd ..
user@user:~] rmdir TBD
rmdir: failed to remove 'TBD': Directory not empty
user@user:~]
Linux Systems
Basic Shell Commands - rm

The previous slide leads to a question on how to delete a
non empty directory?

rm to remove files or directories

Removes each specified file. By default, it does not
remove directories.
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~] ls TBD
Test
user@user:~] rm TBD
rm: cannot remove 'TBD/': Is a directory
user@user:~]


Then how??, Well we need to refer the man pages
Linux Systems
Basic Shell Commands - rm

From the man page or rm you find a option -r which
stands for recursive
user@user:~] rm -r TBD
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates Videos
user@user:~]


Note, once deleted, you loose the files permanently!, its
equivalent to Shift + Delete

Now what if you delete a file or a folder mistakenly?
Wouldn’t you like that the shell ask you before you
delete? So that you avoid these types of issues!

Well will see it in the next slide
Linux Systems
Basic Shell Commands - rm

The -i option provides interactivity
user@user:~] mkdir TBD # TBD - ToBeDeleted
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~] cd TBD
user@user:TBD] mkdir Test
user@user:TBD] cd ..
user@user:~] rm -ri TBD
rm: descend into directory 'TBD/'? y
rm: remove directory 'TBD/Test'? y
rm: remove directory 'TBD/'? y
user@user:~]


Did you observe from the above screen shot that the shell
prompts before you take any action!, you may say y or n

There is always a second chance

But what if you forget -i?
Linux Systems
Basic Shell Commands - alias

The solution the question in the previous slide is alias

An builtin bash command, which helps us to name an
operation (command), literally anything

Some built aliases are as shown below
user@user:~] alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -l'
alias ls='ls --color=auto'
user@user:~]


List varies based on your system installation

Well how do we create one? How does it solve our issue?
Linux Systems
Basic Shell Commands - alias

We may create our own alias with required options as
shown below
user@user:~] alias rm=’rm -i’
user@user:~] alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -l'
alias ls='ls –color=auto'
alias rm=’rm -i’
user@user:~]


Note our alias at the end

A new command (operation) named rm is created, when
used henceforth will be rm -i
Linux Systems
Basic Shell Commands - alias

Lets repeat all the steps again to test our new command
user@user:~] mkdir TBD # TBD - ToBeDeleted
user@user:~] ls
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:~] cd TBD
user@user:TBD] mkdir Test
user@user:TBD] cd ..
user@user:~] rm -r TBD
rm: descend into directory 'TBD/'? y
rm: remove directory 'TBD/Test'? y
rm: remove directory 'TBD/'? y
user@user:~]

Observe that, this time we didn’t provide the -i option!

How does it work?, its the because of our alias we
created.

The name of new command can be anything, but let it be
meaningful
Linux Systems
Basic Shell Commands - alias

The only issue of the new command is that, it is only
available on the terminal it is created??

As soon as we close the terminal its all gone!!. So what is
the point. How do we make it permanent??

For that we need to about some of the files used by Bash
Linux Systems
Basic Shell Commands – Bash Files

Hidden files used by the login as well as non-login shells

Found in users home directory
user@user:~] pwd
/home/user
user@user:~] ls -a
. .bash_logout .cache Documents Pictures Templates
.. .bash_profile .config Downloads .profile Videos
.bash_history .bashrc Desktop Music Public .vimrc
user@user:~]


These are some configuration files which gets sourced to
bash on every new instance is created
Linux Systems
Basic Shell Commands – Bash Files - .bash_profile

Used by the login shell

Any action which is to be carried out while logging in
through login shell can be put here

Found in the users home directory, if not could be
created
Linux Systems
Basic Shell Commands – Bash Files - .bash_logout

Any action which is to be carried out while logging out
through login shell can be put here

Found in the users home directory
Linux Systems
Basic Shell Commands – Bash Files - .bashrc

Used by the non login shell

Any action which is to be carried after you logged into
your machine and open a new terminal window

This is executed on every new instance of the new
terminal even if it is in a form of tabs

So all those the stuff you would like to get sourced the
bash while starting, then this would be right file

By adding the alias in this file would make it persistent
which is raised a question in this slide
Linux Systems
Basic Shell Commands – Bash Files - .bash_history

A file which store all the user activity on the command
prompt for future reference.

The history command shows the entries of this file
Linux Systems
Basic Shell Commands - touch

An command which helps us to update the timestamp of
the existing files

If the file argument that does not exist is created empty
user@user:~] mkdir TBD # TBD - ToBeDeleted
user@user:~] cd TBD
user@user:TBD] ls
user@user:TBD] touch file1 file2 file3
user@user:TBD] ls
file1 file2 file3
user@user:TBD]


Observe that the directory was empty, using touch command it
created the mentioned files

As mentioned it is generally used to update the time stamp, so
you wonder what would be the use of it?

Well, they are used by some advanced commands like make
Linux Systems
Basic Shell Commands - touch

The scope of this topic is to create some empty files
which will help learning some of the commands we are
going cover next slides

But lets see the behavior of the command a bit
user@user:TBD] ls -l
total 0
-rw-rw-r-- 1 user user 0 Nov 30 22:18 file1
-rw-rw-r-- 1 user user 0 Nov 30 22:18 file2
-rw-rw-r-- 1 user user 0 Nov 30 22:18 file3
user@user:TBD]


Note the creation time of all the files
user@user:TBD] touch file1 file2 file3 # 20 Seconds after creation!
user@user:TBD] ls -l
total 0
-rw-rw-r-- 1 user user 0 Nov 30 22:38 file1
-rw-rw-r-- 1 user user 0 Nov 30 22:38 file2
-rw-rw-r-- 1 user user 0 Nov 30 22:38 file3
user@user:TBD]
Linux Systems
Basic Shell Commands - cp

cp to copy files and directory

Accepts to 2 arguments as the source and destiny
user@user:TBD] cp <source> <destiny>


The source and destiny are the path of files to be copied
from and to
user@user:TBD] ls
file1 file2 file3
user@user:TBD] cp file1 file4
user@user:TBD] ls
file1 file2 file3 file4
user@user:TBD] cp file1 ../file5
user@user:TBD] ls ../
Desktop Documents Downloads file5 Music Pictures Public Templates
TBD Videos
user@user:TBD] cp ../file5 .
user@user:TBD] ls
file1 file2 file3 file4 file5
user@user:TBD]
Linux Systems
Basic Shell Commands - cp

While copying the directory the -r option has to be used
user@user:TBD] mkdir Test
user@user:TBD] ls
file1 file2 file3 file4 file5 Test
user@user:TBD] cp Test/ TestCopy
cp: -r not specified; omitting directory 'Test/'
user@user:TBD] cp -r Test/ TestCopy
user@user:TBD] ls
file1 file2 file3 file4 file5 Test TestCopy
user@user:TBD] cp file1 file2 file3 Test
user@user:TBD] ls Test/
file1 file2 file3
user@user:TBD]
Linux Systems
Basic Shell Commands - mv

mv to move of rename files and directories

Accepts to 2 arguments as the source and destiny
user@user:TBD] mv <source> <destiny>

The source and destiny are the path of files to be moved
or renamed from and to
user@user:TBD] ls ../
Desktop Documents Downloads file5 Music Pictures Public Templates
TBD Videos
user@user:TBD] mv ../file5 TestCopy
user@user:TBD] ls TestCopy
file5
user@user:TBD] ls ../
Desktop Documents Downloads Music Pictures Public Templates TBD
Videos
user@user:TBD]

Note the above context, the file gets moved
Linux Systems
Basic Shell Commands - mv
user@user:TBD] ls
file1 file2 file3 file4 file5 Test TestCopy
user@user:TBD] mv file1 file1.txt
user@user:TBD] ls
file1.txt file2 file3 file4 file5 Test TestCopy
user@user:TBD]

In this context, the file gets renamed
Visual Editor - vi
Linux Systems
Visual Editor - vi


Screen-oriented text editor originally created for the
Unix operating system

The name vi is derived from the shortest unambiguous
abbreviation for the ex command visual

Improved version is called as vim

To open a file
user@user:TBD] vi <file_name>

or
user@user:TBD] vim <file_name>
Linux Systems
Visual Editor - vim

vi opens a file in command mode to start mode.

The power of vi comes from the following modes
Mode Functions Key
Escape Search Esc
File Edit
Edit Insert i/I
Replace r/R
Visual Selection v/V
Command Line Commands :


To get a basic grip on vi you may type vimtutor on the
terminal. Just read and follow the instructions
user@user:TBD] vimtutor
Linux Systems
Visual Editor – vim - Information
user@user:TBD] ls
file1.txt file2 file3 file4 file5 Test TestCopy
user@user:TBD] vi file.txt


~
~
~
~
~
~
~
~
~
"file.txt" [New File] 0,0-1 All

filename, file line, cursor view %


mode, command
previous action information
command prompt
Linux Systems
Visual Editor – vim - Modes
This mode is default as soon as you open a file. You may press ESC key any
time to enter this mode
~
2,23 All

Escape Mode. Press ESC

This is the mode where, you would be entering the text. Most of the file
Commands will not work here!. Press i anytime while in ESC mode
~
-- INSERT -- 2,63 All

Insert Mode. Press i


The existing text will be over written or replaced with new one. You may
press R anytime to get into this mode while in ESC mode
~
-- REPLACE -- 2,55 All

Replace Mode. Press ESC Press R


Linux Systems
Visual Editor – vim - Modes
This mode helps us to select a part of the content.
You may press ESC and v to enter into this mode and your arrow keys to select
the text you want. After that you may use any of the file edit command and
perform the required opertion. Observe the selection made above!!
~
-- VISUAL -- 2 3,18 All

Visual Mode. Press ESC Press v


This mode helps us to select a part of the content.
You may press ESC and CTRL v to enter into this mode and your arrow keys to
select the block you want. After that you may use any of the file edit
command and perform the required opertion. Observe the selection made!!
~
-- VISUAL BLOCK -- 4x20 4,42 All

Visual Block Mode. Press ESC Press Ctrl v


Linux Systems
Visual Editor – vim - Modes
Press ESC and : brings us in command mode.
You may use the command required and press ENTER
~
~
:<Type your command here>

Command Line Mode. Press ESC Press :


Linux Systems
Visual Editor – vi – Insert
user@user:TBD] vi test.txt

~
"test.txt" [New File] 0,0-1 All

Press i
~
~
-- INSERT -- 0,1 All

Start typing something

Hey all!, let do some vimming :)


~
-- INSERT -- 0,33 All

Press ESC
Linux Systems
Visual Editor – vi – Insert and Undo
Hey all!, let do some vimming :)
~
"file.txt" 1L, 67C 1,32 All

Press i and start typing something


Hey all!, let do some vimming :THE TEXT GETS INSERTED HERE)
~
-- INSERT -- 1,60 All

Press ESC
Hey all!, let do some vimming :THE TEXT GETS INSERTED HERE)
~
^[ 1,59 All

Press u to Undo

Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago 1,32 All
Linux Systems
Visual Editor – vi – Insert
Press I and observe the cursor and start typing

Hey all!, let do some vimming :)


~
-- INSERT -- 1,1 All

THE TEXT GETS INSERTED HEREHey all!, let do some vimming :)


~
^[ 1,28 All

Press ESC
THE TEXT GETS INSERTED HEREHey all!, let do some vimming :)
~
1,27 All

Press u to Undo

Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago 1,32 All
Linux Systems
Visual Editor – vi – Write and Quit
Hey all!, let do some vimming :)
~
~
~
:w

To save. Press w and ENTER


Hey all!, let do some vimming :)
~
~
~
:"test.txt" 1L, 57C [w] 1,56 All

Hey all!, let do some vimming :)


~
~
~
:q

To quit. Press q and ENTER


Linux Systems
Visual Editor – vi – Write and Quit
user@user:TBD] ls
file1.txt file2 file3 file4 file5 Test TestCopy
user@user:TBD] vi test.txt
user@user:TBD] ls
file1.txt file2 file3 file4 file5 Test TestCopy test.txt
user@user:TBD]


Just to understand this better, lets learn the cat
command before we proceed further
Linux Systems
vi - Basic Shell Commands - cat


cat to concatenate files and print on standard output
user@user:TBD] ls
file1.txt file2 file3 file4 file5 Test TestCopy
user@user:TBD] vi test.txt
user@user:TBD] ls
file1.txt file2 file3 file4 file5 Test TestCopy test.txt
user@user:TBD] cat test.txt
Hey all!, let do some vimming :)
user@user:TBD]

Displays the contents of the file on the screen

cat has many advanced functionalities which will be
covered later as required
Linux Systems
Visual Editor – vi – Write and Quit
user@user:TBD] vi test.txt

Hey all!, let do some vimming :)


~
~
~
:wq

To save and quit. Press wq and ENTER


user@user:TBD] vi test.txt

Hey all!, let do some vimming :)


Added this line too!! which will be missing!
~
~
:q!

To quit without saving. Press q! and ENTER


Linux Systems
Visual Editor – vi – Append

Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago 1,66 All

Press a and start typing something

HTHE TEXT GETS INSERTED HEREey all!, let do some vimming :)


~
-- INSERT -- 1,1 All

Press ESC
HTHE TEXT GETS INSERTED HEREr all!, let do some vimming :)
~
^[ 1,1 All

Press u to Undo

Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago 1,1 All
Linux Systems
Visual Editor – vi – Append
Press A and start typing something

Hey all!, let do some vimming :)


~
-- INSERT -- 1,1 All

Hey all!, let do some vimming :)THE TEXT GETS INSERTED HERE
~
^[ 1,28 All

Press ESC
Hey all!, let do some vimming :)THE TEXT GETS INSERTED HERE
~
^[ 1,1 All

Press u to Undo

Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago 1,1 All
Linux Systems
Visual Editor – vi – Yank (Copy) and Paste

Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago y 1,1 All

Press ESC and yy and start typing something Press p

Hey all!, let do some vimming :)


Hey all!, let do some vimming :)
~
~
1 change; before #4 1 seconds ago 2,1 All

You may copy multiple lines and paste times using the
following syntax
– [n]yy – Copy n line(s) → Say n = 10, 10yy copies 10 lines
– [n]p – Paste copied line(s) n time → Say n = 10, 10p
pastes the copied line(s) 10 times

Note, this rule applies to almost all the commands
Linux Systems
Visual Editor – vi – Open
Hey all!, let do some vimming :)
Hey all!, let do some vimming :)
~
~
1 change; before #4 1 seconds ago 2,1 All

Press o
Hey all!, let do some vimming :)
Hey all!, let do some vimming :)

~
-- INSERT -- 3,1 All

Press ESC Press u to Undo


Hey all!, let do some vimming :)
Hey all!, let do some vimming :)
~
~
1 change; before #4 1 seconds ago 2,1 All
Linux Systems
Visual Editor – vi – Open
Hey all!, let do some vimming :)
Hey all!, let do some vimming :)
~
~
1 change; before #4 1 seconds ago 2,1 All

Press O

Hey all!, let do some vimming :)

Hey all!, let do some vimming :)


~
-- INSERT -- 3,1 All

Press ESC Press 2u to Undo


Hey all!, let do some vimming :)
~
~
~
2 fewer lines; #4 1 seconds ago 2,1 All
Linux Systems
Visual Editor – vi – Increment

1 line Hey all!, let do some vimming :)


~
1 change; before #4 1 seconds ago y 1,1 All

Press ESC and yy and start typing something Press p

1 line Hey all!, let do some vimming :)


1 line Hey all!, let do some vimming :)
~
~
2 fewer lines; #4 1 seconds ago 2,1 All

Press CRTL a and observe the 2 nd


line

1 line Hey all!, let do some vimming :)


2 line Hey all!, let do some vimming :)
~
~
2 fewer lines; #4 1 seconds ago 2,1 All

Press yyp
Linux Systems
Visual Editor – vi – Go
1 line Hey all!, let do some vimming :)
2 line Hey all!, let do some vimming :)
2 line Hey all!, let do some vimming :)
~
2 fewer lines; #4 1 seconds ago 3,1 All

Press CRTL a and observe the 3 rd


line

1 line Hey all!, let do some vimming :)


2 line Hey all!, let do some vimming :)
3 line Hey all!, let do some vimming :)
~
2 fewer lines; #4 1 seconds ago 3,1 All

Press gg and observe the cursor position

1 line Hey all!, let do some vimming :)


2 line Hey all!, let do some vimming :)
3 line Hey all!, let do some vimming :)
~
2 fewer lines; #4 1 seconds ago 1,1 All
Linux Systems
Visual Editor – vi – Go


The most useful shortcut to navigate between different
lines
– [n]gg – Go to nth line → Say n = 10, 10gg takes you to 10
line
– gg take you the first line of the file
– G take you the last line of the file
Linux Systems
Visual Editor – vi – Delete
1 line Hey all!, let do some vimming :)
2 line Hey all!, let do some vimming :)
3 line Hey all!, let do some vimming :)
~
2 fewer lines; #4 1 seconds ago 1,1 All

Press 2dd and observe


3 line Hey all!, let do some vimming :)
~
~
~
2 fewer lines; #4 1 seconds ago 1,1 All

You may delete multiple lines from the current cursor
position
– [n]dd – Delete n line(s) → Say n = 10, 10dd delete 10 lines
– D the current line from the cursor position
Linux Systems
Visual Editor – vi – Decrement

Press 2 CTRL x and observe


1 line Hey all!, let do some vimming :)
~
~
~
2 fewer lines; #4 1 seconds ago 1,1 All

You may increment and decrement n times
– [n]CTRL x – Increments the first integer match from the
cursor position → Say n = 10 and number is 13,
10 CTRL x decrements the number by 10,
resulting to number 3
– The increments works the same way
Linux Systems
Visual Editor – vi – Navigation - Forward
1 line Hey all!, let do some vimming :) Press w and observe cursor
~
~
~
1,1 All

1 line Hey all!, let do some vimming :) Press w and observe cursor
~
~
~
1,8 All

1 line Hey all!, let do some vimming :) Press 3w and observe cursor
~
~
~
1,18 All


[n]w – Move forward n words → Say n = 10w – move 10 words
ahead
Linux Systems
Visual Editor – vi – Navigation - Backward
1 line Hey all!, let do some vimming :) Press b and observe cursor
~
~
~
1,18 All

1 line Hey all!, let do some vimming :) Press b and observe cursor
~
~
~
1,15 All

1 line Hey all!, let do some vimming :) Press 2b and observe cursor
~
~
~
1,18 All


[n]b – Move backward n words → Say n = 10b – move 10
words behind
Linux Systems
Visual Editor – vi – Change Word
1 line Hey all!, let do some vimming :) Press cw and observe
~
~
~
1,18 All

1 line all!, let do some vimming :) You may insert the new words
~
~
~
-- INSERT -- 1,8 All

[n]cw – Change n words → Say n = 10cw – change 10 words
from the current cursor position
Linux Systems
Visual Editor – vi – Delete Word
1 line Hey all!, let do some vimming :) Press u to Undo
~
~
~
1 change; before #16 1,18 All

1 line all!, let do some vimming :) Press dw and observe


~
~
~
1,8 All

[n]dw – Delete n words → Say n = 10dw – delete 10 words
from the current cursor position
Linux Systems
Visual Editor – vi – Settings
1 line Hey all!, let do some vimming :)
~
Press : to enter Command Line
Mode
~
~
:

1 line Hey all!, let do some vimming :) Type set hls and ENTER
~
to enable search highlights
~
~
:set hls

:set hls – Enable highlight search

:set nohls – Disable highlight search

:set nu – Enable line numbers

:set nonu – Disable line numbers
Linux Systems
Visual Editor – vi – Search - Forward
1 line Hey all!, let do some vimming :) Press / and type search pattern
~
and ENTER
~
~
/e 1,6 All

1 line Hey all!, let do some vimming :)


~
Press n
~
~
/e 1,9 All

[n]n – Forward search n words → Say n = 10n – Searches the
10 words from the current position forward
Linux Systems
Visual Editor – vi – Search - Backward

1 line Hey all!, let do some vimming :) Press N


~
~
~
/e 1,9 All

1 line Hey all!, let do some vimming :) Press 3N and observe the
~
cursor position
~
~
search hit TOP, continuing at BOTTOM All

[n]n – Reverse search n words → Say n = 10n – Searches the
10 words from the current position backwards
1 line Hey all!, let do some vimming :) Type :set nohls and ENTER
~
to disable search highlights
~
~
:set nohls
Linux Systems
Visual Editor – vi – Settings
1 line Hey all!, let do some vimming :)
~
Press : to enter Command Line
Mode
~
~
:

1 line Hey all!, let do some vimming :) Type set hls and ENTER
~
to enable search highlights
~
~
:set nohls

1 1 line Hey all!, let do some vimming :) Type set nu and ENTER
~
to enable line numbers
~
~
:set nu
Linux Systems
Visual Editor – vi – Substitute

Type yy and 2p
1 1 line Hey all!, let do some vimming :)
~
~
~
:set nu

1 1 line Hey all!, let do some vimming :) Press : to enter Command Line
2 1 line Hey all!, let do some vimming :)
Mode
3 1 line Hey all!, let do some vimming :)
~
:set nu

1 1 line Hey all!, let do some vimming :) Press : to enter Command Line
2 1 line Hey all!, let do some vimming :)
Mode
3 1 line Hey all!, let do some vimming :)
~
:

1 1 line Hey all!, let do some vimming :) Type


2 1 line Hey all!, let do some vimming :) %s/vimming/VIMMING/g
3 1 line Hey all!, let do some vimming :)
~
:%s/vimming/VIMMING/g
Linux Systems
Visual Editor – vi – Substitute
1 1 line Hey all!, let do some VIMMING :)
2 1 line Hey all!, let do some VIMMING :) Substitutes the pattern globally
3 1 line Hey all!, let do some VIMMING :)
~
3 substitutions on 3 lines

1 1 line Hey all!, let do some VIMMING :) Substitutes the pattern on the
2 1 line Hey all!, let do some VIMMING :) Specified line(s)
3 1 line HEY all!, let do some VIMMING :)
~
:3s/Hey/HEY/g

1 1 linEE Hey all!, let do some VIMMING :) Substitutes the first occurrence
2 1 linEE Hey all!, let do some VIMMING :) of pattern globally
3 1 linEE HEY all!, let do some VIMMING :)
~
:%s/e/EE/

Interesting pattern substitutions possible if have knowledge on
Regular Expressions
Linux Systems
Visual Editor – vi – Edit and Read


Edit
– :e filename - open another file without closing the
current
– To switch between these files use CTRL 6. Make sure
the file is save
– You will not be able to undo after the switch

Read
– :r filename - reads file named filename at the current
cursor position
Shell Scripting – Part 1
Linux Systems
Shell Scripting - Programming Languages

There are various types of programming languages,
compared on various parameters

From Embedded system engineer’s view it should be seen
how close or how much away from the hardware the
language is

Based on that view programming languages can be
categorized into three areas:
– Assembly language (ex: 8051)
– Middle level language (ex: C)
– High level / Scripting language (ex: Shell)
Linux Systems
Shell Scripting - Programming Languages

Each programming language offers some benefits with
some shortcomings

Depending on the need of the situation appropriate
language needs to be chosen

This make language selection is a key criteria when it
comes to building real time products!
Linux Systems
Shell Scripting – Prog... Lang... – A Comparison

Language
Assembly C Shell
parameter
Speed High Medium Medium
Portability Low Medium High
Maintainability Low Medium High
Size Low Medium Low
Easy to learn Low Medium High

Shell or any scripting language is also called as ‘interpreted’ language as it doesn’t


go through compilation phase. This is to keep the language simple as the purpose is
different than other languages.
Linux Systems
Shell Scripting – What is a Script?

Any collection of shell commands can be stored in a file,
which is then called as shell script

Programming the scripts is called shell scripting

Scripts have variables and flow control statements like
other programming languages

Shell script are interpreted, not compiled

The shell reads commands from the script line by line and
searches for those commands on the system
Linux Systems
Shell Scripting - Script – Where to use?

System Administration
– Automate tasks
– Repeated tasks

Development
– Allows testing a limited sub-set
– Testing tools

Daily usage
– Simple scripts
– Reminders, e-mails etc…
Linux Systems
Shell Scripting - Script – Example
user@user:~] cd # Move to home directory
user@user:~] mkdir -p ECEP/LinuxSystems/Classwork
user@user:~] cd ECEP/LinuxSystems/Classwork
user@user:Classwork]

user@user:Classwork] vi hello.sh

1 #!/bin/bash
2 echo "Hello World" # Print Hello World on standard output
~
~
:wq

user@user:Classwork] bash hello.sh


Hello World
user@user:Classwork] ls -l
-rw-rw-r-- 1 adil adil 31 Dec 2 21:44 hello.sh
user@user:Classwork] chmod +x hello.sh
user@user:Classwork] ls -l
-rwxrwxr-x 1 adil adil 31 Dec 2 21:44 hello.sh
user@user:Classwork] ./hello.sh
Hello World
user@user:Classwork]
Linux Systems
Shell Scripting - Script – echo

echo displays a line of text
user@user:Classwork] echo Hello World
Hello World
user@user:Classwork]


Helps use to print on screen with required formatting

Used in scripts to print normal messages and the value of
variable, which will be seeing shortly

Some examples are as shown below
user@user:Classwork] echo Hello\nWorld # To print on 2 seperate line
Hello\nWorld
user@user:Classwork] echo -e “Hello\nWorld” # To print on 2 seperate line
Hello
World
user@user:Classwork] echo -e “Hello\tWorld” # To words with tab
Hello World
user@user:Classwork] echo -e “Hello\rWorld” # To overwrite previous print
World
user@user:Classwork]
Linux Systems
Shell Scripting - Special Characters

Characters which has special meaning

Used in many advanced functionalities

Lets understand some of the most important ones
~ → The current user's home directory
user@user:Classwork] echo ~ # Expands to users home directory path
/home/user
user@user:Classwork]

& → Open applications or commands in the background


user@user:Classwork] firefox
⇐ Observe here, the shell would be held by the application you invoked!!

user@user:Classwork] firefox & # firefox is opened in background


[1] 7746
user@user:Classwork] # You may use the command prompt
Linux Systems
Shell Scripting - Special Characters
* → wildcard, matching zero or more characters (e.g. : ls doc_*)
user@user:Classwork] mkdir TBD
user@user:TBD] cd TBD
user@user:TBD] touch file_{1..5}.txt # Create 5 files from 1 to 5
user@user:TBD] touch file_{6..10}.c
user@user:TBD] touch file_{1..5}.sh
user@user:TBD] ls
file_10.c file_13.sh file_1.txt file_4.txt file_7.c
file_11.sh file_14.sh file_2.txt file_5.txt file_8.c
file_12.sh file_15.sh file_3.txt file_6.c file_9.c
user@user:TBD] ls *.txt
file_1.txt file_2.txt file_3.txt file_4.txt file_5.txt
user@user:TBD] ls *1*
file_10.c file_12.sh file_14.sh file_1.txt
file_11.sh file_13.sh file_15.sh
user@user:TBD]
Linux Systems
Shell Scripting - Special Characters
? → wildcard, matching exactly one character (e.g.: ls doc_?)
user@user:TBD] ls
file_10.c file_13.sh file_1.txt file_4.txt file_7.c
file_11.sh file_14.sh file_2.txt file_5.txt file_8.c
file_12.sh file_15.sh file_3.txt file_6.c file_9.c
user@user:TBD] ls file_?.c
file_6.c file_7.c file_8.c file_9.c
user@user:TBD] ls file_??.c
file_10.c
user@user:TBD] ls file_??.*
file_10.c file_11.sh file_12.sh file_13.sh file_14.sh file_15.sh
user@user:TBD]
Linux Systems
Shell Scripting - Special Characters
$ → used to access a variable (e.g. : $HOME), used with echo command
user@user:TBD] echo $ # Note, A $ with any non space character will be seen
as a variable. Will see this in next few slides
$
user@user:TBD] echo $0 # Expands to name of the shell or shell script
/bin/bash
user@user:TBD] echo $$ # Expands to this shell process id
2668
user@user:TBD] echo $? # Expands to the status of the previous command
0
user@user:TBD]


There 2 more which makes sense in a script will see them
shortly
– $@ → Value of all arguments passed
– $# → No of arguments passed to shell script
Linux Systems
Shell Scripting - Variables

Variables are a way of storing information temporarily

A couple of conversions we need to follow
– Variables usually appear in uppercase
– There should not be a white space between the variable
name and the equal sign
user@user:TBD] X=10
user@user:TBD] echo $X # Any non space charater adjacent to $ is a variable
10
user@user:TBD] NAME=”EMERTXE”
user@user:TBD] echo $NAME
EMERTXE
user@user:TBD] echo $DUMMY # The DUMMY variable is not set. Hence no print

user@user:TBD]
Linux Systems
Shell Scripting - White-space & Line-breaks

Bash shell scripts are very sensitive to white-space & line-
breaks

Because the “keywords” of this programming language are
actually commands evaluated by the shell

Need to separate arguments with white spaces

Likewise a line-break in the middle of a command will mislead
the shell into thinking the command is incomplete.
user@user:TBD] TEST = 10 # Not allowed
TEST: command not found
user@user:TBD] TEST1=10; TEST2=20 # Allowed
user@user:TBD] echo $TEST1 $TEST2
10 20
user@user:TBD]
Linux Systems
Shell Scripting - The Shell Env Variables

Login-shell's responsibility is to set the non-login shell and
it will set the environment variables

Environment variables are set for every shell and
generally at login time

Environmental variables are set by the system.

env - lists shell environment variable/value pairs
Linux Systems
Shell Scripting - The Shell Env Variables
user@user:TBD] echo $HOME # The current user's home directory
/home/user
user@user:TBD] echo $SHELL # Shell that will be interpreting user commands
/bin/bash
user@user:TBD] echo $USER # The current logged in user
user
user@user:TBD] echo $PWD # The previous working directory
/home/user/ECEP/LinuxSystems/Classwork/TBD
user@user:TBD] echo $OLDPWD # The previous working directory
/home/user
user@user:TBD] echo $PATH # System will check when looking for commands here
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/
local/games:/snap/bin:
user@user:TBD] echo $HOSTNAME # The hostname of the computer
emertxe
user@user:TBD] echo $TERM # Type of terminal to emulate when running the shell
xterm-256color
user@user:TBD] echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\
033[00m\]:\[\033[01;34m\]\W\[\033[00m\]]
user@user:TBD]
Linux Systems
Shell Scripting - Quotes

Using double quotes to show a string of characters will
allow any variables in the quotes to be resolved
user@user:TBD] VAR=10
user@user:TBD] echo $VAR
10
user@user:TBD] echo “The value is $VAR”
The value is 10
user@user:TBD]


Using single quotes causes the variable name to be used
literally, and no substitution will take place
user@user:TBD] VAR=10
user@user:TBD] echo $VAR
10
user@user:TBD] echo ‘The value is $VAR’
The value is $VAR
user@user:TBD]
Linux Systems
Shell Scripting - Expressions

expr Evaluates simple math on the command line
calculator
user@user:TBD] expr 10 + 20
30
user@user:TBD] expr 10 * 20
expr: syntax error
user@user:TBD] expr 10 \* 20 # * acts a wildcard, so need to escape it
200
user@user:TBD]

bc An arbitrary precision calculator language
user@user:TBD] bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundati
on, Inc. This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'./
10 * 20
200
10 + 34 * 23
792
quit
user@user:TBD]
Linux Systems
Shell Scripting – Expressions - Script
user@user:TBD] cd ../PPT-Examples
user@user:PPT-Examples]

user@user:PPT-Examples] vi 1_expression.sh

1 #!/bin/bash
2
3 NUM1=5
4 NUM2=3
5
6 ADD=$((${NUM1} + ${NUM2}))
7 SUB=$((${NUM1} - ${NUM2}))
8 MUL=$((${NUM1} * ${NUM2}))
9 DIV=$((${NUM1} / ${NUM2}))
10 MOD=$((${NUM1} % ${NUM2}))
11
12 echo -e "Addition of two numbers is\t\t: ${ADD}"
13 echo -e "Substraction of two numbers is\t\t: ${SUB}"
14 echo -e "Multiplication of two numbers is\t: ${MUL}"
15 echo -e "Division of two numbers is\t\t: ${DIV}"
16 echo -e "Modulum of two numbers is\t\t: ${MOD}"
~
"1_expression.sh" 16L, 418C 0,0-1 All
Linux Systems
Shell Scripting – Expressions - Script

user@user:PPT-Examples] chmod +x 1_expression.sh


user@user:PPT-Examples] ./1_expression.sh
Addition of two numbers is : 8
Substraction of two numbers is : 2
Multiplication of two numbers is : 15
Division of two numbers is : 1
Modulum of two numbers is : 2
user@user:PPT-Examples]
Linux Systems
Shell Scripting - Conditions – if else


The if statement chooses between alternatives each of
which may have a complex test

The simplest form is the if-then statement
Syntax

if [ condition ]
then
expression
else
expression
fi
Linux Systems
Shell Scripting - Conditions – if else
user@user:PPT-Examples] vi 2_if_then.sh

1 #!/bin/bash
2
3 NUM1=5
4 NUM2=3
5
6 if [ ${NUM1} -gt ${NUM2} ]
7 then
8 echo "NUM1 is greater than NUM2"
9 else
10 echo "NUM2 is greater than NUM1"
11 fi
~
"2_if_then.sh" 11L, 139C 0,0-1 All

user@user:PPT-Examples] chmod +x 2_if_then.sh


user@user:PPT-Examples] ./2_if_then.sh
NUM1 is greater than NUM2
user@user:PPT-Examples]
Linux Systems
Shell Scripting - Conditions – if else if

Multiple if blocks can be strung together to make an
elaborate set of conditional responses
Syntax
if [ condition_a ]
then
condition_a is true
elif [ condition_b ]
then
condition_b is true
else
both false
fi
Linux Systems
Shell Scripting - Conditions – if else if
user@user:PPT-Examples] vi 3_if_elif.sh

1 #!/bin/bash
2
3 NUM1=5
4 NUM2=3
5
6 if [ ${NUM1} -eq ${NUM2} ]
7 then
8 echo "NUM1 is equal to NUM2"
9 elif [ ${NUM1} -gt ${NUM2} ]
10 then
11 echo "NUM1 is greater than NUM2"
12 else
13 echo "NUM1 is less than NUM2"
11 fi
~
"3_if_elif.sh" 14L, 200C 0,0-1 All

user@user:PPT-Examples] chmod +x 3_if_elif.sh


user@user:PPT-Examples] ./3_if_elif.sh
NUM1 is less than NUM2
user@user:PPT-Examples]
Linux Systems
Shell Scripting - Conditions – case

The case statement compares the value of
the variable ($var in this case) to one or
more values Syntax

Once a match is found, the associated case ${VAR} in
commands are executed and the case value_1)
commands;
statement is terminated ;;

Used to execute statements based on value_2)
commands;
specific values ;;
*)

Often used in place of an if statement if commands;
there are a large number of conditions. ;;
esac

Each set of statements must be ended by a
pair of semicolon

*) is used for not matched with list of
values
Linux Systems
Shell Scripting - Conditions – case
user@user:PPT-Examples] vi 4_case_statments.sh

1 #!/bin/bash
2
3 echo “Enter a number:”
4 read NUM
5
6 case ${NUM} in
7 1)
8 echo "You entered One"
9 ;;
10 2) echo "You entered Two"
11 ;;
12 *) echo "Obey my orders please" ;;
13 esac
~
~
"4_case_statements.sh" 13L, 187C 0,0-1 All
Linux Systems
Shell Scripting - Conditions – case
user@user:PPT-Examples] chmod +x 4_case_statements.sh
user@user:PPT-Examples] ./4_case_statements.sh
“Enter a number < 3:”
2
You entered Two
user@user:PPT-Examples] ./4_case_statements.sh
“Enter a number < 3:”
5
Obey my orders please
user@user:PPT-Examples]
Linux Systems
Shell Scripting – Numeric Test Operators

Operator Functions
-eq Compare if two numbers are equal
-ge Compare if one number is greater than or equal to num
-le Compare if one number is less than or equal to a num
-ne Compare if two numbers are not equal
-lt Compare if one number is less than another number
-gt Compare if one number is greater than another number
Linux Systems
Shell Scripting - String Tests

String comparison, Numeric comparison, File operators
and logical operators

Comparison operations are provided below
Operator Functions
= Compare if two strings are equal
!= Compare if two strings are not equal
-n Evaluate if string length is greater than zero
-z Evaluate if string length is equal to zero
Linux Systems
Shell Scripting - String Tests
user@user:PPT-Examples] vi 5_string_test.sh

1 #!/bin/bash
2
3 echo “Enter the first string”
4 read STR1
5 echo “Enter the second string”
6 read STR2
7
8 if [ -z ${STR1} ]; then
9 echo "First string is empty"
10 else
11 echo "First string is not empty"
12 fi
13 if [ -n ${STR2} ]; then
14 echo "Second string is not empty"
15 else
16 echo "Second string is empty"
17 fi
18 if [ ${STR1} = ${STR2} ]; then
19 echo "Both strings are equal"
20 else
21 echo "Both strings are not equal"
22 fi
~
"5_string_test.sh" 22L, 405C 0,0-1 All
Linux Systems
Shell Scripting - String Tests
user@user:PPT-Examples] chmod +x 5_string_test.sh
user@user:PPT-Examples] ./5_string_test.sh
“Enter the first string”
Hello
“Enter the second string”
World
First string is not empty
Second string is not empty
Both strings are not equal
user@user:PPT-Examples] ./5_string_test.sh
“Enter the first string”
Hello
“Enter the second string”
Hello
First string is not empty
Second string is not empty
Both strings are equal
user@user:PPT-Examples]
Linux Systems
Shell Scripting – Logical Operators

Operator Functions
! Compare if two strings are equal
-a Logically AND two logical expression
-o Logically OR two logical expressions

user@user:PPT-Examples] vi 6_logical_operator.sh

1 #!/bin/bash
2
3 echo “Enter the first number A” ; read A
4 echo “Enter the second number B” ; read B
5 echo “Enter the third number C” ; read C
6
7 if [ ${A} -gt ${B} -a ${A} -gt ${C} ]; then
8 echo "A is the greatest of all"
9 elif [ ${B} -gt ${A} -a ${B} -gt ${C} ]; then
10 echo "B is the greatest of all"
14 elif [ ${C} -gt ${A} -a ${C} -gt ${B} ]; then
15 echo "C is the greatest of all"
16 else
17 echo "Invalid Input"
18 fi
"6_logical_operators.sh" 18L, 426C 0,0-1 All
Linux Systems
Shell Scripting - Logical Operator
user@user:PPT-Examples] chmod +x 6_logical_operator.sh
user@user:PPT-Examples] ./6_logical_operator.sh
“Enter the first number A”
10
“Enter the second number B ”
2
“Enter the third number C ”
3
A is the greatest of all
user@user:PPT-Examples] ./6_logical_operator.sh
“Enter the first number A”
3
“Enter the second number B ”
10
“Enter the third number C ”
2
B is the greatest of all
user@user:PPT-Examples]
File Specific Commands
and Operators
Linux Systems
File Specific Cmds & Opers - more

more helps us to view a file content page wise
user@user:PPT-Examples] more 2_if_then.sh # Use q to quit
#!/bin/bash

NUM1=15
NUM2=6

if [ ${NUM1} -gt ${NUM2} ]


then
echo "NUM1 is greater than NUM2"
else
echo "NUM2 is greater than NUM1"
fi
user@user:PPT-Examples]


Note, if the file size if greater than the window size you
would have page view

You may try the below example
user@user:PPT-Examples] sudo more /var/log/syslog
Linux Systems
File Specific Cmds & Opers - less

less similar to more with many features
user@user:PPT-Examples] less 2_if_then.sh # Use q to quit

#!/bin/bash

NUM1=15
NUM2=6

if [ ${NUM1} -gt ${NUM2} ]


then
echo "NUM1 is greater than NUM2"
else
echo "NUM2 is greater than NUM1"
fi
2_if_then.sh (END)

Note, if the file size if greater than the window size you
would have page view

You may try the below example
user@user:PPT-Examples] sudo less /var/log/syslog
Linux Systems
File Specific Cmds & Opers - | (pipe)

A pipe is a form of redirection that is used in Linux
operating systems to send the output of one program to
another program for further processing.

A pipe is designated in commands by the vertical bar
character
user@user:PPT-Examples] ls
10_cmd_line_args.sh 4_string_test.sh 8_while_loop.sh
1_expressions.sh 5_logical_operators.sh 9_arrays.sh
2_if_then.sh 6_case_statements.sh
3_if_elif.sh 7_for_loop.sh
user@user:PPT-Examples] ls | wc # Gives the word count info from ls output
10 10 166
user@user:PPT-Examples]


It is a very useful operator. Well see some more usage in
later slide
Linux Systems
File Specific Cmds & Opers - head

head helps us to output the first part of files
user@user:PPT-Examples] head -5 1_expressions.sh # Print first 5 lines
#!/bin/bash

NUM1=5
NUM2=3

user@user:PPT-Examples]

user@user:PPT-Examples] ls -1v | head -5 # Print first 5 lines for ls output


1_expressions.sh
2_if_then.sh
3_if_elif.sh
4_string_test.sh
5_logical_operators.sh
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - tail

tail helps us to output the last part of files
user@user:PPT-Examples] tail -5 1_expressions.sh # Print last 5 lines
echo -e "Addition of two numbers is\t\t: ${ADD}"
echo -e "Substraction of two numbers is\t\t: ${SUB}"
echo -e "Multiplication of two numbers is\t: ${MUL}"
echo -e "Division of two numbers is\t\t: ${DIV}"
echo -e "Modulum of two numbers is\t\t: ${MOD}"
user@user:PPT-Examples]

user@user:PPT-Examples] ls -1v | tail -5 # Print first 5 lines for ls output


7_for_loop.sh
7_while_loop.sh
6_case_statements.sh
9_arrays.sh
10_cmd_line_args.sh
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - Redirection

Operators used to redirect data
– From a file to a command – Input Redirection → <
– To a file from the command – Output Redirection → >

We may control the data stream which passed to the file
– 1> → from stdout to file
– 2> → from stdin file

Data can be appended to an existing file
>>


The functions discussed here basic

Many advanced functions are available if required
Linux Systems
File Specific Cmds & Opers - Redirection
user@user:PPT-Examples] ls
10_cmd_line_args.sh 4_string_test.sh 8_while_loop.sh
1_expressions.sh 5_logical_operators.sh 9_arrays.sh
2_if_then.sh 6_case_statements.sh
3_if_elif.sh 7_for_loop.shh
user@user:PPT-Examples] echo Hello > test.txt
user@user:PPT-Examples] ls
10_cmd_line_args.sh 4_string_test.sh 8_while_loop.sh
1_expressions.sh 5_logical_operators.sh 9_arrays.sh
2_if_then.sh 6_case_statements.sh test.txt
3_if_elif.sh 7_for_loop.sh
user@user:PPT-Examples] cat text.txt
Hello
user@user:PPT-Examples] echo World > test.txt # Overwrites the files
user@user:PPT-Examples] cat text.txt
World
user@user:PPT-Examples] echo Hello >> test.txt
user@user:PPT-Examples] cat text.txt
World
Hello
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - Redirection
user@user:PPT-Examples] ls
10_cmd_line_args.sh 4_string_test.sh 8_while_loop.sh
1_expressions.sh 5_logical_operators.sh 9_arrays.sh
2_if_then.sh 6_case_statements.sh test.txt
3_if_elif.sh 7_for_loop.sh
user@user:PPT-Examples] ls 9_arrays.sh 1234 > test.txt
ls: cannot access '1234': No such file or directory
user@user:PPT-Examples] cat text.txt
9_arrays.sh
user@user:PPT-Examples] ls 9_arrays.sh 1234 2> test.txt
9_arrays.sh
user@user:PPT-Examples] cat text.txt
ls: cannot access '1234': No such file or directory
user@user:PPT-Examples] echo 1 + 2 > text.txt
user@user:PPT-Examples] bc < test.txt # The file contents is passed as input
3
user@user:PPT-Examples] rm text.txt # Not needed anymore
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - du

du helps us to estimate file space usage
user@user:PPT-Examples] du
44 .
user@user:PPT-Examples] du -h # Human readable
44K .
user@user:PPT-Examples] du -sb * # Size in bytes
301 10_cmd_line_args.sh
416 1_expressions.sh
139 2_if_then.sh
200 3_if_elif.sh
405 4_string_test.sh
427 5_logical_operators.sh
187 6_case_statements.sh
131 7_for_loop.sh
103 8_while_loop.sh
436 9_arrays.sh
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - df

df helps us to fine system disk space usage
user@user:PPT-Examples] df
tmpfs 803976 9520 794456 2% /run
/dev/sda1 76765204 9557568 63285052 14% /
tmpfs 4019868 8484 4011384 1% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 4019868 0 4019868 0% /sys/fs/cgroup
/dev/sda6 519987072 71655932 421847532 15% /home
tmpfs 803976 60 803916 1% /run/user/1001
user@user:PPT-Examples] df -h # Human readable
udev 3.9G 0 3.9G 0% /dev
tmpfs 786M 9.3M 776M 2% /run
/dev/sda1 74G 9.2G 61G 14% /
tmpfs 3.9G 8.3M 3.9G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda6 496G 69G 403G 15% /home
tmpfs 786M 60K 786M 1% /run/user/1001
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - df

df helps us to find system disk space usage
user@user:PPT-Examples] df # Note the output depends on the installation
tmpfs 803976 9520 794456 2% /run
/dev/sda1 76765204 9557568 63285052 14% /
tmpfs 4019868 8484 4011384 1% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 4019868 0 4019868 0% /sys/fs/cgroup
/dev/sda6 519987072 71655932 421847532 15% /home
tmpfs 803976 60 803916 1% /run/user/1001
user@user:PPT-Examples] df -h # Human readable
udev 3.9G 0 3.9G 0% /dev
tmpfs 786M 9.3M 776M 2% /run
/dev/sda1 74G 9.2G 61G 14% /
tmpfs 3.9G 8.3M 3.9G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda6 496G 69G 403G 15% /home
tmpfs 786M 60K 786M 1% /run/user/1001
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - stat

stat helps us to display file or file system status
user@user:PPT-Examples] stat 1_expression.sh
File: '1_expressions.sh'
Size: 416 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 23600973 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1001/ adil_sk) Gid: ( 1001/ adil_sk)
Access: 2018-12-03 16:24:18.489886098 +0530
Modify: 2018-12-03 14:24:00.896270594 +0530
Change: 2018-12-03 14:29:10.811075929 +0530
Birth: -
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers - ln

ln helps us to make links between file

Two types of links are possible
– Hard link → only on files

The link file has the same inode number

Doesn’t matter if the original file get deleted

It a default link created
– Soft link → both on files and directories

The link file has the different inode number

If the original file is deleted the link get broken

Created with -s option
Linux Systems
File Specific Cmds & Opers – ln – Hard link
user@user:PPT-Examples] echo Hello > test.txt
user@user:PPT-Examples] cat test.txt
Hello
user@user:PPT-Examples] ln test.txt hard_link.txt # Default is hardlink
user@user:PPT-Examples] cat hard_link.txt
Hello
user@user:PPT-Examples] echo World >> hard_link.txt
user@user:PPT-Examples] cat text.txt
Hello
World
user@user:PPT-Examples] stat text.txt | head -3
File: 'text.txt'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 23600979 Links: 2
user@user:PPT-Examples] stat hard_link.txt | head -3
File: 'hard_link.txt'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 23600979 Links: 2
user@user:PPT-Examples] rm text.txt # Doesn’t effect the hard link!!
user@user:PPT-Examples] cat hard_link.txt
Hello
World
user@user:PPT-Examples]
Linux Systems
File Specific Cmds & Opers – ln – Soft link
user@user:PPT-Examples] echo Hello > test.txt
user@user:PPT-Examples] ln -s test.txt soft_link.txt
user@user:PPT-Examples] ls -l soft_link.txt
lrwxrwxrwx 1 user user 8 Dec 4 15:01 soft_link.txt -> test.txt
user@user:PPT-Examples] cat soft_link.txt
Hello
user@user:PPT-Examples] echo World >> soft_link.txt
user@user:PPT-Examples] cat text.txt
Hello
World
user@user:PPT-Examples] stat text.txt | head -3
File: 'text.txt'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 23601012 Links: 1
user@user:PPT-Examples] stat hard_link.txt | head -3
File: 'soft_link.txt'
Size: 8 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 23600982 Links: 1
user@user:PPT-Examples] rm text.txt # This effects the soft link!!
user@user:PPT-Examples] cat soft_link.txt
cat: soft_link.txt: No such file or directory
user@user:PPT-Examples] ls -l soft_link.txt # Broken link!!
lrwxrwxrwx 1 user user 8 Dec 4 15:01 soft_link.txt -> test.txt
user@user:PPT-Examples]
Shell Scripting – Part 2
Linux Systems
Shell Scripting - Loops – for

The structure is a looping structure. Used to execute a
set of commands while the provided list is empty

The loop terminates as soon as the all the elements in
the list is evaluated

Can be used in multiple methods, an example for fixed
iteration is show below
Syntax
for i in list
do
Code Block
done
Linux Systems
Shell Scripting - Loops – for
user@user:PPT-Examples] vi 7_for_loop.sh

1 #!/bin/bash
2
3 for i in 1 2 3 4 5
4 do
5 echo "Loop counter is ${i}"
6 done
~
"2_for_loop.sh" 6L, 70C 0,0-1 All

user@user:PPT-Examples] chmod +x 7_for_loop.sh


user@user:PPT-Examples] ./7_for_loop.sh
Loop counter is 1
Loop counter is 2
Loop counter is 3
Loop counter is 4
Loop counter is 5
user@user:PPT-Examples]
Linux Systems
Shell Scripting - Loops – while

The structure is a looping structure. Used to execute a
set of commands while a specified condition is true

The loop terminates as soon as the condition becomes
false. If condition never becomes false, loop will never
exit

Any valid conditional expression will work in the while
loop.

Syntax
while [ condition ]
do
Code Block
done
Linux Systems
Shell Scripting - Loops – for
user@user:PPT-Examples] vi 8_while_loop.sh

1 #!/bin/bash
2
3 LOOP=1
4
5 while [ ${LOOP} -le 5 ]
6 do
7 echo "Looping : ${LOOP}"
8 LOOP=$((${LOOP} + 1))
9 done
~
"8_while_loop.sh" 9L, 103C 0,0-1 All

user@user:PPT-Examples] chmod +x 8_while_loop.sh


user@user:PPT-Examples] ./8_while_loop.sh
Looping : 1
Looping : 2
Looping : 3
Looping : 4
Looping : 5
user@user:PPT-Examples]
Linux Systems
Shell Scripting - Arrays

An array is a variable containing multiple values may be
of same type or of different type

There is no maximum limit to the size of an array

Array index starts with zero
Linux Systems
Shell Scripting - Arrays
user@user:PPT-Examples] vi 9_arrays.sh

1 #!/bin/bash
2
3 echo "Number of elements in the array: ${#LINUX_DISTROS[@]}"
4 echo "Printing elements of array in one shot: ${LINUX_DISTROS[@]}"
5 echo "Printing elements of array in one shot: ${LINUX_DISTROS[*]}"
6 echo "Printing elements of array in using a loop:"
7 for ((i = 0; i < ${#LINUX_DISTROS[@]}; i++))
8 do
9 echo ${LINUX_DISTROS[$i]}
10 done
~
"9_arrays.sh" 11L, 146C 0,0-1 All

user@user:PPT-Examples] chmod +x 9_arrays.sh


user@user:PPT-Examples] ./9_arrays.sh
Number of elements in the array: 5
Printing elements of array in one shot : Debian Redhat Ubuntu Suse Fedora
Printing elements of array in one shot : Debian Redhat Ubuntu Suse Fedora
Printing elements of array in using a loop : Debian Redhat Ubuntu Suse Fedora
user@user:PPT-Examples]
Linux Systems
Shell Scripting – Command Line Arguments

Shell script can accept command-line arguments & options
just like other Linux commands

Within your shell script, you can refer to these arguments as
$1,$2,$3,.. & so on.

Then the command line arguments are executed like

Read all command line arguments and print them
Linux Systems
Shell Scripting - Command Line Arguments
user@user:PPT-Examples] vi 10_cmd_line_args.sh

1 #!/bin/bash
2
3 if [ $# != 2 ]
4 then
5 echo "Usage: Pass 3 arguments"
6 exit 0
7 fi
8
9 echo "The arguments of the script you passed are:"
10 echo "Total number of arguments you passed are : $#"
11 echo "The name of the script is : $0"
12 echo "The first argument is : $1"
13 echo "The second argument is : $2"
~
"10_cmd_line_args.sh" 13L, 301C 0,0-1 All
Linux Systems
Shell Scripting - Command Line Arguments
user@user:PPT-Examples] chmod +x 10_cmd_line_args.sh
user@user:PPT-Examples] ./10_cmd_line_args.sh Hello 1234
The arguments of the script you passed are:
Total number of arguments you passed are : 2
The name of the script is : ./10_cmd_line_args.sh
The first argument is : Hello
The second argument is : 1234
user@user:PPT-Examples]
Linux Systems
Shell Scripting - Functions

Writing functions can greatly simplify a program

Improves modularity, readability and maintainability

However speed will get slowed down

Arguments are accessed as $1, $2, $3…
Syntax
function name()
{
<command>
<statments>
<expression>
}
Linux Systems
Shell Scripting - Functions
user@user:PPT-Examples] vi 11_functions.sh

1 #!/bin/bash
2
3 function sum()
4 {
5 x=`expr $1 + $2`
6 echo $x
7 }
8
9 y=`sum 5 3`
10 echo "The sum is 5 and 3 is $y"
11 echo "The sum is 6 and 2 is `sum 6 2`"
~
"11_functions.sh" 11L, 146C 0,0-1 All

user@user:PPT-Examples] chmod +x 11_functions.sh


user@user:PPT-Examples] ./11_functions.sh
The sum is 5 and 3 is 8
The sum is 6 and 2 is 8
user@user:PPT-Examples]
Stay Connected
About us: Emertxe is India’s one of the top IT finishing schools & self learning kits provider. Our primary
focus is on Embedded with diversification focus on Java, Oracle and Android areas

Emertxe Information Technologies,


No-1, 9th Cross, 5th Main,
Jayamahal Extension,
Bangalore, Karnataka 560046
T: +91 80 6562 9666
E: [email protected]

https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides


Thank You

You might also like