Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views169 pages

Linux OS Day 1 & 2

Uploaded by

geethas1141999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views169 pages

Linux OS Day 1 & 2

Uploaded by

geethas1141999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 169

Linux Operating System

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 1


Purushotham BV
 BE – CSE from BLR University – 1995
 MS – From BITS Pilani – 1999
 Worked as lecturer / Asst. Prof / HOD – various engg college in BLR
 Associated with Adobe – Tech Consultant
 Oman & Libya – teaching in Computer related courses for Colleges
and Universities
 Freelance Technical Trainer
 2012 to 2017 – Linux, Shell Programming, DB, Java, Web
 2018 joined MC as Technical Training Head

2
Agenda
Let me chk with the IT ppl

Introduction to Linux OS
Commands- basic and advanced
vi editor
Shell Scripting
DB Connection to MySQL
Summary

3
Introduction to UNIX OS Architecture
An Operating System (OS) is a software component of a
computer system, which:
 Shares the limited resources of the computer
 Coordinates and manages the various activities
 Offers a number of services to application programs and users
 Acts as a host for Applications that are run on the machine
Monitor Applications

Operating
Printer System
Disk
Drive

Keyboard
4
General Functions of O/S
 Opening files
 Reading files
 Closing files
 Creating files and/or directories
 Copying or deleting files and directories
 Command Interpretation
 Device Management
 Memory management
 Network communication
 Reading and writing to devices
 Allowing or limiting access to system resources
 Monitoring/Killing processes
5
Types of Operating System
Based on O/S Performance Based on User Performance

 Command User Interface[CUI]  Single User Interface[SUI] - DOS


 Graphical User Interface [GUI]  Multi User Interface[MUI] – Win / Linux

6
Features of UNIX OS

 Multi-user, Multi-tasking, Time sharing system


 File structure
 Modularity
 Input/Output Redirection and Pipes
 Consistent Peripheral Interface
 Security
 Communication and Networking
 UNIX Utilities and Software Development Tools

7
UNIX Architecture
Everything below the It provides file
system call interface system, CPU
and above the physical scheduling, memory
hardware is the management, and
KERNEL. other OS functions
using system calls.

The system call


interface layer converts System call provides a
a process running in programming interface
user mode to a that allows user programs
protected kernel mode to access kernel
process. functions.

8
User Application and Kernel Interaction

9
UNIX Architecture (Contd.,)
Major components of UNIX are:
 Kernel
– Monitors and controls the resources of a computer
and allocates them among its users in an optimal
manner
 Shell
– Provides a processing environment for the user
programs and acts like a command translator
 Utilities
– Programs which are used for development purposes
 User Applications
– Programs written by the user
10
UNIX File System
What is a file?
 A collection of data items stored on a disk

What is a file system?


 A group of files and relevant information regarding them
 Each file system is stored in a separate whole disk partition
 UNIX supports variety of files
– Ordinary file
– Directory file
/
– Special files
etc bin tmp users src
– Character special files
webmin usb tdlibuser User 1 User 2
– Block special files

11
History of UNIX
Year UNIX Variant Features
1957 BESYS • At Bell Labs
• To run batch jobs
1965 MULTICS (Multiplexed Information • Adopted third generation computer equipments
and Computing Service)

1969 UNICS (UNiplexed Information and • To play space travel on another smaller machine (DEC PDP-7)
Computing Service)

1971 UNIX • The first edition of the "UNIX PROGRAMMER'S MANUAL”


– By K. Thompson [and] D. M. Ritchie; included over 60
commands

1977 BSD • Developed at the Computer System Research Group (CSRG)


(Berkeley Software Distribution)
1986 AIX • Developed By IBM in 1990
(Advanced Interactive eXecutive) • Shells available (Korn, Bourne, C)
• Default Korn shell
1987 MINIX • A free UNIX clone written from scratch
• Small size
• Micro kernel-based design and simple documentation
• Suited for personal computer

1992 Sun Solaris • Sun company's UNIX variant operating system

12
UNIX Flavors

Originator Proprietary Name


U. Cal. Berkley BSD
Sun Solaris
IBM AIX
HP HP - UX
SGI IRIX
Digital Equipment2 DEC UNIX
Compaq Tru64 UNIX
Apple MacOS X
L. Torvalds/GNU Linux

13
What is Linux ?

 A fully-networked 32/64-Bit Unix-like Operating System


 Unix Tools Like sed, awk, and grep (explained later)
 Compilers Like C, C++, Fortran, Smalltalk, Ada
 Network Tools Like telnet, ftp, ping, traceroute
 Multi-user, Multitasking, Multiprocessor
 Has the X Windows GUI
 Coexists with other Operating Systems
 Runs on multiple platforms
 Includes the Source Code

Comviva Internal
14
LINUX
Developed by Linus Torvalds
Placed under General Public License
Variants
 Caldera Linux
 Debian Linux
 Kondara Linux
 Red Hat Linux
 Mandrake Linux
 SuSE Linux
 Fedora
 Ubuntu

15
Basic Commands

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 16


Packaging System – rpm file

RPM (Red Hat Package Manager)


The standard Linux package format (according to
the Linux Standard Base) is RPM.
RPM is a packaging system originally developed
by Red Hat and widely used in the Linux
community.
Distributions using it include Fedora, Mandriva,
Red Hat (naturally), and SUSE.
An RPM package file normally will be named
something like program-version-other.rpm 17
yum
 yum does for RPM packages roughly what apt-get does for Debian
packages.
 yum can be download and install packages from a configured
repository.
 yum install ${packagename}
 To remove software is just as easy.
 yum remove ${packagename}
 yum does not keep a local copy of your package database by default,
so normally there is no need to update it. To install all available
security patches and bug fixes, use this command:
 yum update
 You can also explicitly update a single package with:
 yum update ${packagename}

18
Linux Boot Process (Startup Sequence)

19
Boot Process - BIOS
 BIOS stands for Basic Input/Output System
 Performs some system integrity checks
 Searches, loads, and executes the boot loader program.
 It looks for boot loader in floppy, cd-rom, or hard drive. You can press
a key (typically F12 of F2, but it depends on your system) during the
BIOS startup to change the boot sequence.
 Once the boot loader program is detected and loaded into the
memory, BIOS gives the control to it.
 So, in simple terms BIOS loads and executes the MBR boot loader.

20
Boot Process - MBR
 MBR stands for Master Boot Record.
 It is located in the 1st sector of the bootable disk. Typically /dev/hda,
or /dev/sda
 MBR is less than 512 bytes in size. This has three components 1)
primary boot loader info in 1st 446 bytes 2) partition table info in next
64 bytes 3) mbr validation check in last 2 bytes.
 It contains information about GRUB (or LILO in old systems).
 So, in simple terms MBR loads and executes the GRUB boot loader.

21
Boot Process - GRUB
 RUB stands for Grand Unified Bootloader.
 If you have multiple kernel images installed on your system, you can
choose which one to be executed.
 GRUB displays a splash screen, waits for few seconds, if you don’t enter
anything, it loads the default kernel image as specified in the grub
configuration file.
 GRUB has the knowledge of the filesystem (the older Linux loader LILO
didn’t understand filesystem).
 Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to
this)
 In simple terms GRUB just loads and executes Kernel and initrd images.

22
Boot Process - Kernel
 Mounts the root file system as specified in the “root=” in grub.conf
 Kernel executes the /sbin/init program
 Since init was the 1st program to be executed by Linux Kernel, it has the
process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
 initrd stands for Initial RAM Disk.
 initrd is used by kernel as temporary root file system until kernel is
booted and the real root file system is mounted. It also contains
necessary drivers compiled inside, which helps it to access the hard
drive partitions, and other hardware.

23
Boot Process - Init
 Looks at the /etc/inittab file to decide the Linux run level.
 Following are the available run levels
 0 – halt
 1 – Single user mode
 2 – Multiuser, without NFS
 3 – Full multiuser mode
 4 – unused
 5 – X11
 6 – reboot

 Init identifies the default initlevel from /etc/inittab and uses that to load all
appropriate program.
 Execute ‘grep initdefault /etc/inittab’ on your system to identify the default
run level
 If you want to get into trouble, you can set the default run level to 0 or 6.
Since you know what 0 and 6 means, probably you might not do that.
 Typically you would set the default run level to either 3 or 5. 24
Boot Process - Init
 When the Linux system is booting up, you might see various services getting started. For
example, it might say “starting sendmail …. OK”.
 Those are the runlevel programs, executed from the run level directory as defined by your run
level.
 Depending on your default init level setting, the system will execute the programs from one of
the following directories.
 Run level 0 – /etc/rc.d/rc0.d/
 Run level 1 – /etc/rc.d/rc1.d/
 Run level 2 – /etc/rc.d/rc2.d/
 Run level 3 – /etc/rc.d/rc3.d/
 Run level 4 – /etc/rc.d/rc4.d/
 Run level 5 – /etc/rc.d/rc5.d/
 Run level 6 – /etc/rc.d/rc6.d/
 Please note that there are also symbolic links available for these directory under /etc directly.
So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
 Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
 Programs starts with S are used during startup. S for startup.
 Programs starts with K are used during shutdown. K for kill. 25
Types of Commands

Internal
 Built-in into the shell
 No separate process invoked
External
 Executable files
 Stored in known locations in the file system
Shell scripts
 Interpreted by the shell

26
Some Simple Commands
 ls = list the files and directory
 date = display date and time
 cal = display calendar
 clear = clear the screen
 pwd = prints the name of current working directory
 mkdir = creates the directory
 rmdir = removes the directory
 cd = changes the directory
 cat = display the contents of file
 rm = removes the files
 cp = copy the files and directory
 mv = move and rename ( files and directories )
 touch = updates the file/directory time-stamps
 less = pager command
 more = pager command

Comviva Internal
27
UNIX Command Syntax
 General form
 Commandname [argument]… [<inputfile] [>outputfile]
 Commandname
 The name of the command comes first

 Argument
 If there are any arguments to the command, they follow the
command name

 Inputfile
 If there is an input redirection symbol “<“, input comes form
inputfile, otherwise it comes form the keyboard

 Outputfile
 If there is an output redirection symbol “>“, output is sent to
outputfile, otherwise to the screen 28
File Metacharacters

 ls -l; ls -l .bash*
 ls -la or ls -l -a
 ls -la --ignore=.b*
 It is also possible to give more than one command:
 Coammdnd1 | command2 | command3 | … | commandn
– ls -l ; date ; echo "Hello“

 ? - Matches a single character.


 * - Matches one or more characters
 [abc] - Matches a single character – either a, b or c.
 [!abc] - Matches a single character – which is not a, b or c.
 [a-c] – Matches a single character which is within the range of a and c
29
Interpretation of ls – l
 % ls -l then the following information might be displayed:
 -rw-r--r-- 4 a1a1 guest 9512 May 10 15:21 banner.gif
 drwx------ 1 a1a1 guest 512 May 13 09:59 document
 -rw-r--r-- 4 a1a1 guest 512 May 19 15:27 homepage.html
 drwx------ 1 a1a1 guest 25194 Jun 16 09:59 temp
 The meaning of the information contained in each column:
 Column 1: The first character is either d indicating a directory, - indicating a file. Next
we have access permissions such as -rw-r--r--. These specify which users have
permission to read (r), write (w) or execute (x) the file. More information about
permissions, groups and owners can be found in the section about understanding
permissions.
 Column 2: The number of hard links to the file.
 Column 3: The owner of the file (e.g. a1a1).
 Column 4: The name of the group the file belongs to (groups are collections of users,
e.g. guest).
 Column 5: The size of the file in characters (bytes).
 Column 6: The date and time of the most recent change to the contents of the file (e.g.
May 10 15:21).
 Column 7: The name of the file or directory (e.g. banner.gif or document). 30
man Utility

 Known as man pages


 You can ask for any thing!

31
Basic File Manipulation Commands
 ls – list contents of directory
 Commonly used switches
 l – Long listing
 a – All
 d – List directory contents
 R – Recursive
 touch – update file modification time
 Create file if it does not exist
 cat – display contents of a file
 touch - change file timestamps
 rm – Remove file
 Switch –i or interactive highly recommended
 cp – Copy file
 mv – Move file
 Try mv .b* .c* 32
Basic Commands
 which
 $ which ls
 ls  whereis
 $ ls -l  $ whereis ls
 $ ls -a
 $ ls -la  locate
  $ locate stdio.h
$ ls -l --sort=time  $ locate iostream
 $ ls -l --sort=size -r
 cd  rpm
 $ cd /usr/bin  $ rpm -q bash
 $ rpm -qa
 pwd  $ rpm -qa | sort | less
 $ pwd
 ~  find
 $ find / | grep stdio.h
 $ cd ~  $ find /usr/include | grep stdio.h
 ~user
 $ cd ~weesan
 What will “cd ~/weesan” do?

Comviva Internal
33
Basic Commands (cont)

 rm
 $ rm foo
 echo  $ rm -rf foo
 $ echo “Hello World”
 $ echo -n “Hello World”  $ rm -i foo
chgrp
 cat  $ chgrp bar /home/foo
 $ cat /etc/motd  chgrp im /root/out
 $ cat /proc/cpuinfo
 chsh
 cp  $ chsh foo
 $ cp foo bar  chfn
 mv  $ chfn foo
 $ mv foo bar  chsh im
 mkdir  chown
 $ mkdir foo  $ chown -R foo:bar /home/foo

Comviva Internal
34
Basic Commands (cont)

 Pipe
 tar  $ cal > foo
 $ tar cvfp lab1.tar lab1  $ cat /dev/zero > foo
 gzip  $ cat < /etc/passwd
 $ gzip -9 lab1.tar  $ who
 untar & ungzip  backtick
 $ gzip -cd lab1.tar.gz | tar xvf –  $ echo “The date is `date`”
 $ tar xvfz lab1.tar.gz  $ echo `seq 1 10`
 touch  Hard, soft (symbolic) link
 $ touch foo  ln vmlinuz-2.6.24.4 vmlinuz
 $ cat /dev/null > foo  ln -s firefox-2.0.0.3 firefox

Comviva Internal
35
Alias Command
Alias  Using this command we can create alternate name of a exists
unix command
Syntax:
alias Altenatename=‘Unix Command’
Ex:
alias MLA=‘cat’
MLA>file123
UnaliasTo Cancel the Alternate name of the Unix Command
Syntax:
unalias alternatename
ex:
unalias MLA

36
Structure of a UNIX File System(UFS)

 3 blocks
 Boot block
 Super block
 Inode list (ilist)
 Data blocks

 Boot Block
 The first block of every file system (block 0)
 Contains bootstrap code for system
 Boot block empty for all except root file system

37
Super Block
 Contains data about file system
 Fields are
– The size of the file system

– The number of free blocks

– A list of free blocks

– The index of the next free block

– The size of the inode list

– The number of free inodes

– The list of free inodes

– The index of first free inode

– Lock fields for free block and free inode list

– Sanity flag – Indicates whether the super block has been modified since the
last disk flush

 http://www.angelfire.com/myband/binusoman/Unix.html
38
Disk Drive

39
Detailed File system model

 inode entry of a file contains Addresses, pointers and pointers to


pointers for data blocks

40
Inode List
 Disk array of inodes
 Fields in individual inode (entries)
 uid of owner
 gid of “group” owner
 Type of file
 Number of links
 Permissions
 Access time
 Modified time
 Inode change time
 Size
 Disk addresses
– Direct and indirect addresses
41
I-Node

42
Links
 Alternate access paths to a file
 Two types
 Hard Links (ln src dst)
 Symbolic Links (ln –s src dst)
 Hard Links
 Two directory entries with the same inode number
 Can be across directories
 Must be within the same file system
 Link count field incremented when new link created
 File deleted only when link count reaches zero
 Directory also have a link with parent
 Symbolic Links
 Use the ln -s command
 Creates another inode
 Contents of file are link to source
 When source deleted, access through link fails
43
Hard Link v/s Soft Link
 Hard Link is a mirror copy of the original file. Hard links share the same
inode. Disk usage is same.
 Any changes made to the original or Hard linked file will reflect the other.
 Even if you delete any one of the files, nothing will happen to the other.
 Hard links can't cross file systems.
 Soft Link is a symbolic link to the original file. Soft Links will have a different
inode value. Disk is increased.
 A soft link points to the original file. If you delete the original file, the soft link
fails.
 If you delete the soft link, nothing will happen.
 Soft links can cross file systems.

 Demo
 How many links are possible for a directory containing 3 sub-directories? 44
File Compression

 Results in smaller file size


 Text files can be compressed over 75%
 tar archives are often compressed

Comviva Internal
45
Create New Archive
The tar command
Stands for tape archive
Writes archives directly to tape devices
Creates new archives
Lists files in existing archives
Extracts files from archives
Syntax
tar [options] [directory file]
Common Options
c create an archive (begin writting at the start of the file)
t table of contents list
x extract from an archive
v verbose
46 46
Extract Archive File

Command Function
$ tar -cvf archive.tar TestF1 TestF2 • Creates archive file archive. tar for files TestF1 and
TestF2
• Additionally, use of –v (verbose) option displays
the progress while tar works.
• To create archive, we need to specify the name of
the archive (with –f), the copy, or write operation (-
c), and the filenames.

Command Function
$ tar -xvf archive.tar tar uses –x option to extract files from an archive

47 47
Compress and Expand Compressed File

 The gzip and gunzip command


 gzip – compresses the file
 gunzip – expands compressed file

Syntax:
gzip Filename
Ex: gzip file1

48 48
Compression/Decompression
Commands
Gunzip  TO Unzip a file
Syntax:
gunzip filename.gz

– $ gunzip TestGipTry.gz

49
Compression Utilities

 gzip, gunzip

 Standard Linux compression utility


 Up to 75% compression for text files

 bzip2, bunzip2

 Newer Linux compression utility


 Generally achieves better compression than gzip

Comviva Internal
50
Using Compression

 gzip somefile
( To create compressed file )
 gunzip somefile
( To uncompress a compressed file )
 bzip2 somefile
( To create compressed file )
 bunzip2 somefile
( To uncompress a compressed file )

Note: - We can use z for gzip and j for bzip2 type of compression with tar

Comviva Internal
51
Basic Commands (cont)

 Disk usage
 $ df -h /
 File space usage
 $ du -sxh ~/
 Advance stuff 
 $ ssh eon who
 $ ssh eon ‘cd .html ; tar cvfp - cs183 | gzip -9c’ | tar
xvfpz -
 $ ssh kilo-1 ‘tar cvfp - /extra/weesan’ | tar xvfp - -C /
Comviva Internal
52
Absolute and Relative Pathnames
Directory Related Commands
 The Absolute Path.
 The entire pathname starting from root(/).
 Example: /home/training/charigs
 The Relative Path.
 The path relative to your present working directory.
 Example: training/charigs

 . Denotes the present directory


 .. Denotes the parent directory
 pwd – Display current (present working directory) directory
 cd – Change current directory
 mkdir – Create (make) directory
 rmdir – Remove directory
 Directory must be empty
53
File Security
 Every person has a unique userid
 Userid is alphanumeric
 Internally numeric userid is used
 Mapped via /etc/passwd

 Every user belongs to a group


 External representation is alphanumeric; internal numeric
 Mapped via /etc/passwd

 Triad of permissions
 User - User matching owner of file
 Group - Users whose group is the same as the group of the file,
Other members of the group
 Others - Users who are not the owner nor belong to the group

54
Triad Permissions
 Triad of permissions for each set
 r – Read permission
 w – Write permission
 x – Execute permission
 Yielding 9 bits
– 0 indicates no access

– 1 indicates access

 chmod – change file permissions


 Absolute mode
– Three digit octal number

 Symbolic mode Owner Group Others


– +x – execute
r w x r w x r w x
– +r – read
4 2 1 4 2 1 4 2 1
– + w – write

– ugo – user group others


1 1 1 1 1 1 1 1 1
55
File Permission – Symbolic Mode
Uses characters & arithmetic operators for
mentioning the permissions
 Operators
– + To add permissions
– - To remove permissions
– = To assign permission
 Symbols
– r - read , w - write , x - execute
– u - owner , g - group , o - others, a - all users
 Example
– u+rx – Indicates the user has been granted read
and execute permissions 56
Other commands

 chown
 Change ownership of file
 chgrp
 Change group

57
Directory Security

The rwx has slightly different permissions for


directories
 r permission means that the files in the directory can be
read
 w means that files in the directory can be modified
 x means that the directory can be navigated (no effect)

58
Redirection : the 3 standard files
 Standard Input
 file / stream represents i/p, connected to keyboard
 < or pipleline
 wc, cat
 Standard Output
 file / stream represents o/p, connected to display
 wc sample.txt > newfile, cat newfile
 Standard Error
 file / stream represents error messages originating from command / shell also connected to display
 0 – standard input
 1 – standard output
 2 – standard error
 Cannot be redirected to a file
 cat foo (cat: cannot open foo) so use cat foo 2> errorfile then cat errorfile
 Filters using both standard input and output
 Directory commands neither i/p nor o/p
 ls,pwd,who etc., take i/p and produce o/p
 lp uses i/p but no o/p
 cat, wc, od, cmp, gzip etc., use both i/p and o/p

59
Devices and Terminals

 Device special files represent physical devices, pseudodevices, and named


pipes, are stored in the /dev directory and create a link between the system and
the device drivers.
 There must be a device special file for each physical device such as disk, tape,
printer, terminal or pseudodevice.
 A pseudodevice is a network interface, a named pipe, or a UNIX domain socket.
 The driver handles all read and write operations and follows the required
protocols for the device.

60
Devices and Terminals (Contd.,)
 There are three types of device files:
 Block device files are used for devices performing I/O buffering such as disks like rz0
whose block device files in the /dev directory are:
 brw------- 1 root system 8, 1 Jan 19 11:20 /dev/rz0a
 brw------- 1 root system 8, 1 Jan 19 10:09 /dev/rz0b
 Character device files are used for devices with driver handled I/O buffering. Some
disks, terminal, pseudoterminal, and tapes are defined as character device files:
 crw-rw-rw- 1 root system 7, 0 Jan 31 16:02 /dev/ptyp0
 crw-rw-rw- 1 root system 7, 1 Jan 31 16:00 /dev/ptyp1
 Socket device files. Printer daemon lpd and error logging daemon syslogd use the
socket device files:
 srw-rw-rw- 1 root system 0 Jan 22 03:40 log
 srwxrwxrwx 1 root system 0 Jan 22 03:41 printer
 When the disk and tape interface supports more than one device, each device file has
a major and a minor number, where the major number specifies the interface (example
SCSI connection) and the minor number specifies the unit on the interface.

61
Device Files

Symmetrical operations on devices


Device files
 /dev directory
 Hook to device driver in kernel

62
Major and Minor device numbers

Major and minor device numbers


 Pair unique for any device
Major device number
 Identifies hardware controller
– e.g. SCSI controller 1

Minor device number


 Identifies actual device within controller
tty

63
Process Commands

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 64


Process Related Commands

 ps – Lists processes (Process Status)


 For current user

 Common switches
 a – All
 e – Everyone
 f – Full
 l – Long

 kill command
 kill -x <pid>
 Values of x can be between 1 and 19 (typical)
 Need not necessarily cause termination of process
 Value of 9 is said to be sure-shot kill

 who
 List users on the system
 whoami
65
Interpretation of ps -l

 bash-3.2# ps -l
 F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0S 0 16459 16455 0 75 0 - 16539 wait pts/1 00:00:00 bash
0T 0 16769 16459 0 78 0 - 14779 finish pts/1 00:00:00 man
0T 0 17553 17551 0 75 0 - 15886 finish pts/1 00:00:00 less
4R 0 18155 16459 0 77 0 - 15873 - pts/1 00:00:00 ps
 bash-3.2#
 F – no longer used
 S state(status)
 UID – user ID
 PID – process Id
 PPID – Parent PID
 PRI – priority
 SZ - size
66
Processes

Identified by the process identifier (PID)


PID used to manipulate processes
PID used to locate information from process table
Process Table
 System wide
 Fixed size array of structures
 PID is index of array
 PID monotonically increases, dead process entries not
immediately reused

67
Processes and Signals

Used to signal events


Specific meaning to each signal
Kernel to process
Process to process
Default actions or disposition
Terminate process (with or without core)
Ignore
Default can be changed

68
Some Signals
 1 SIGHUP
 2 SIGINT
 3 SIGQUIT
 8 SIGFPE
 9 SIGKILL
 10 SIGBUS
 11 SIGSEGV
 12 SIGSYS
 14 SIGALRM
 15 SIGTERM
 16 SIGURG
 20 SIGCHLD
 30 SIGUSR1
 31 SIGUSR2
69
kill, SIGKILL, pkill
 The SIG part of the signal name is optional.
 To list the possible signal names, run kill with the -l switch:
 Signals can also be identified by a signal number.
 The bash shell’s built-in kill command lists the signals with
their signal numbers, for example: $ kill –l
 If you attempt to kill a process and it does not die, you can
try using the unignorable SIGKILL signal.
 So don’t use kill -9, it may lead to inconsistency on your
program
 A safer tool is pkill. The pkill command is used like the kill
command but instead of using the PID as the argument to
the signal, the command name is used. 70
The signal Call

Used for user defined handling of signals


signal(signo, function)
 Pointer to function, normally function name
Sets field in process table, no other action
Hence must be at beginning of program
Catcher can take only one argument, signo

71
Zombie Processes

 Normally, when a child process is killed, the parent


process is told via a SIGCHLD signal.
 Then the parent can do some other task or restart a new
child as needed.
 When a process is killed, a ps listing may still show the
process with a Z state. This is a zombie, or defunct,
process. The process is dead and not being used.
 In most cases, in a few moments it will actually be gone
and your ps output won’t show it.
 If you find that the process stubbornly refuses to die, you
may need to reboot your system.
72
The top Command

The top command is a very useful tool for quickly


showing processes sorted by various criteria.
It is an interactive diagnostic tool that updates
frequently and shows information about physical
and virtual memory, CPU usage, load averages,
and your busy processes.
The top command is usually found on *BSD,
Linux, and Mac OS X systems.
$top
^C to stop 73
Filter Commands

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 74


Advanced Tools
 Pipes and Filters
– wc
– Metacharacters
– grep
– sort
– cut
– paste
– head
– tail
– pg
– more
– tee
– tr

75
Filters

 Programs in a pipeline are executed sequentially.


 UNIX automatically handles the data flow from one program to the
next, producing the effect as if one program is being executed.
 A filter is a program or UNIX command that takes its input from
standard input, processes (or filters) it and sends its output to the
standard output.
 It is obvious that the commands such as ls, date, pwd etc. can not be
used as filters as they do not require any input.
 Filters can be used anywhere in the pipeline.

76
Pipes and Filters
 Connecting two or more commands in tandom: Pipes
 Filters
 Commonly used filters
 grep sort cut paste head tail wc pg more tee tr
 The fact that many UNIX commands accept input from the standard
input and send output to the standard output allows us to link them
together with a pipe ( | ).
 With this facility, the shell allows any number of such commands to be
connected in a sequence, known as a pipeline where the standard
output of each command in the sequence is piped into the standard
input of the next command as shown in the figure.

77
The wc Command

Counts the number of characters, words and lines


Switches to restrict count to one of the above
three elements
Examples
 wc test_file
 wc –l test_file
 wc –cw poem

78
Regular Expressions

 The patterns used by the grep utility are called regular expressions.
The regular expressions are always given in double quotes.
Regular Meaning
Expressions
[] To specify a pattern which consists of any one of a set of
characters.
Example "[xyz]" specifies the pattern either "x“ or "y" or "z"
[ ] with hyphen To specify a pattern which consists of any one of the range of
characters specified in the square brackets.
Example "[1-3]" specifies the pattern either 1 or 2 or 3.
Similarly "[ 1-9 a-z]" specifies a pattern of any one character which
can be a number from 1-9 or a lower case alphabet from a to z.
^character This character is used to specify that the pattern following it must
(caret symbol) occur at the beginning of the line.
Example "^[123]" specifies that the pattern either 1 or 2 or 3 should
appear at the beginning of each line to be selected.

79
Regular Meaning
Expressions
[^characters] When a caret symbol is specified within the square brackets and is
followed by one or more characters or ranges of characters
(indicated by hyphens) then strings containing any of the specified
will be excluded.
Example "[^123]" specifies that the searched string should not
contain 1, 2 or 3
Dot (.) This character is used in conjunction with any other characters and
indicates any one character
Example "[123]." specifies the pattern either 1 or 2 or 3 followed by
any single character
\ (Backslash) This character is used in conjunction with the above characters and
indicates that grep should ignore the special meaning of the
character following it in the regular expression.
Example "\[abc\]" specifies the pattern [abc] since the characters'['
and ']' are treated as ordinary characters and not signifying a set or
range of characters.
$(dollar) To specify that the pattern preceding it must occur at the end of
each line. Example "[abc]$" specifies the pattern either a or b or c at
the end of each line.

80
grep (Global search for Regular
Expression and Print)
 Searching a pattern in a file
 $ grep [options] "<pattern>" <filename>
 options:
 -n: prints line numbers
 -v: the reverse search criterion
 -c: display only a count of matching patterns

 regular expressions:
 "^" beginning of line
 "$" end of line
 "." any single character
 [...] any one character from the list
81
grep (Contd.,)

The grep (Global search for Regular


Expression and Print) utility, is the standard file
searching and selection utility. It is used to search
for a particular pattern of characters and display
all the lines containing the pattern.
This utility examines the input file line by line for
the given pattern. When a match is found, the line
is copied to the standard output file. No output is
produced when the desired pattern is not found.
Variants like egrep, fgrep,
82
grep (Contd.,)

 Example:
 Display the line(s) containing the string MURALI.
 $ grep “MURALI" emp.dat <Enter>

 Display the line(s) containing the string JOBBY preceded


by the line number.
 $ grep -n “JOBBY" emp.dat <Enter>

 Display the lines of the file excluding the line(s)


containing the string TARUNA.
 $ grep -v “TARUNA" emp.dat <Enter>

83
Emp.dat file
 Searching through the employee file using the grep command
 Consider the file employee having the following data:
PATHAK MANI 2057 CD E1 3 03/03/56 1575
BHATIA NAVEEN 9090 SALES E2 4 03/04/67 9000
YADAV JOHNNY 8909 MKT E3 6 09/12/78 8500
ALVA VIJAY 9009 MKT E6 7 09/11/53 29000
MISHRA M.R. 2103 ADMIN E3 7 03/11/54 12000
JOSE JOBBY 9001 MKT E2 5 03/11/75 3000
BABY P.K. 9002 ADMIN E3 10 03/12/69 6000
JAMEEL S. 9012 CD E3 10 03/12/62 6500
LATHA N. 9023 CD E2 10 03/12/56 6400
SINGH ROHIT 9015 TRG E2 4 09/07/69 6899
SINHA RAJEEV 1289 CD E3 11 12/11/62 9812
GULATI RAJENDER1190 ADMIN E1 3 05/03/66 5400
DUGGAL TARUNA 1193 ADMIN E3 7 02/07/65 7200
VERMA MOHIT 2390 CD E1 2 02/09/67 4389
SAXENA RAHUL 3476 ADMIN E6 18 01/04/55 18500
MISHRA JR 7654 MKT E3 7 07/07/64 9065
DAS HK 3420 MKT E1 2 05/03/68 5400
MURALI N 2978 TRG E4 8 04/03/61 9000
FRANC SHAM 1390 TRG E2 5 07/07/69 7000
84
Use of grep as a filter
 Finding a particular file in the directory:
 $ ls | grep "emp.dat"
 The output of the ls command is a list of files in the directory and this output
is directly given to the grep command as input file. The pattern "emp.dat"
will be searched in the list of file names produced by the ls command.
 Note that the symbol "|" is used to join the two commands in the pipe
line.
 To find out whether a given user has logged in or not:
 $ who | grep "mano“
 who command will produce a list of user-names who are currently logged in.
This output of who will become the input for grep and grep will search for
the pattern "mano".
 Note that, no error message is produced, if the search using the grep
command fails.

85
sort
 Arranges the output
 $ sort [- option] [+pos] [-pos] [-o output file name] [filename]
 options :
 -d Dictionary order: only letters, digits and blanks
 -n Sort on the first numeric field
 -r Reverse order of sort
 -u Eliminate duplicate lines in sorted output
 -t Specify field separator character (within double quotes after -t)
 -o Stores output in a file

 The sort command takes lines from input file and sorts
them, displaying the output on the standard output device.
By default the sorting is done on the first field in ascending
order. 86
sort (Contd.,)
 Each line from the input file is treated as a series of one or more fields
separated from each other by a space, or tab by default or any other
field delimiter if specified.
 The command line format is: $ sort [option] [filename]
 sort without a filename takes input from the standard input and
arranges each line alphabetically and produces the output on the
standard output.
 $ sort <Enter> the output will be:
Alka
 Alka
Bhupinder
 Lalit Lalit
 Bhupinder Malvika
 Puneet Puneet
$
 Malvika
 <CTRL+D> 87
sort (Contd.,)

 $ cat > namelist <Enter> $ sort namelist <Enter>


 Puneet Alka
 Malvika Bhupinder
 Lalit Lalit
 Bhupinder
Malvika
 Alka
Puneet
 <CTRL +D>
$
 To display the output of the file specified in the reverse order:
 $ sort -r namelist <Enter>
 Puneeta
 Malvika
 Lalit
 Bhupinder
 Alka
 $ 88
The cut command

Retrieves a sets of columns from a file


Concept of a delimiter
Default is tab
Examples
 cut –d, -f2 emp.dat
 cut –d, -f2,3 emp.dat
Alternate syntax
 cut –c2-15 emp.dat

89
cut
 Retrieving selected fields from a file: $ cut [options] <filename>
 options:
 -c selects columns specified by list
 -f selects fields specified by list
 -d field delimiter (default is tab)
 It is often useful to organize information in a file as a table, with rows
and columns.
 Each row of a table represents one record in the file.
 An example can be of an employee file (emp.dat) having records of
different employees.
 The cut command of UNIX is useful, when a file has to be queried to
display selective fields from a file. This command picks up selected
columns or fields from a file, by specifying a list of character positions or
a list of fields.
90
Example
 The command line format is:
 $ cut [options] <filename> <Enter>
 To display the employee code, firstname and basic salary from the employee file
 $ cut -d" " -f2,3,8 emp.dat <Enter> where
 -d is used to specify the field delimiter. A space in this case.
 -f represents fields and the numbers after it are the field positions.
 The specified columns are copied to the standard output.
 To display characters 4 to 9 both inclusive, from each record of the file employee:
 $ cut -c4-9 emp.dat <Enter>
 To display fields from first to fourth:
 $ cut -d" " -f-4 emp.dat <Enter> (note that -4 means 1-4)
 To display from the second to the fourth fields:
 $ cut -d" " -f2-4 emp.dat <Enter>
 To display from the fourth field onwards:
 $ cut -d" " -f4- emp.dat <Enter>

 Using cut as a Filter


 $ grep "SALES" emp.dat| cut -d" " -f2,8 | sort <Enter>
 ls -l | cut -f3 what is the output, correct if anything required.

91
tee
 The output redirection will redirect the standard output to a file or
some destination other than the terminal. In case the output is
required to be sent to two different destinations at the same time, the
redirection operator can not be used.
 The tee command is used to send the output to more than one
destination.
 As the name suggest, the command is like a 'T', having one source of
input and two destinations of outputs.

92
tee (Contd.,)

 tee transfers data without any change from its standard input to its
standard output, but at the same time it directs a copy of the data into
another file, that is specified at the command line. This command can be
used anywhere in a pipeline.
 If the tee command is given at the $ prompt, it will expect input from the
keyboard and at the end of input, it displays the output on the terminal and
also stores it in the file specified. The format is: $ tee <filename> <Enter>
 This command differs from other filters as it sends a copy of its output to a
disk. $ <command1> | tee <file> | <cmmand2>
 The standard output of the command1 becomes the standard input for
tee. This input is then passed on as the standard input for command2 and
a copy is diverted to file at the same time.
 $ sort emp.dat | tee tempfile | pg will sort the emp.dat file, display the
result on the screen page wise and store it in tempfile. 93
The paste command
 Vertical combination of files
 paste <file1> <file2>
 options: -d (field delimiter)

 The paste command prints files side by side in a


columnar manner. It takes two tables and combines them
side by side forming a single wide table as the output.
This utility is opposite to cut.
 This command uses tab as a field delimiter. However, with
the –d option a different delimiter can be given
 The command line format is:
 $ paste [option] <filename1> <filename2> <Enter>
94
Examining file contents

 head [-n] file : display first n lines of a file


 tail [+/- n] file : display last n lines of a file
 wc [-lwc] file : count lines, words and characters
 pg and more : page wise display a file
 tr : translate characters
 tee : send output to two different destinations
 head
 This command by default displays first ten lines of a file.
 $ head emp.dat will display first ten lines from emp.dat
 $ head -5 emp.dat will display first five lines from the emp.dat

95
Examining file contents (Contd.,)

 tail
 This command displays the last ten lines of the file by default.
 The number of lines can be changed by providing it along with the
tail command.
 The options can be of two types:
 $ tail emp.dat will display the last ten lines from emp.dat
 $ tail -7 emp.dat will display the last seven lines
 $ tail +10 e emp.dat will display lines from the 10th line till end of
the file

https://www.linux.com/blog/14-tail-and-hea
d-commands-linuxunix
96
Examining file contents (Contd.,)
 The options available with wc are:
 -c: for character count only
 -w: for word count only
 -l: for line count only
 $ wc -c emp.dat may display 1756
 $ wc -w emp.dat may display 296
 $ wc - l emp.dat may display 24
 The head, tail and wc commands are filters and can be used anywhere in a pipeline.

 more
 controls the display of a file on the screen
 $ more [filename]
 options:
– + line number : starts the display at the line number

– +/pattern : starts the display two lines before the pattern

– -c : clears the screen before display


97
Examining file contents (Contd.,)

 The more command, reads the contents of a long text file one
screenful at a time. To display the next screen the space bar is
pressed, if the user presses the <Enter> only the next line is
displayed. The more command continues displaying the contents till
the EOF (end of file) is reached.
 The command line format is:
 $ more <filename> <Enter>
 This command displays 23 lines, at a time, then the prompt ---
More--- is displayed on the last line. To quit before the EOF is
reached,' q' can be pressed which will terminate the program and
return to the shell prompt.
 $ more employee will display the file employee one screenful at a
time
98
tr

 This command is used to squeeze the repetitive characters


from the input and translate the input to some other form.
 The format of tr command is: tr <str> <newstr> < <file|
stdin> where newstr is the string to replaced with every
occurrence of str in the input.
 To squeeze the repetitive characters, the -s option is used
followed by the character to be squeezed.
 tr -s "c" < <file> where "c" is the character to be squeezed
 tr -s " " filename will wait

99
Example (Contd.,)
 To convert all lower case alphabets into upper case and display them on the
terminal:
 $ cat emp.dat | tr "[a-z]" "[A-Z]"
 Display all the occurrences of small "a" as "A":
 $ ls -l | tr "a" "A"
 To produce the file size and names in descending order of size:
 $ ls -l | tr -s " " | cut -d" " -f 5,9 | sort -nr <Enter>
 The output of ls -l and who are in a tabular form, where the columns are
separated by one or more spaces. To provide these output to cut as input, the
extra spaces need to be squeezed to a single space.
 To produce only the login names and time of login:
 $ who | tr -s " " | cut -d" " -f1,5 <Enter>

100
vi Editor

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 101


Agenda

102
vi Editor
vi works in 3 different modes
 Command mode : Keys are interpreted as commands
 Insert mode : Keys echoed in edit buffer
 Esc colon (:) mode - Keys interpreted as commands of
ex mode

103
Editor Work Modes
$ vi filename write
UNIX Shell on the prompt to All types of operations
open file. and navigation are done
in command mode.
Command Mode
Append (a,A) Allows to make
Insert (i,I) Text Input Mode changes in the file.
Change (o,O)
Replace (R)
Delete (x , dd)
Join (J) Press esc key to
move back to the
Undo (u,U) esc command mode.
Yank ( y, Y) ,etc.
Press : (colon) key
to go to Exit Mode.
Allows exit and
set commands.
Exit Mode UNIX Shell

 Under different modes the same key press creates different effect.
104
What is the input mode?
 The Input mode is used to insert, append, replace or change
text.
 A summary of input mode commands are given below:
Command Function
i Inserts text to left of cursor
I Inserts text at beginning of line
a Appends text to right of cursor
Command Function
A Appends text at
i end of line
o Opens line below
O Opens line above
r ch Replaces single character at cursor with character ch
R Replaces text from cursor to right
s Replaces single character at cursor with any number of characters
S Replaces entire line
105
Saving & Quitting: The Last Line Mode
 vi uses the ZZ command to save and quit editor. The ‘ex’ mode, also referred to as last
line mode, can also be used.
 To switch from command mode to ex mode, a colon (:) is pressed, which appears as ex
prompt in the bottom line. Any ex command can be entered at this prompt.
 Following commands can be used for saving and quitting from the ex mode:
Command Function
w Write buffer into the editor
x Save and quit the editor
wq Write and quit editor
q Quit editor
 The Repeat Factor
 A number can be prefixed to any command: most commands will interpret the
instruction to repeat the command that many times. Hence this number is called as the
repeat factor.
 For example, to insert a series of 30 asterisks in a line, 30i* can be used.
 The repeat factor can be used with input as well as command mode. 106
Navigation (Cursor Movement commands):

Command Function
h or backspace Move cursor left
j Move cursor down
k Move cursor up
l or spacebar (or spacebar) Move cursor right
^ Move cursor to beginning of first word of line (no
repeat factor)
0 or | Move cursor to beginning of line (no repeat factor
with 0)
$ Move cursor to end of line
b Move cursor back to beginning of word
e Move cursor forward to end of word
w Move cursor forward to beginning of word

107
Paging Functions

Command Function
<ctrl-f> Full Page forward
<Ctrl-b> Full Page backward
<Ctrl-d> Half Page forward
<Ctrl-u> Half Page backward
<Ctrl-l> Redraw page screen (no repeat
factor)

108
Search and repeat commands

Command Function
/pat Searches forward for pattern pat

?pat Searches backward for pattern pat


n n Repeats search in same direction
as previous search (no repeat factor)
N Repeats search in opposite direction
as previous search (no repeat factor)
fch Moves cursor forward to first
character occurrence of ch in current line

109
set command
 There are many options which can be used with :set command
 To get the list of all options in set command use :set all

110
Commands Summary

111
Review of vi Motion Commands
Movement Command
Scroll forward one screen ^F
Scroll backward one screen ^B
Scroll forward half screen ^D
Scroll backward half screen ^U
Scroll forward one line ^E
Scroll backward one line ^Y
Move current line to top of screen and scroll z ENTER
Move current line to center of screen and scroll z.
Move current line to bottom of screen and scroll z-
Redraw the screen ^L
Move to home—the top line of screen H
Move to middle line of screen M
Move to bottom line of screen L
Move to first character of next line ENTER
Move to first character of next line + 112
Review of Motion Commands (Contd.,)

113
Operators

 vi uses a number of operators which can be used along


with commands to perform
 complex editing functions. The most commonly used
operators are:
 d – delete
 c – change
 yy – yank (copy)
 ! – filter to act on text

 Operators can work only when combined with a command


or itself. The operators
 also take a repeat factor.
114
Operators (Contd.,)
 Some samples of using operators:
 Command Function
 d$ or D Deletes from cursor to end of line
 5dd Deletes five lines
 d/endif Deletes from cursor up to the first occurrence of the string endif
in the forward direction
 d30G Deletes from cursor up to line number 30
 df. Deletes from cursor to first occurrence of a dot
 c0 Changes from cursor to beginning of line
 c$ or C Changes from cursor to end of line
 3cw / c3w Changes three words

115
Yank and put

The yy command yanks a line


With a count xyy
 Yanks x lines
The p command
 After current line
The P Command
 After current line
ctr + R for Redo

116
Search and Replace

/pattern
 Searches forward for first occurrence of a pattern.
?pattern
 Searches backward for first occurrence of a pattern.
n
 Repeats the last search.
N
 Repeats the last search command in opposite
direction.

117
Search and Replace (Contd.,)

 :s/ptn1/ptn2
 Replaces first occurrence of ptn1 with ptn2.

 : s/ptn1/ptn2/g
 Replaces all occurrences in the current line.

 : m, n s/ptn1/ptn2/g
 Replaces all occurrences in lines m to n.

 : ., $ s/ptn1/ptn2/g
 Replaces all occurrences from current line to end of file.

 % s/ptn1/ptn2/g
 Replaces all occurrences in file

118
Handling Multiple Files
 Can open more than one file
 vi file1 file2 file3
 Opens all files

: n
 Permits editing of next file in the buffer

 : rew
 Permits editing of first file in buffer

 : args
 Displays names of all files in the buffer

: f
 Displays the name of the current file

119
Customizing vi
 The $HOME/.exrc file
 The set command
– :set nu

 The abbr command


– :abbr mc “Mahindra Comviva Inida Pvt. Ltd.,”

 The map command


– :map ^X :wq

120
Named Registers

 The “ command
 Example “adw deletes word placing it in register a
 “ap puts contents of register a

121
vi v/s vim
 Vim (vi-improved)
 Functionally, vim is almost a proper superset of vi. Therefore,
everything that is in vi is available in vim.
 Vim adds onto those features. Here are a some of the extended vim
features:
– Vim has been ported to a much wider range of OS's than vi.
– Vim includes support (syntax highlighting, code folding, etc) for
several popular programming languages (C/C++, Python, Perl, shell,
etc).
– Vim integrates with cscope.
– Vim can be used to edit files using network protocols like SSH and
HTTP.
– Vim includes multilevel undo/redo.
– Vim allows the screen to be split for editing multiple files.
– Vim can edit files inside a compressed archive (gzip, zip, tar, etc). 122
vi v/s vim (Contd.,)
– Vim includes a built in diff for comparing files (vimdiff).
– Vim includes support for plugins, and finer control over config and startup
files.
– Vim can be scripted with vimscript, or with an external scripting language
(e.g. python, perl, shell).
 There are many more differences. Refer below sources which are few of
good places to start finding out more.
 :h vi-differences

123
Shell Programming

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 124


Basic Shell Programming

Types of shells
Creating and running a shell program
Variables
Expressions
String operations
Operators
Read and echo
Conditional statements

125
The UNIX Shells

Bourne Shell
 sh
C Shell
 csh
Korn Shell
 ksh
Bourne Again Shell
 bash

126
The UNIX Shells (Contd.,)

 Bourne Shell
 The oldest amongst all shells
 Still the shell language of choice

 C Shell
 Early BSD enhancement
 Provides C like syntax
 Significant advances over Bourne shell
– History and command substitution
 Extremely non-intuitive and convoluted syntax
– Alias
– Arrays

127
The UNIX Shells (Contd.,)

Korn Shell
 Developed in mid 80’s by David Korn
 Completely compatible with the Bourne shell
 Extends the Bourne shell
 Firstly with the addition of all C Shell features
– More intuitively than the C shell
 Secondly, with the addition of completely new features
– select and let

128
The UNIX Shells (Contd.,)

The Bourne Again Shell


 Completely compatible with the Bourne shell
 Extends the Bourne shell
 Most features like the Korn shell
 Some additional features
 Directory stacks

A few more shells


 Extension of C Shell called TC
– Command completion
 Z Shell
– Variant of the Korn
129
Shells – Comparison

Shell Type
Feature sh csh ksh bash
Job Control O P P P
Command History O P P P
Command Line Editing O O P P
Local Variables O O P P
Aliases O P P P
Custom Prompt O O P P

130 130
How to Run Shell Scripts

 Use vi to write the script


 Take care not to name the file as either an internal or external
command
 Precedence order
 Internal
 For each directory in $PATH in order
– External
– Shell script

 Use chmod to turn on execute permission


 Run the file like any other command
 Shell is located in /bin
 vi file.sh or vi file and sh file.sh or sh file
131
Rules for variables

The first character of variable name should be


alphabet or underscore
No commas or blanks allowed
Variable names should be of reasonable length
Variable names are case sensitive, name and
nAme are different variables
Variable name should not be a reserve or key
word

132
Variables
 Is a database or memory location name, is temporary location, its
value can change during execution
 Shell has only string variables, no data types
 Special handling for numeric values
 var=value
 Examples
 greeting1=Hello
 greeting2=World
 greeting=“$greeting1 $greeting2”

 Three types:
 Local variables
 Constant variables
 Global variables
133
Constant and Global variables
 Constant variables
 Will not change their value during the execution
 readonly is the keyword to create constant variables
 x=100
 readonly x
 Global variables
 Scope of these is global
 export is the keyword to create global variables
 export variablename
 To comment a line
 #
 # this line is commented

134
Some Important Shell Variables

 $HOME – Home directory of the current user


 $PATH – Path used whilst searching for external commands or
executable files
 PATH=$PATH:$HOME/bin
– Appends the bin directory located under the home directory of the
current user
 $TERM – Terminal type of the current terminal
 ANSI standard terminals
 VTxxx – Virtual Terminals originally manufactured by DEC
 $VISUAL and $EDITOR – Editor used for command substitution
 $USER – Current user

135
Shell Keywords

 Shell key words are

break exec set


case exit shift
continue export trap
do fi ulimit
done for umask
echo if until
else read unset
esac readonly wait
eval return while

136
Built-in Variables

 IFS
 Internal Field Seprator, space/tab/\n
 PS1 (Prompt String)
 Primary Shell prompt
 PS1=“BVP$”
 PS2
 The system prompt2, default >
 PS3
 Used for display of select command
 PS4
 set –x tracing

137
Built-in Variables

 $?
 Exit status
of last cmd
 $!
 PID of last
bg cmd
 $$
 PID of script
 $0, $1…$9
 Arguments to script
 $#
 Number of arguments
 $* or $@
 All arguments
138
The read and echo commands
 read
 Take input from the user
 read variablename
– Dot Not read $var like this

 read –p “Enter value:” variablename


 read –s –p “Enter passwd:”variablename
 echo
 A shell keyword used to write data to the output scree
 echo Welcome to Comviva Technology
 echo Today’s date is : `date`
 echo the value entered is $variablenameecho the password entered is
$variablename

139
Substitution Operators
 Variable substitution operator
 $variablename or ${variablename}
 Command Substitution operator
 `commandname` or $(commandname)
 Quotes and double quotes
 Used when whitespace and other special characters
 Single quotes do not cause variable substitution
 Double quotes cause variable substitution
 Backquotes
 Cause the output of a command to be stored in the variable
 year=`date | cut -d” “ –f6`
 a = 100, b = 1.5, n = $a, course = UNIX, name=“Comviva”,
mypath=“/home/bin/abc”
 k = `ls’, c = `cat emp.dat`
140
The expr command

 To convert the string expression to integer expression


 expr is mandatory for all arithmetic expression
 For arithmetic operations
 Integer
– c=`expr $a + $b`
 With a space string without space assignment
– c=$(($a+$b))
– echo `expr $a \* $b` escape sequence for *
– echo * display all the files in the current working directory

 Float
– a=10.56
– b=5.4
– echo `echo $a + $b | bc -l` - for float redirect to bc (calculator cmd with library
option)
141
File Operators

 File test commands

Option Meaning
-s file True if exist and its size > 0
-f file True if exist and it is not a directory
-d file True if exist and it is a directory
-c file True if exist and it is character special file
-b file True if exist and it is block special file
-r file True if exist and it has read permission
-w file True if exist and it has write permission
-x file True if exist and it has execute permission

142
String Operators
Arithmetic Operators
• str1 = str2
Operator Meaning • Equal =
+ Addition • str1 != str2
• Not equal !=
- Subtraction
• -n “str1”
* Multiplication
• Not null (true if str1 is not
/ Division empty)
% Remainder • -z “str1”
• Null (true if str1 is empty)

143
Numeric Operators
Operator Meaning
-eq Equal to
-ne Not equal to Logical Operators
-le Lesser than or equal to
-lt Lesser than
Operator Meaning
-ge Greater than or equal to
-gt Greater than -a Logical and
-o Logical or
! Logical not

144
The if Construct

if expr if expr if expr


then then
then commands commands
commands else elif expr
commands then
fi fi commands
else
commands
fi

145
The case Construct

case word in
Pat1) commands
;;
Pat2) commands
;;
*) commands
;;
esac

 Pattern examples
 Yes | yes)
 [yY]*)
146
Looping constructs
 while
 As long as condition is true
 until
 As long as condition is false
 for
 Not like C for loop

147
Looping constructs syntax
until expr
while expr
do
do commands
done
commands num=1
done until [ $num -gt 10 ]
do
echo $num
num=1 num=`expr $num + 1`
done
while [ $num -le 10 ]
do for var in word1 word2 … wordn
do
echo $num commands
num=`expr $num + 1` done
for i in *
done
do
echo $i
done 148
Debugging Shell Scripts
 Syntax error, and one of the ways to search for and correct them is to use –n
while running a script
 Set –x displays + lines as they are executed
 Set –v (verbose) displays lines as they are executed
 Set –vx causes all

set -x
a=1
b=2
let c=$a+$b
echo $c

 sh –n script.sh sh –x script.h sh –v script.sh


149
Shell Functions
 Set of commands to be executed repeatedly
 Takes arguments
 $1 is first argument to script
– $1 reset after function termination

 Returns values
 To declare a function, simply use the following statement:
 name () { commands; }
 Called with the name along with arguments
 The function call args will be in $1 to $9, $#, $?
 Advantages:
 Reduce code length of the program
 Reduce the cost of maintenance
 Easy to maintain the application
150
Examples
1. Simple function with out any argument
function hello() {

echo “Welcome to Shell functions!”


}
hello
2. Function accept 2 arguents
repeat() {
echo “I don’t know $1 $2 “
}
repeat Your Name
3. Function accept 2 arguments and do computation on them
function add() {
if [ $# -eq 2 ]
then
c=`expr $1 + $2`
echo " $1 + $2 = $c "
else
echo "Invalid number of arguments"
fi
}
add 15 2
151
Function returning a value
function fact()
{
f=1
i=$1
while [ $i -ge 1 ]
do
f=`expr $f \* $i`
i=`expr $i - 1`
done
return $f # returning a parameter
}
echo Enter the number:
read num
fact $num #calling a function along with parameter

echo The factorial is $? # returned parameter is in $?

152
Running and Stopping Services
Commands

Copyright © 2013 Comviva Technologies Limited. All rights reserved. 153


Network Configuration Commands
 Ifconfig
 Used to view the properties of active and inactive network interfaces
 ifconfig
 Ifconfig –a - to display information about inactive network interface
 ifup / ifdown
 Used to activating and deactivating a network interface
 ifdown eth0
 ifup eth0
 mii-tool
 Tool allows a system administrator to view, monitor, log and change the
negotiated speed of Ethernet network cards
 mii-tool –v - to view the current status of network interface card
 mii-tool –v --force 100baseTx-FD eth0
– To change the negotiated speed of Ethernet network card

Comviva Internal
154
Network Configuration Utilities

 netconfig
 Text-based network configuration tool
 Only writes config files. Does not activate device or changes.
Use ifup/ifdown to active changes or restart the network service

 system-config-network
 GNOME-based network configuration tool
 Can be launched by a non-privileged user, but requires authentication as root.

Comviva Internal
155
ssh: Secure Shell

 ssh allows remote logins and remote command execution via a secure
encrypted connection
 Example
 ssh [email protected]

scp: Secure Copy


 scp works like cp, except it copies from one host to another in a secure
encrypted channel
● Example

● scp somefile [email protected]:

Comviva Internal
156
Network (IP) Bonding
 Network bonding is a process of combing or joining two or more
network interfaces together into a single interface.
 Network bonding offers performance improvements and redundancy
by increasing the network throughput and bandwidth.
 If one interface is down or unplugged the other one will work.
 It can be used in situations where you need fault tolerance,
redundancy or load balancing networks.
 In Linux, a special kernel module named bonding is used to connect
multiple network interfaces into a single interface.
 Two or more network interfaces can be connected into a single logical
“bonded” interface.
 The behavior of the bonded interfaces depends on the type of
bonding method used.
157
Types of Network Bonding
 mode=0 (balance-RR)
 mode=1 (active-backup)
 mode=2 (balance-XOR)
 mode=3 (broadcast)
 mode=4 (802.3ad)
 mode=5 (balance-TLB)
 mode=6 (balance-ALB)

158
Types of Network Bonding (Contd.,)
 mode=0 (balance-RR)
 This mode is based on Round-robin policy and it is the default mode.
 This mode offers fault tolerance and load balancing features.
 It transmits the packets in Round robin fashion that is from the first
available slave through the last.

 mode-1 (active-backup)
 This mode is based on Active-backup policy.
 Only one slave is active in this band, and another one will act only when
the other fails.
 The MAC address of this bond is available only on the network adapter
part to avoid confusing the switch.
 This mode also provides fault tolerance.
159
Types of Network Bonding (Contd.,)
 mode=2 (balance-XOR)
 This mode sets an XOR (exclusive or) mode that is the source MAC
address is XOR’d with destination
 MAC address for providing load balancing and fault tolerance.
 Each destination MAC address the same slave is selected.

 mode=3 (broadcast)
 This method is based on broadcast policy that is it transmitted everything
on all slave interfaces.
 It provides fault tolerance. This can be used only for specific purposes.

160
Types of Network Bonding (Contd.,)
 mode=4 (802.3ad)
 This mode is known as a Dynamic Link Aggregation mode that has it created
aggregation groups having same speed.
 It requires a switch that supports IEEE 802.3ad dynamic link.
 The slave selection for outgoing traffic is done based on a transmit hashing
method.
 This may be changed from the XOR method via the xmit_hash_policy option.

 mode=5 (balance-TLB)
 This mode is called Adaptive transmit load balancing.
 The outgoing traffic is distributed based on the current load on each slave
and the incoming traffic is received by the current slave.
 If the incoming traffic fails, the failed receiving slave is replaced by the MAC
address of another slave.
 This mode does not require any special switch support. 161
Types of Network Bonding (Contd.,)
 mode=6 (balance-ALB)
 This mode is called adaptive load balancing. This mode does not require any
special switch support.

162
Batch Scheduling
 The at command
 Run command “at” specified time
 at HH:MM command
 User must be allowed in at.allow and not denied in at.deny

163
Try it by yourself
 1. Locate your document and check the current date as in the following:
 $ ls
– important_report.swx

 $ date
– Thu Dec 9 14:53:06 CST 2004

 2. Type the at command followed by the time you want the job to occur:
 $ at 17:00

 3. Type the copy command (cp), the name of file you want to copy, and the location of the file in
which you want to place the backup copy on the file system, and then press Enter:
 at> cp important_report.swx ~username/backups

 4. Press Ctrl + D to exit the at shell; you should see something akin to the following:
 job 1 at 2004-12-10 17:00

 Use the atq command to double-check that the job has been scheduled. To delete a command
before it has been executed, use the atrm command:
 $ atq
 1 2004-12-10 17:00 a username
 To delete a command before it has been executed, use the atrm command:
 $ atrm 1

164
The cron scheduler and crontabs

The cron deamon runs in the background


checking for jobs
 Runs jobs periodically
 /user/var/cron database
 Not to be edited directly
Edited via command crontabs
 The crontab –e command
 User must be allowed access through cron.allow and
not denied though cron.deny

165
The crontab format

 Six fields
 minute (from 0 to 59)
 hour (from 0 to 23)
 day of month (from 1 to 31)
 month (from 1 to 12)
 day of week (from 0 to 6) (0=Sunday)
 Command to be run
 0 1 * * 5 /bin/execute/this/script.sh causes script to be run every Friday at
1 AM

166
Network related commands

ifconfig
netstat
Listing all the LISTENING Ports of TCP and UDP
connections Listing all ports (both TCP and UDP)
using netstat -a option.
Listing only TCP (Transmission Control Protocol)
port connections using netstat -at.
Listing all active listening ports connections
with netstat -l.

167
Summary

 Introduction to Linux OS
 Commands- basic and advanced
 vi editor
 Shell Scripting
 Debugging – GDB
 SED
 AWK
 DB Connection to MySQL

168
Thank You

169

You might also like