Introduction To Unix1.2
Introduction To Unix1.2
Unix/Linux
Definition of OS
It is interface between the user and the
hardware, it will take care of memory
management and process management etc.
UNIX
The Unix System was first described in 1969 by
Ken Thompson and Dennis Ritchie
Based on Multics
The goals of the multics system to provide
simultaneous computer access to a large
community of users, to supply ample computation
power and data storage, and to allow users to share
their data easily if desired.
UNIX
1973-Dennis Ritchie re-wrote unix kernel using c
language.
Mid 70's-BSD-Berkeley software distribution from
Berkeley University.
Two version of UNIX emerged are AT&T unix and
BSD unix
Linus Torvalds at the university of Helsinki
described new Flavour of unix specifically for i-
386 that was hosted on net for free download and
named as LINUX
UNIX
Over 30 years of development, so it is
Robust,versatile,familiar to hardware/software
developers & Popular for mission-critical
applications,e.g. Web servers
Portability without hardware constraints.
Hardware -independent file system.
Standards
POSIX:Portable Operating Sys.Interface for
UNIX(IEEE/ISO)
GNU:GNU's Not Unix(Free Software
Foundation)
OS
Kernel:- The core features of operating
systems like Process management,Memory
management,I/O management & system
resources
Shell:-Is a command interpreter
Features of Shell are:- -
Foreground processing -
Background processing
-I/O redirection
-Shell Variables -
Programming Constructs
Features of Unix
Multiuser
Multitasking
Multiprocessing
Multiterminal
I/O Redirection
Hierarchical File System
Security
Modular
Accounting
Architecture of UNIX System
Hardware CC
cpp
as
ld
sh
who
a.out
date
wc
grep
ed
vi
wc
Block Diagram of the system kernel
Libraries
System call Interface
File -sub
System
Char Block
Device Driver
Inter -process
communication
Process
Control Scheduler
Sub Memory Management
System
Hardware Control
Hardware
Kernel level
Hardware level
Kernel level
User level
User Programs
Common LINUX Commands
What are types of commands
available in Linux?
There are several types of command are available in LiNuX .
Those are
Terminal Control Characters for C-shell
Login and Authentication
Information
File Management
Display Contents of files
Directories
Devices
Controlling Program Execution for C-shell
E-mail and communication
Editors and Formatting Utilities
1.Terminal Control Characters
for C-shell
Erase previously typed character
Erase entire line of input so far typed
End-of-input for programs reading from
terminal
Stop printing on terminal
Continue printing on the terminal
Suspend currently running job; restart with bg
or fg
Kill currently running program and allow
clean-up before exiting
Emergency kill of currently running program
with no chance of cleanup
^h , backspace
^u
^d
^s
^q
^z
Del, ^c
^\
Functions Commands
2. Login and authentication.
Access computer; start interactive session
Disconnect terminal session
Change local login password; you must set a non-
trivial password
Login
Logout
Passwd
Functions Commands
Continued
su set user
su switches to super user
su username switches to particular
username
3. Information
Find out info about a user@system finger
Who is logged onto this terminal whoami
Who is on the system and what are
they doing
w, who
On-line documentation for GNU
programs
info
Show on-line documentation by
program name
man
Display system messages mesg
List of previously executed
commands
history
Show date and time date
Functions Commands
Continued
date +%T
date +20%y
date +it is now %m/%d/%y
date +%M
date +%Y
date +%D
Continued
HISTSIZE=50
!n
mesg y
mesg n
man 1 mkfifo
man 2 mknod
man 3 mkfifo
man 8 halt
info mkfifo
Continued
who
w
whoami
finger
The file is a regular file
The file is an executable
The file is a directory
The file is a symbolic link
The file is a fifo (named pipe)
The file is a socket
(nothing)
*
/
@
|
=
Definition Character
Continued
Block special file(devicefile)
character special file
Directory
plain file
Symbolic link
Named pipe
Socket
b
c
d
f
l
p
s
Definition Character
Continued
4. File Management
Combining files
Copy files
List files in a directory and their attributes
Change file name or directory location
Remove files
Create another link (name) to a file
Set file permissions
Find files that match specified criteria
cat
cp
ls
mv
rm
ln
chmod
find
functions Commands
Continued
cat>file Ctrl d
cat file1 file2
cat >> file
cp -option source dest
options:- -r ,-f,-i
ls -option argument
options:- -l,-R,-i,-a,-F,-t,-u
Continued
ls -lia
ls -lF
ls -lt
ls -lu
ls -R
Ls -ls
Continued
mv source destination
rm -option argument
option:- -r ,-f,-i.
ln file file1
ln -s file file1
chmod perm files
perm:- 2 write 4 read 1 execute
chmod 0777 directoryname
Continued
find path expression
-xdev donot descend directories on other filesystem
-inum n selects file having inode number n
-type p selects file named pipe
-user username selects file owned by user
-perm 0644 selects files with perm of 644
-links n selects files with no of links =n
-print prints selected file
-atime n file was last accessed n days back
Continued
find path expression
find . -name \*.c -print
find .. -type f -print
find . -type d -print
find ~ -perm 644 -print
find . -name core -print 2>/dev/null
find / -user raju -xdev -print
find / -atime +31 -print
find / -xdev -inum 123456 -print
find . -name core -exec ls -l {} \;
find . -name core -ok ls -l {} \;
find . -name core |xargs ls -l
Continued
find . \( -name core -o -name \*.c \)
find . -mtime +31
5 Display contents of files
Copy file to display device
Screen editor for modifying text files
Files on the display terminal with page control
Less is more
Show 1
st
few lines of a files.
Show last few lines of a file ; or reverse line
order
Display lines that match a pattern
Send file to line printer.
Format files with page headers, multiple
columns etc.
Count characters, words, and lines in a files.
cat
vi
more
less
head
tail
grep
lpr
pr
wc
functions Commands
Continued
ls -lia | more
Less:-
command Descriptions
space Scroll forward one screen
b Scroll backward one screen
enter Scroll forward one line
y Scroll backward one line
g go to the beginning of text
G go to the end of the text
/pattern search forward for pattern
?pattern search backward for pattern
n next occurrence of last search
N previous occurrence
Continued
ls -l|head => first 10 lines
ls -l| head -5 => first 5 lines
ls -l|tail => last 10 lines
ls -l|tail -5 => last 5 lines
ls -l |tail +5 => starting from line 5 till EOF
grep word filename filename1 => serach a
pattern
grep root /etc/passwd => get the line which
has got root in the specified location
/etc/passwd => all passwords are encrypted
and stored
Continued(options of grep)
Precedes each line by the block number on which
it was found
Display a count of matching lines rather than
displaying the lines that match
Ignores case
Inverts the search to display only lines that do not
match
Precedes each line by its relative line number in
the file
-b
-c
-i
-v
-n
What it does Option
Continued( grep's regular expr metacharacters )
Beginning of line anchor
End of line anchor
Matches one character
Matches zero or more characters
Matches one in the set
Matches one not in the set
Beginning of word anchor
End of word anchor
Tags matched characters
Repetition of character x, m times .
Repetition m times and not more than n times
^
$
.
*
[]
[^]
\<
\>
\(..\)
x\{m\}
x\{m,n\}
Function Meta
Datafile
northwest NW Ken 3.0 .98 3 34
western WE Ritchie 5.3 .97 5 23
southwest SW Thompson 2.7 .8 2 18
southern SO Dennis 5.1 .95 4 15
southeast SE Linus 4.0 .7 4 17
eastern EA Torvalds 4.4 .84 5 20
northeast NE Richard 5.1 .94 3 13
north NO Robert 4.5 .89 5 9
central CT Raju 5.7 .94 5 13
grep '^n' datafile
grep '4$' datafile
grep '5\..' datafile
grep '\.5' datafile
grep '^[we]' datafile
grep '[^0-9]' datafile
grep '[a-z]\{9\}' datafile
grep '\<north\>' datafile
grep -n '^south' datafile
grep -i 'raju' datafile
grep -v 'Richard' datafile
grep -c 'west' datafile
grep -w 'north' datafile
Continued( egrep's regular expr metacharacters )
Matches one or more of the preceding characters
Matches zero or one of the preceding characters
Matches either a or b
Groups characters
+
?
a|b
()
Function Meta
Continued...(egrep ex)
egrep 'NW|EA' datafile
egrep '3+' datafile
egrep '2\.?[0-9]' datafile
egrep '(no)+' datafile
egrep 'R(i|o)' datafile
fgrep '[a-z]****[0-9]..$5.00' file
cat datafile | lpr
pr -h <IMP> datafile
pr -l 12 datafile
wc
wc -l, wc -w, wc -c
6. Directories
Create new directory
Change to new directory
Remove empty directory.
Change name of directory
Show current directory
mkdir
cd
rmdir
mv
pwd
functions Commands
Continued...
mkdir
mkdir /src/books/power
!!/articles
mkdir -p /src/books/power/articles/files
mkdir -m 755 /src/books/power
7.Devices
Show disk space used by files or directories
Describe free space on disk device
du
df
functions Commands
8. Controlling program execution
Run job in back ground
Kill job in for ground
List current jobs
Print process status
Kill back ground jobs
Run program at lower priority
Create alias name for program(in,login)
Execute command file
&
del
jobs
ps
kill
nice
alias
sh,csh
functions Commands
Continued...
sleep 50&
jobs
fg %jobno
Ctrl Z
bg %jobno
ps
ps -aux
kill pid
Continued...
kill -9 pid
nice --20 ls
nice -19 ls
alias c=clear
9. E-mail & communication
Send/read e-mail can be run by other
Translate real name to account name for e-mail
mail
finger
functions Commands
Crontab Commands
crontab -e Edit your crontab file
crontab -l Display your crontab file
crontab -r Remove your crontab file
crontab -v Display the last time you edited your crontab
file
* * * * *
command to be exected
min(0-59) hour(0-23) dayofmonth(1-31) month(1-12)
day of week(0-6)
Continued...
Min hour day/month month day/week
30 0 1 1,6,12 *
0 20 * 10 1-5
0 0 1,10,15 * *
5,10 0 10 * 1
Continued...
Min hour day/month month day/week
30 18 * * * rm
/home/user/tmp/*
30 18 * * * rm
/home/user/tmp/*>/home/user/cronlogs/file
Miscellaneous
PS1=$USER@`uname -n`$
tr a-z A-Z <file
tr -s a-z A-Z <file
file /bin/sh
PATH=$PATH:/new/dir
cat datafile |sort
cat datafile|sort -n
cat datafile|sort -r
cat datafile|sort -rn
Miscellaneous
sort -t : /etc/passwd
sort -t : +2 -3 /etc/passwd
sort -t : +2rn -3 /etc/passwd
sort -t : +0 -1,+2 -3 file
ls l| sort +0.0 0.1
Miscellaneous
ls -l | tee file
cut -c 1-3,8-12 /etc/passwd
date |cut -c1-3
date |cut -c6-
cut -d : -f1,3 /etc/passwd
cut -d : -f1-5 /etc/passwd
paste file1 file2
Miscellaneous
cmp file1 file2
diff file1 file2
comm file1 file2
nohup ls -lR />out 2>errfile &
nohup ls -lR / & (o/p nohup.out)
Miscellaneous
eval echo hello
$pipe=|
ls $pipe wc -l
eval ls $pipe wc -l
eval echo \$$#
x=100
ptrx=x
eval \$$ptrx
eval $ptrx=50
echo $x
Miscellaneous
arg=12
eval echo \${$arg}
Miscellaneous
chown user1 file[s]
chown user1:user2 file[s]
chown user1: file[s]
chgrp user1 file[s]
Vi Editor
Why vi?
Vi (short form of visual editor)
there are Four editor in all most all version of
Unix
ed ,ex , vi & Emacs
the ed is the original editor that has been a
part of the Unix program since very
beginning
ed
The ed program is basically a line
editor.which mean that ed assigns lines
numbers to the lines in the file
ex
An improved version of ed is called ex, it
understand all the command of ed
it is a user friendliness with more informative
error messages.
Advantages of vi
Compare to ed or ex the vi text editor is head and
shoulders above them in almost every way.
It is a full screen editor rather than a line editor.
It allowed the user to view and edit the entire
document at the same time.
Creating and editing files became a lot easier &
thats the reason it became an instant hit with the
programmers.
Modes of operation
Command mode
Insert mode
Ex-command mode
Editing in vi
$vi letter
Vi Essential
-> get help
-> Scroll and move the cursor
-> Work with files
-> Work with windows
-> Work with buffers
Getting help
Get help with vi commands
Get a help on particular word
:help or f1
:help {word}
functions Commands
Getting help
Insert characters left of cursors
Insert characters right of cursors
Insert text at the end of line
Insert text at the beginning of line
insert new below the current cursor position
insert new above the current cursor position
i
a
A
I
o
O
functions Commands
Scrolling
Move down one line
Move up one line
Move left one character
Move right one character
Move left one word
Move right one word
Move to the beginning of the line
Move to the end of the line
Scroll down one page
Scroll up one page
Scroll down one window
Scroll up one window
Ctrl + e or down arrow or j
k or up arrow
Left arrow or h
Right arrow or l
b
w
Home
End
Ctrl + f
Ctrl + b
Ctrl + d
Ctrl +u
Functions Commands
Continued...
Move forward one sentence
Move backward one sentence
Move forward one para
Move backward one para
Move to find matching parenthesis
used matching braces( { })
)
(
}
{
%
Functions Commands
Copying & Pasting text
yw
yy
y$
y0
p
Functions Commands
Copy (yank) word from cursor position
Copy a line from cursor position
Copy line from cursor position to end
of line
Copy line from cursor position to
beginning of line
Pastes last copied buffer
Deleting text
dd
D
dw
x
X
Functions Commands
Delete the current line
Delete the reminder of the line
Delete the current word
Delete the current character
Delete the previous character
Undoing editing changes
u
U
Functions Commands
Undo the last change
restore the current line
Commands for quitting vi
:wq
:w filename
and :q
:w! filename
and :q
:q!
:q
Functions Commands
Writes the buffer to the file and quits vi
Writes the buffer to the filename (new)
and quits vi.
Overwrite the existing file filename with
the contents of the buffer and quits vi.
Quits vi whether or not changes made to
the buffer were written to a file. Does not
incorporate changes made to the buffer
since the last write (:w) command.
Quits vi if changes made to the buffer
were written to a file.
Various block commands
:nd
:m,n d
:n mo p
:m,n mo p
:m co p
:m,n co p
:m,n w filename
:m,n w >> filename
:r file name
:r !command
Functions Commands
Deletes nth line
Deletes line from m to n.
Moves line n after p
Moves lines m to n after line p
Copies lines after line p
Copies lines m to n after p
Writes lines m to n to a file
Appends lines m to n to a file
Reads the contents of the file file name
at current cursor position.
Executes shell command and output of
the command is read at the current cursor
position.
Continued...
G or L
nG
M
H
/pattern
?pattern
:s/pattern/replacement
:s/pattern/replacement/g
:m,n s/pattern/replacement/g
:.,$ s/pattern/replacement/g
:%s/pattern/replacement
:%s/patter/replacement/g
Functions Commands
Go to end line
Go to n th line
Go to Middle line
Go to top line
Forward search for pattern
Backward search for pattern
search & replace the first occurrence
Across global in a line
Across the lines specified by m and n
Current line to till the end.
Search even in sub string of string
Across global
Shell Script
What is a shell?
Shell is an command language
interpreter that executes commands
read from the standard input device
(keyboard) or from a file.
. Shell accepts your instruction or commands
in English (mostly) and if its a valid
command, it is pass to kernel.
Different types of shells?
Sh
BASH
CSH (C SHell)
KSH (Korn SHell)
TCSH
What is a shell script ?
"Shell Script is series of command
written in plain text file. Shell script is
just like batch file in DOS but have more
power than the DOS batch file."
Why to write a shell script ?
To automate some task of day today life.
System Administration part can be also
automated.
Execute your script as
syntax:
bash your-script-name
sh your-script-name
./your-script-name
Examples:
$ bash bar
$ sh bar
$ ./bar
First Script
vi first
#
# My first shell script
#
clear
echo "Knowledge is Power"
Metacharacters(wildcards)
Literal interpretation of the following character
Background processing
Command separator
Variable substitution
Match for a single characters
Match for zero or more characters
Match for one from a set of characters
Match for one not from the set of characters
Matches one character in the range from a to e
Execute commands in a sub shell
Execute commands in current shell
\
&
;
$
?
*
[abc]
[^abc]
[a-e]
(cmds)
{cmds}
Meaning Metachar
Continued...
touch abc abc1 abc122 abc123 file1
file1.txt file2 file2.txt none non noone
nothing nowhere now one
ls *
ls *.t*
echo a*
ls a?c?
ls ??
echo abc???
Continued...
ls abc[123]
ls abc[1-3]
ls [^f-z]???
ls abc12[23]
echo How are you?
echo How are you\?
echo When does this line\
> ever end\?
New Ksh Metacharacters
?matches zero or one occurrences of any pattern in
the parentheses.
*matches zero or more occurrences of any pattern in
parentheses.
+matches one or more occurrences of any pattern in
the parentheses.
@matches exactly one occurrence of any pattern in
the parentheses.
!matches all strings except those matched by any of
pattern in the parentheses.
abc?(2|9)1
abc*([0-9])
abc+([0-9])
no@(one|ne)
no!(thing|where)
Meaning Metachar
Continued...
ls abc?(1|2)
ls abc*([1-5])
ls abc+([0-5])
ls no@(thing|ne)
ls no!(one|w)
Variables
Local variables
Environment variables
Setting local variables
$round=world
$echo $round
$name=Raghavendra Raju
$echo $name
$x=
$echo $x
$file=$HOME
$echo $file
Predefined special
parameters
Number of positional parameters
Exit value of the last command
PID of the current shell
All the parameters
Process id of the last command executed in
background
References the name of the current shell script
Positional parameters 1 through 9
$#
$?
$$
$* or $@
$!
$0
$1....$9
Meaning Parameter
Continued...
$echo $$
$echo $round
$ sh (start a subshell)
$echo $$
$echo $round
$exit
$echo $$
$echo $round
Continued...
$export round
$echo $round
$ sh (start a subshell)
$echo $$
$echo $round
$exit
$echo $$
$echo $round
$set a b c d e f g h
$echo $# ,$*
Setting Read-only variables
$name=LINUX
$readonly name
$ echo $name
$unset name
$name=unix
Environment Variables
The search path for commands
HOME directory
Internal field separators,normally space,tab and
newline
The user's login name
Present working directory
Primary prompt string,dollar sign by default
Secondary prompt string,right angle bracket by
default
Shell name
Our OS type
No of lines
PATH
HOME
IFS
LOGNAME
PWD
PS1
PS2
SHELL
OSTYPE
LINES
Meaning ENV variable
Continued...
$env
$set
$HOME=/root/basic
$SHELL=/bin/sh
Variable Expansion modifiers
If variable is set and is nonnull,substitute its
value;otherwise,substitute word
If variable is set or is nonnull,substitute its
value;otherwise,set it to word.the value of the
variable is substituted permanently
If parameter is set and is nonnull.substitute
word;otherwise substitute nothing.
If variable is set and is nonnull,substitute its
value;otherwise,print word and exit from the shell.if
word is omitted the message Parameter null or not
set is printed.
{variable:-word}
{variable:=word}
{variable:+word}
{variable:?word}
Meaning VariableMod
Continued...(temp default values)
$linux=linus
$echo ${linux:-Torvalds}
$echo ${fruit:-apple}
$echo $fruit
$name=
$echo $name
$echo ${name:-Raju}
Continued...(permanent default
values)
$name=
$echo ${name:=Torvalds}
$echo $name
Continued...(assigning temp
alternate value)
$name=linus
$echo ${name:+Torvalds}
$echo $name
Continued...(Creating error
message based on default values)
$echo ${namex:?namex is undefined}
$echo ${y?}
About Quotes
"Double Quotes" - Anything enclose in double
quotes removed meaning of that characters (except \
and $).
'Single quotes' - Enclosed in single quotes remains
unchanged
`Back quote` - To execute command
' '
` `
Meaning Quotes
Continued...(Single Quotes)
$echo 'Learning ,BASIC LINUX'
$echo 'hi there
>how r u?'
$echo 'Hello, 'How R U?' '
Continued...(Single , double,back
Quotes)
$echo 'Learning ,BASIC LINUX'
$echo 'hi there
>how r u?'
$echo 'Hello, 'How R U?' '
$name=linux
$echo Hi i am learning $name!
$echo -e Hi now time is \n `date`
$set `date`
$echo $*
$echo $2 $6
Std I/O & Redirection
Redirect input
Redirect output
Append output
Redirect error
Redirect output to where error is going
Redirect error to where output is going
<
>
>>
2>
1>&2
2>&1
Meaning Symbols
Continued...(I/O redirection)
$tr '[A-E]' '[a-e]' <file
$ls>file
$date>>file
$cc prog.c 2>errfile
$find . -name \*.c -print >foundit 2>/dev/null
$find . -name \*.c -print >foundit 2>&1
$echo I/O Redirection 1>&2
$who|wc -l
Assignment
Write shell script to display
username,pwd,hostname,listing & time.
Reading User Input
Reads a line from std input & assign it to variable
Reads a line from std input to the first till white
space or new line and rest of the line into last
Till first white space into first similarly into second
and so on & rest of line into last variable
read answer
read first last
read first second
third
Meaning Format
Continued...(ex on read)
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"
Integer Arithmetic Using expr
command
Syntax:
expr op1 math-operator op2
$expr 1 + 2
$expr 1+4
$expr 5 + 9/3
$expr 5 * 4
$expr 5 \* 4
$expr 11 % 3
$num=2
$num=`expr $num + 1`
$echo $num
Command Line arguments for
shell
Shell arguments $0 to $9 are used to pass
command line arguments.
Example
#!/bin/sh
#
# Script that demos, command line args
#
echo "Total number of command line argument are
$#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"
Continued...
#!/bin/sh
#
#Script to demonstrate the use of special variable
# modifier and arguments
name=${1:?requires an argument}
echo Hello $name
Assignment
Write a shell script accepting positional parameter
as first,middle,last name.Display it & reset the
positional parameter.
Conditional Constructs & Flow
Control
If condition
If..else..fi
Nested if..else..fi
Multilevel if-then-else
Conditional expressions(Strings)
string1 is equal to string2
string1 is not equal to string2
string is not null
length of string is zero
length of string is non zero
string1 = string2
string1 != string2
string
-z string
-n string
Test For Test command
Conditional expressions(Integers)
int1 is equal to int2
int1 is not equal to int2
int1 is greater than int2
int1 is greater than or equal to int2
int1 is less than int2
int1 is less than or equal to int2
Examples:-
test -n $word or [ -n $word ]
test linux = unix or [ linux = unix ]
int1 -eq int2
int1 -ne int2
int1 -gt int2
int1 -ge int2
int1 -lt int2
int1 -le int2
Test For Test command
Conditional expressions(files)
block special file
character special file
directory existence
regular file existence and not a directory
set-group-id is set
sticky bit is set
file is a named pipe
file is readable
file is nonzero size
set-user-id bit is set
file is writable
file is executable
-b file
-c file
-d file
-f file
-g file
-k file
-p file
-r file
-s file
-u file
-w file
-x file
Test For Test command
Conditional expressions(logical)
Not operator
and operator
or operator
Examples:-
[ ! -f filename ]
[ -r filename -a -w filename ]
[ $g -lt 0 -o $g -gt 100 ]
!
-a
-o
Test For Test command
If condition
Syntax:
if condition
then
command1 if condition is true or if exit
status of condition is 0 (zero)
...
...
fi
Continued...
#!/bin/sh
#
#Script to print file
#
if cat $1
then
echo -e "\n\nFile $1, found and successfully
echoed"
fi
Continued...
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Usage: $0 mdays size"1>&2
exit 1
fi
if [ $1 -lt 0 -o $1 -gt 30 ]
then
echo "mdays is out of range"
exit 2
fi
Continued...
if [ $2 -le 4 ]
then
echo "size is out of range"
exit 3
fi
find / -xdev -mtime $1 -size +$2 -print
If..else..fi
Syntax:
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
else
if condition is not true then
execute all commands up to fi
fi
If..else..fi
#!/bin/sh
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
Nested if..else..fi
Syntax:
if condition
then
if condition
then
.....
..
do this
else
....
..
do this
fi
else
.....
do this
fi
Multilevel if-then-else
Syntax:
if condition ;then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
then
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
then
condition2 is zero (true - 0)
execute all commands up to elif statement
else
None of the above condtion,condtion1,condtion2 are
true (i.e. all of the above nonzero or false)
execute all commands up to fi
fi
Continued...
#!/bin/sh
# Script to test if..elif...else
#
if [ $1 -gt 0 ]; then
echo "$1 is positive"
elif [ $1 -lt 0 ]
then
echo "$1 is negative"
elif [ $1 -eq 0 ]
then
echo "$1 is zero"
else
echo "Opps! $1 is not number, give number"
fi
Continued...
#!/bin/bash
read file
if [ -d $file ]
then
echo "$file is a directory"
elif [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
echo "You have read write and execute
perm on $file."
fi
else
echo "$file is neither a file nor a directory."
fi
Assignment...
Write shell script for calculator.
Loops
For loop
Nested for
While loop
Case statement
Until loop
For loop
Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do
and done)
done
Example :
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Continued...
Syntax:
for (( var=0 ;var<= number; var++ ))
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do
and done)
done
Example :
for ((i=0;i<=100;i++))
do
echo "Welcome $i times"
done
While loop
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
done
Continued...
#!/bin/sh
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line
argument"
echo "Syntax : $0 number"
echo " Use to print multiplication table for given number"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
Case statement
Syntax:
case $variable-name in
pattern1) command
...
command;;
pattern2) command
...
command;;
patternN) command
...
command;;
*) command
...
command;;
esac
Case statement
# if no vehicle name is given
# i.e. -z $1 is defined and it is NULL
#
# if no command line arg
if [ -z $1 ]
then
rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
rental=$1
fi
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not get a $rental for you";;
esac
Until loop
until who |grep raju
do
sleep 5
done
mail raju<message
Continued...
hour=1
until [ $hour -gt 24 ]
do
case "$hour" in
[0-9]|1[0-1]) echo "Good morning!"
;;
12) echo "Lunch time"
;;
1[3-7]) echo "Siesta time"
;;
*) echo "Good night"
;;
esac
hour=`expr $hour + 1`
done
Shift command
$set `date`
$shift
$echo $*
$shift 3
$echo $*
#!/bin/bash
#Shift through command line arguments
while [ $# -gt 0 ]
do
echo $*
shift
done
Try running script with a b c d e as positional parameter
Break
break [n] where n is no of loops to break
While true; do
echo Are you ready to move on \?
read answer
if [ $answer = Y -o $answer = y ]
then
break
else
........commands............
fi
done
print Here we are
Continue Command
Continue [n]
#!/bin/bash
for name in ` cat mail_list`
do
if [ "$name" = "ken" ]; then
continue
else
mail $name <memo
fi
done
Continued...
#!/bin/bash
for month in jan feb mar apr may jun jul aug sep oct nov dec
do
for week in 1 2 3 4
do
echo -n Processing the month of $month.O.K?
read ans
if [ $ans = n -o -z $ans ]
then
continue 2
else
echo -n Process week $week of $month?
read ans
if [ $ans =n -o -z $ans ]
then
continue
else
Continued...
echo Now processing week $week of $month
sleep 1
#commands go here
echo done processing...
fi
fi
done
done
Running loops in the
background
for person in bob jim joe sam
do
mail $person <memo
done&
IFS and Loops
names=linus:robert:love:ken
OLDIFS=$IFS
IFS=:
for persons in $names
do
echo Hi $persons
done
IFS=$OLDIFS
set unix minix linux
for os in $*
do
echo Our os is $os
done
Functions
Format:-
function function_name(){commands; commands;}
$function dir(){echo Directories:; ls -l;}
Continued...
Increment() {
sum=`expr $1 + 1`
return $sum
}
echo -n The sum is
increment 5
echo $?
echo $sum
Trapping Signals
Format:
trap 'command; command' signal-number
trap 'rm tmp*;exit 1' 1 2 15
Fake signals
Signal what it does
DEBUG Executes trap commands after script commands
ERR Executes trap commands if any command in the
script returns a nonzero exit status
Exit or 0 Executes trap commands if shell exits
Signals
Signal Meaning
01 Hangup-you logged out while the process was still
running
02 Interrupt-you pressed the interrupt(break)key
sequence<ctrl-c>
03 Quit-you pressed the quit key sequence<ctrl-\>
09 kill signal: The most powerful(and risky) signal that can be
sent:Signal cannot be avoided or ignored
15 Terminal signal(default):Stop a process signal can be
handled by programs
Continued...
Default
Ignore
Create a handler fun to be called when the signal
arrives
Default:-
trap 2
Ignore:-
trap 1 2
Handler:-
trap 'echo echo will be executed when signal arrives' 2
Continued...
#!/bin /bash
#Script to illustrate the trap command and signals
#
trap 'echo Control-C will not terminate $0.' 2
trap 'echo Control -\ will not terminate $0.' 3
echo Enter any string after the prompt.
When you are ready to exit,type\stop\.
while true
do
echo -n Go ahead...>
read reply
if [ $reply = stop ]
then
break
fi
done
Traps in functions
trapper() {
echo In trapper
trap 'echo Caught in a trap!''' 2
}
while true
do
echo In the main script
trapper
echo Still in main
sleep 5
done
Debugging
Echo option (display each line of script after variable
substitutions and before execution)
Verbose option (display each line of script before
execution just as you typed it)
interpret but do not execute commands
sh -x script
sh -v script
sh -n script
Option Command
getopts
Ls -l-i-a or ls -lia or ls -li -a
format:-
getopts xy options
ex:-
while getopts la options
do
case $options in
l) echo You entered -l as an option
ls -$options;;
a) echo You entered -a as an option
ls -$options;;
\?) echo Only -l and -a are valid options
esac
done
Continued...(with arguments)
while getopts l:i: options
do
case $options in
l) echo "You have entered -l as an option and
argument is $OPTARG"
ls -$options $OPTARG;;
i)echo "You have entered -i as an option and argument
is $OPTARG"
ls -$options $OPTARG;;
\?) echo "Options are -l and -i";;
esac
done
Continued...(No of arguments)
While getopts abc: options
do
case $options in
a) echo "You have entered -a as an option " ;;
b)echo "You have entered -b as an option " ;;
c) echo You have entered -c as an option & $OPTARG
as argument;;
\?) echo "Options are -a -b and -c";;
esac
done
echo Total no of arguments is $OPTIND
The set command and option
Set -f The f option is turned on,disabling filename expansion
set +f The f option is turned off , enabling filename
expansion
Arrays
Arrays allows you to collect a list of words into one variable.
Arrays are created with the built-in function
declare -a arr
arr[0]=unix
arr[1]=linux
arr[2]=Rtlinux
print ${arr[0]}
print ${arr[1]}
print ${arr[2]}
print ${arr[*]}
print ${#arr[*]}
Continued...
typeset -i ints[4]
ints[0]=20
ints[1]=75
ints[2]=100
ints[3]=happy
print ${ints[0]}
print ${#ints[*]}
print ${ints[*]}
Continued...
Set -a fruit or declare -a fruit
fruit=(apples pears peaches)
print ${fruit[*]}
declare -ar fruits=(apples pears peaches)
fruits[1]=grapes
fruits=(apples [3]=pears peaches)
fruits[1]=grapes
print ${fruits[1]}
print ${fruits[3]}
print ${fruits[4]}
print ${fruits[*]}
unset fruits or unset ${fruits[0]}
Select
-Korn shell introduces a new loop, called the select
-loop which is used primarily for creating menus.
-The PS3 prompt is used to prompt the user for input;
by default, PS3 is #?.
Continued...
select var in wordlist
do
command(s)
done
Continued...
#!/bin/ksh
PS3=please choose one of the three os
select choice unix linux Rtlinux
do
case $choice in
unix)
print Power of character based os
break;;
linux)
print Power of GUI based os
break;;
Rtlinux)
print Power of embedded os
break;;
Continued...
*)
print $choice is not one of the
choices1>&2
break;;
print Try again
esac
done
Relational Test (Numeric
Expression)
$ a = 6
(( a == 6 ))
$ print $?
(( a != 5 )) (( a > 4 )) (( a >= 6 ))
(( a < 6 )) (( a <= 6 ))
Relational Test (String
Expression)
$ s=ab
[[ $s = ab ]]
[[ $s != ab ]]
[[ -n $s ]] ## not null
[[ -z $s ]] ## Null
File Expression test
file1 -nt file2 file1 newer than file2
file1 -ot file2 file1 older than file2
Test File Command
[[ -s file1 ]]
[[ file1 -ot file2 ]]
Logical Opeartors
! NOT Operator
&& AND Operator
|| OR Operator
Test Logical Operator
(( 7 > 5 && 6 > 5 ))
(( 7 == 5 || 6 < 7 ))
(( ! 5 > 3 ))
Arithmetic Operations in Korn
The Korn shell supports both integer
and floating-point arithmetic, but floating-
point arithmetic is available only on
versions of the Korn shell newer than
1988.
The typeset command is used for
assigning types.
Continued...
Commands Alias
typeset i integer
typeset -F Floating-point number assignment
typeset -i2 x Binary number
typeset -i8 x Octal number
typeset -i16 x Hexadecimal number
typeset -i
x=6
x=$x + 1
print $x
Arithmetic Operators and the let
Command
The let command is a Korn shell built-in
command that is used to perform integer arithmetic.
x=3
let x=x+1
print $x
While
while (( $# > 0 ))
do
print $*
shift
done
Continued...
while true; do
read answer? Are you ready to move on\?
if [[ $answer = [Yy]* ]]; then
break
else
... commands...
fi
done
print Here we are
Case study
Employee id
Employee Name
Designation
Salary
Phone No
Emp_id
Emp_name
Desig
Emp_sal
Emp_phone
Description Field Name
Main menu
1.Addition of employee
2. Deletion of employee
3. Retrieve the record of employee
4. Show all records
5. Exit
Your Choice?
Addition of employee
Enter the Employee id:
Employee Name:
Designation:
Salary:
Phone:
Are u sure?(y/n)
Data added
Want to exit?(y/n)
Deletion of employee
Enter the Employee id:
Are u sure?(y/n)
Your record deleted
Want to exit?(y/n)
Retrieve the record of employee
Enter Employee id:
Name Designation salary
Want to exit?(y/n)
All records sorted by employee id
Emp_name Designation Salary
Streamlined Editor
The sed command is a streamline,non interactive editor.It
allows you to perform the same kind of editing tasks used in
the vi and ex editors.
The sed editor processes a file one line at a time and sends its
output to the screen.Sed stores the line it is currently
processing in a temporary buffer, called a pattern space.once
action is done on this line it is sent to the screen(unless the
command was to delete the line or suppress its printing).
Continued...
Format: sed options 'command' file
options:-
-e is used for multiple edits at the command line
-f precedes a sed script file name
-n suppresses printing output
Datafile
northwest NW Ken 3.0 .98 3 34
western WE Ritchie 5.3 .97 5 23
southwest SW Thompson 2.7 .8 2 18
southern SO Dennis 5.1 .95 4 15
southeast SE Linus 4.0 .7 4 17
eastern EA Torvalds 4.4 .84 5 20
northeast NE Richard 5.1 .94 3 13
north NO Robert 4.5 .89 5 9
central CT Raju 5.7 .94 5 13
Sed commands
Printing:the p command
Deleting:the d command
Substitution:the s command
Printing
sed '/north/p' datafile
sed -n '/north/p' datafile
Deleting
sed '3d' datafile
sed '3,$d' datafile
sed '$d' datafile
sed '/north/d' datafile
Substitution
sed 's/west/north/g datafile
sed -n 's/^west/north/p' datafile
sed 's/[0-9][0-9]$/&.5/' datafile
sed 's/Linus/Torvalds/gp' datafile
sed 's/\(Lin\)us/\1ux/p' datafile
Range of selected lines:The
comma
sed -n '/west/,/east/p' datafile
sed -n '5,/^northeast/p' datafile
sed '/west/,/east/s/$/**VACA**/' datafile
sed -e '5,6d' -e 's/Ken/Kens/' datafile
Writing,Reading and Appending:
The w command
sed '/Ken/w newfile' datafile
The r command
sed '/Ken/r newfile' datafile
The a command
sed '/^north/a -->The North sales<--' datafile
Inserting and Next:
Inserting:-
sed '/eastern/i \\Line is inserted before pattern\\'
datafile
Next: The n commands
sed '/eastern/{n;s/NW/NorthWest/;}' datafile
Transform and Quit:
The y Command:
sed
'1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGH
IJKLMNOPQRSTUVWXYZ/' datafile
The q command:
sed '3q' datafile
sed '/Ken/{s/Ken/Kens/;q;}' datafile
Holding and Getting:The h , G &
g commands
sed -e '/northeast/h' -e '$G' datafile
sed -e '/WE/{h;d;}' -e '/CT/{G;}' datafile
sed -e '/northeast/h' -e '$g' datafile
sed -e '/WE/{h;d;}' -e '/CT/{g;}' datafile
Holding and Exchanging:The h ,
& x commands
sed -e '/Linus/h' -e '/Richard/x' datafile
Sed Script Examples
cat sedding1
#My first sed script
/Thompson/a\
Thompson is TOP salesperson for April!!\
Thompson is moving to the southern
district\ next month.\
CONGRATULATIONS!
/Robert/c\
************************\
ROBERT HAS RETIRED\
**********************
Continued...
1i\
EMPLOYEE DATABASE\
---------------------------------
$d
Sed -f sedding datafile
Sed Script Examples
cat sedding2
/eastern/,/central/{/^*$/d; /Torvalds/{h;d;} }
/Raju/g
s/EA/EASTERN/
sed -f sedding2 datafile
AWK
Awk is programming language used for
manipulating data and generating reports,it is an
indispensable tool used in shell script for
managing small databases.
Continued...
Format:-
awk 'pattern' filename
awk '{action}' filename
awk 'pattern{action}' filename
Patterns and Actions
Pattern consists of a regular expression,an
expression resulting in a true or false condition.
Actions are statements enclosed within curly
braces and separated by semicolons.
$cat>employees
Ken Thompson 1234 1/12/66 565455
Ritchie 1232 11/4/66 90898
Linus Torvalds 2212 1/2/67 635353
Robert 2321 22/3/65 133323
Continued...
awk '/Linus/' datafile
awk '{print $1}' datafile
awk '/Ken/{print $1,$2}' datafile
df | awk '$4>75000'
awk '/Ken/{print \t\tHave a nice day, ,$1,$2,
!}' datafile
date|awk '{print Month: $2 \nYear:,$6}'
Printf in awk
$echo UNIX| awk '{printf |%-15s|\n,$1}'
$echo UNIX|awk '{printf |%15s|\n,$1}'
$ awk '{printf The name is :%-15s ID is
%8s\n,$1,$2}' employees
The Match Operator
$awk '$1~/[Ii]tc/' employees
$awk '$1 !~/Thompson$/' employees
Awk commands in a file
$cat file
/^Ken/{print Hello Ken!}{print $1,$2,$3}
$awk -f file datafile
Records and Fields
Records:-Each line is called a record and is
terminated with a new line.
Record Separator:-ORS & RS is output and input
record separators.
The $0 variable:- An entire record can be
referenced.
The NR variable:-NR will hold no of record has
been processed.
Fields:-Each record consists of words called
fields(separated by white space).
Continued...
The NF variable:-The no of fields in a record.NF
gives no of fields and $NF will give value of
that field.
Field Separators(FS):-space is default field
separator.To change the value of FS at the
command line , the -F option is used.
Continued...
awk '{print $0}' datafile
awk '{print NR,$0}' datafile
awk '{print NR,$1,$2,$3}' datafile
awk '{print $0,NF}' datafile
awk -F: '/Ken/{print $1,$2}' datafile
awk -F[ :\t] '/Ken/{print $1,$2,$3}' datafile
awk '$3<2000' datafile
Awk commands in a script file
$vi file
/Ken/{print Ken's id is $3}
/Linus/{print NR,$0}
/^Robert/{print Hi Robert.$1has a salary of $
$4.}
$awk -f file datafile
$awk -F: -f file datafile
Relational operators
Less than
Less than or equal to
Equal to
Not equal to
Greater than or equal to
Greater than
Matched by regular expression
Not matched by regular expression
<
<=
==
!=
>=
>
~
!~
Meaning Operators
Continued...
$awk '$2==1232' datafile
$awk '$2>1232' datafile
$awk ' $1 ~/Ken/' datafile
$awk '$1 !~/Ken/' datafile
Arithmetic operators
Add
Subtract
Multiply
Divide
Modulus
Exponentiation
+
-
*
/
%
^
Meaning Operators
Continued...
$awk '$3*$4>500' file
Logical operators
Logical AND
Logical OR
NOT
&&
||
!
Meaning Operators
Continued...
$awk '$2>5 && $2<= 15' file
$awk '$3 == 100 || $4>50' file
$awk '!($2<100 && $3 <20)' file
BEGIN and END Patterns
The BEGIN pattern is followed by an action
block that is executed before awk processes any
lines from the input file.The BEGIN action is
often used to change the value of built-in
variables , OFS,RS,FS,and so forth to assign
initial values to user -defined variables.
END patterns do not match any input
lines.END patterns are handled after all lines of
input have been processed.
Continued...
$awk
'BEGIN{FS=:;OFS=\t;ORS=\n\n}{print
$1,$2,$3}' file
$awk 'BEGIN{print NEW YEAR}' file
$awk 'END{print The number of records is
NR}' file
Conditional Statements
$awk '{if ($6>50) print $1 Too high}' file
$awk '{if($6>20 && $6 <= 50){safe++;print
OK}}' file
$awk '{if($6>50) print $1 Too high;else print
Range is OK}' file
$awk '{if ($3>89 && $3 <101) Agrade++
elseif ($3 > 79) Bgrade++
elseif($3 >69 ) Cgrade++
elseif($3 >59) Dgrade++
else Fgrade++}' file
While and For Loop
$awk '{i=1; while(i <= NF){print NF,$i;i++}}'
file
$awk '{for( i=1; i <=NF; i++) print NF,$i}' file