Linux Systems Slides Part1
Linux Systems Slides Part1
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
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
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
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
~
"test.txt" [New File] 0,0-1 All
Press i
~
~
-- INSERT -- 0,1 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 ESC
Hey all!, let do some vimming :THE TEXT GETS INSERTED HERE)
~
^[ 1,59 All
Press u to Undo
Press ESC
THE TEXT GETS INSERTED HEREHey all!, let do some vimming :)
~
1,27 All
Press u to Undo
●
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
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 :)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
Press o
Hey all!, let do some vimming :)
Hey all!, let do some vimming :)
~
-- INSERT -- 3,1 All
Press O
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
●
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
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 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 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 :) 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
user@user:Classwork] vi hello.sh
1 #!/bin/bash
2 echo "Hello World" # Print Hello World on standard output
~
~
:wq
●
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]
●
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
●
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
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
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
●
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
●
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]
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
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
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
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