Shell Script Day1
Shell Script Day1
Shell Scripting
1
Course Materials
2
Day 1 Contents
• History
• Directory Hierarchy
• Redirection & Piping
• Processes
• Sed
• Awk
3
Introduction
4
Introduction
5
Unix Essential concepts
• Directory Hierarchy
• Processes
6
Directory Hierarchy
7
Directory Hierarchy
pwd
/export/home/user1/dir1
cd ..
pwd
/export/home/user1
cd ../../..
pwd
/
cd
cd dir1
pwd
/export/home/user1/dir1
cd ../dir2
pwd
/export/home/user1/dir2
8
Directory Hierarchy
• To return to home directory
cd
pwd
/export/home/user1
cd /export/home/user1
cd ~user1
pwd
/export/home/user1
cd ~/dir1/d1
9
Piping and Redirection
• Types of metacharacters
– Redirection metacharacters
• You can redirect input to and output from commands by
using redirection.
10
Piping and Redirection
11
Piping and Redirection
• Example
cat (Read from stdin)
First line (Read from stdin)
First line (Write to stdout)
What's going on? (Read from stdin)
What's going on? (Write to stdout)
Control-d (Read from stdin)
14
Piping and Redirection
ls –F /etc | grep “/”
X11/
acct/
apache/
apache2/
apoc/
15
Piping and Redirection
ls –F /etc | grep “/”
X11/
acct/
apache/
apache2/
apoc/
16
Processes
• Every program you run creates a process. For example
– Shell
– Command
– An application
• System starts processes called daemons which are
processes that run in the background and provide
services
• Every processes has a PID
• When a process creates another, the first is the parent of
the new process. The new process is called the child
process. Parent waits for her child to finish
17
Viewing a Process
• Process status command
ps option(s)
• Output
– PID
– TTY -> terminal identifier
– Execution time
– Command name
• Options
– -e: all system processes
– -f: full information
18
Viewing a Process
• Example
ps –ef
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 Oct 23 pts/6 0:18 -ksh
root 0 0 0 Oct 23 pts/6 0:30 –ps
• Where
– C: CPU utilization for scheduling
– STIME: Time process started
– TIME: Execution time for the process
19
Searching for a Specific Process
20
Sending a Signal to a Process
• A signal is a message sent to a process to
perform a certain action
• Signals are identified by a signal number and a
signal name, and has an associated action.
Signal # Signal Event Default
Name Response
15 SIGTERM Terminate Exit
9 SIGKILL KILL EXIT
21
Sending a Signal to a Process
• Using kill command
• kill [-signal] PIDs
• Examples
pgrep -l mail
215 sendmail
12047 dtmail
kill 12047
pgrep -l mail
215 sendmail
22
Sending a Signal to a Process
• Using pkill command
pkill [-signal] process_name
• Example
pkill -9 dtmail
23
Managing a Job
• A job is a process that shell can manage
• Three types of jobs
– Foreground jobs
– Background jobs
– Stopped jobs
• Running a job in the background
• Example
sleep 500 &
[1] 3028
[1] + Done
24
Managing a Job
• Listing Current Jobs
jobs
[1] + Running sleep 500&
• Bringing background job into foreground
fg %1
sleep 500
• Sending foreground job into background
sleep 500
^Z[1] + Stopped (SIGTSTP) sleep 500
25
Managing a Job
jobs
[1] + Stopped (SIGTSTP) sleep 500
bg %1
[1] sleep 500&
jobs
[1] + Running sleep 500
26
Managing a Job
• Stopping a background job
• jobs
• [1] + Running sleep 500&
• kill –STOP %1
• jobs
• [1] + Stopped (SIGSTOP) sleep 500&
• kill %1
• [1] + Terminated sleep 500&
• jobs
27
Useful Commands
split
• split a file into n line chunks
diff
• compare two files
line
• read the first line
28
Examples
$ split -10 /etc/passwd
$ls
• xaa xad
• xab xae
• xac xaf
29
The Streamlined Editor
• What is sed?
• How sed works
• Addressing
• Commands
• Examples
30
What is sed?
• It is a streamline, non-interactive editor.
31
How does sed Work?
• The sed editor process a file (input) one line at
a time and sends its output to the screen.
32
Addressing
• Addressing is used to determine which lines to
be edited.
• The addressing format can be
– Number
– Regular expression
– Both
* Number represents a line number.
33
Commands
• The sed commands tell sed what to do with the
line:
– Print it
– Remove it
– Change it
• The sed format
sed ‘command’ filename
34
Examples
• To print lines contain the pattern root
$sed ‘/root/p’ myfile
islam
maha
root
root
user
35
Examples
• To print lines from maha to root
$sed –n ‘/maha/,/root/p’ myfile
maha
root
36
Examples
• To delete the third line
$sed ‘3d’ myfile
islam
maha
user
37
Examples
• To delete lines from 1 to 3
$sed ‘1,3d’ myfile
user
38
Examples
• To substitute islam by iaskar
$sed ‘s/islam/iaskar/g’ myfile
iaskar
maha
root
user
39
Examples
• To issue multi command
$sed –e ‘2d’ –e
‘s/islam/iaskar/g’ myfile
iaskar
root
user
40
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
41
What is Awk?
42
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
43
Awk’s Format
• The awk program consists of
– awk command
– Program instructions enclosed in quotes
– Input file or default stdin.
44
Records and fields
• By default, each line is called a record and
terminated with a new line.
45
Records and fields
• Each record consists of words called fields
which by default separated by white spaces.
46
Examples
• To print the first field of the file, but as the default delimiter is white
space, you have to specify the delimiter
47
Examples
• To display the whole file (cat)
$awk ‘{print $0}’ /etc/passwd
root:x:0:1:Super-user:/:/sbin/sh
...
48
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
49
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
50
Conditional expressions
condition expression1 ? expression2 :expression3
condition expression1 ? expression2 :expression3
if (expression1)
expression2
else
expression3
if if(expression1)
(expression1){
statement; statement;...
expression2
}
else if (expression2){
else
statement; statement;...
}
expression3
else {
statement
}
51
Conditional expressions
condition expression1 ? expression2 :expression3
if (expression1){
if (expression1)
statement;
expression2 statement;...
} elseexpression3
else if (expression2){
if (expression1){
statement; statement;...
statement;
} statement;...
} else if (expression2){
statement; statement;...
else
} {
else {
statement
statement
} }
52
Relational Operators
Operator Meaning
< Less than
<= Less than and equal
== Equal to
!= Not equal to
>= Greater than and equal
> Greater than
~ Match regular expression
53
Loops
• while Loop
$awk –F: ’{i=1; while (i<NF)
{print NF,$i;i++}}’ /etc/passwd
• For Loop
$awk ‘{for ( i=1 ; i<NF; i++)
print NF,$i}’ /etc/passwd
54
Examples (cont.)
• The variable max value is set according to compression
between the first 2 fields:
$ awk ‘{if ($1>$2)
max=$1;
else
max=$2;
print max}’ testing
• Arithmetical Operations
$ awk ‘{if ($1*$2>100) print $0}’
testing
55
Examples (cont.)
• To display line 4 and 5 only
$awk ‘{if (NR==4 || NR==5)
print NR”:”$0
}’ /etc/passwd
56