Shell Script Lec 1
Shell Script Lec 1
Scripting
Presented by:
Moataz Ahmed
Quick
Revision
Unix concept
• Directory Hierarchy
• Processes
3
Basic UNIX commands
• cd
• ls
• pwd
• mkdir
• rmdir
• find
• rm
• cat
• more
• head
• tail
• grep
• cut
• sort
• ….
4
Useful Commands
• split
• split a file into n line chunks
• diff
• compare two files
• line
• read the first line
• uniq
• select unique lines or rows
5
Useful Commands (cont.)
• join
• joining two files, matching row by row
• paste
• paste multiple files, column by column
• tr
• transform character by character
6
Examples
$ split -10 /etc/passwd
$ls
xaa xad
xab xae
xac xaf
$ line < /etc/passwd
root:x:0:1:Super-User:/:/sbin/sh
$ uniq names uniq-names
$ paste names uniq-names
sherine sherine
sherine root
root mdesouky
mdesouky
mdeouky
7
Streamlined
Editor sed
and
The Awk Utility
The Streamlined Editor
• What is sed?
• How sed works
• Addressing
• Commands
• Examples
9
What is sed?
10
How does sed Work?
11
Addressing
• Addressing is used to determine
which lines to be edited.
• The addressing format can be
• Number
• Regular expression
• Both
12
Commands
• The sed commands tell sed what to
do with the line:
• Print it
• Remove it
• Change it
• ….
• The sed format
sed ‘command’ filename
13
Examples
• To print lines contain the pattern root
$sed ‘/root/p’ myfile
sherine
maha
root
root
mdesouky
• To suppresses the default behavior of the
sed
$sed –n ‘/root/p’ myfile
root
14
Examples (cont.)
15
Examples (cont.)
16
Examples (cont.)
17
Examples (cont.)
• To print lines from 2 to the line that
begins with mdes
$sed –n ‘2,/^mdes/p’ myfile
maha
root
mdesouky
18
The awk Utility
• What is awk?
• What does awk stands for?
• The awk’s format
• Records and Fields
• Examples
• BEGIN Pattern
• END Pattern
• Conditional Expressions
• Loops
• Examples
19
What is Awk?
• awk is a programming language
used for manipulating data and
generating reports.
20
What does Awk stands for?
• awk stands for the first initials in the
last names of each authors of the
language, Alfred Aho, Peter
Weinberger, and Brian Kernighan
21
Awk’s Format
• The awk program consists of
• awk command
• Program instructions enclosed in quotes
• Input file or default stdin.
22
Records and fields
• By default, each line is called a record
and terminated with a new line.
• Record separators are by default
carriage return, stored in a built-in
variables ORS and RS.
• The $0 variable is the entire record.
• The NR variable is the record number.
23
Records and fields (cont.)
24
Examples
• To print the first filed of the file as the default
delimiter is white space, you have to specify the
delimiter
$awk –F: ‘{print $1}’ /etc/passwd
root
daemon
sherine
…
$awk –F: ‘{print “Logname:”,$1}’ /etc/passwd
Logname:root
Logname:daemon
Logname:sherine
…
25
Examples (cont.)
26
BEGIN Pattern
• BEGIN Pattern is followed by an action block that
is executed before awk process any line from the
input file.
• Example
$awk ‘BEGIN{FS=“:”; ORS=“\n\n”} {print $1,$2,$3}’
myfile
27
END Pattern
• END patterns are handled after all
lines of input have been processed.
• Example:
• To print the number of lines in a file
$awk ‘END { print NR } ’ testfile
28
Conditional expressions
condition expression1 ? expression2 :expression3
if (expression1)
expression2
else
expression3
if (expression1){
statement; statement;...
}
else if (expression2){
statement; statement;...
}
else {
statement
}
29
Relational Operators
Operator Meaning
< Less than
30
Loops
• while Loop
•Example
$awk –F: ’{i=1; while (i<NF) {print
NF,$i;i++}}’ /etc/passwd
• For Loop
•Example
$awk ‘{for ( i=1 ; i<NF; i++) print
NF,$i}’ /etc/passwd
31
Examples (cont.)
• Arithmetical Operations
$ awk ‘{if ($1*$2>100) print $0}’ testing
32
Examples (cont.)
33