Tech Mahindra Limited confidential© Tech Mahindra Limited 2008
UNIX
Day 3
CONFIDENTIAL© Copyright 2008
2
Objectives
 At the end of this session, you will be able to:
 Understand the regular expressions
 Understand the grep family of commands
 Understand the UNIX Shell features and its environment
 Read/Write basic shell scripts using decision making constructs
CONFIDENTIAL© Copyright 2008
3
Agenda: Day 3
 Unix Shell Scripting (Contd. from Day 2)
 Introduction to sed
Tech Mahindra Limited confidential© Tech Mahindra Limited 2008
Shell Scripting (Contd. from Day 2)
CONFIDENTIAL© Copyright 2008
5
Recap ...
 Advanced Filters
 Regular Expressions
 Shell Overview
 Shell Scripting
 Statements in Shell Scripting
 Positional Parameters
 Command Aliases
 .profile files
 Command History
 Shell & Sub Shell
CONFIDENTIAL© Copyright 2008
6
Looping Statements
 Looping statements are used to perform the same set of
operations for more than one time till the specified
condition is true or false. There are 3 types of looping
statements:
1. while
2. until
3. for
CONFIDENTIAL© Copyright 2008
7
Conditional Looping Statements
1. while
 while statement repeatedly performs a set of instructions till the
control command returns a true exit status
 It is known as an entry-controlled loop
 Syntax:
while test <condition>
do
command1
command2
done
CONFIDENTIAL© Copyright 2008
8
Example of While Statement
ctrl=1
while test $ctrl –le 4
do
echo $ctrl
ctrl=`expr $ctrl + 1`
done
CONFIDENTIAL© Copyright 2008
9
Conditional Looping Statements
2. until
 The set of instructions is executed repeatedly as long as
the condition remains false
 The until statement complements the while statement
 Syntax:
until test <condition>
do
command1
command2
done
CONFIDENTIAL© Copyright 2008
10
Unconditional Looping Statements
3. for
 The loop is executed as many times as there are items in
the list. It doesn’t test condition but uses a list instead.
 Syntax:
for <identifier> in <val1 val2 …>
do
command1
command2
done
CONFIDENTIAL© Copyright 2008
11
Example of for Loop
for v_item in 10 20 30
do
echo $v_item
done
echo “Outside for loop”
CONFIDENTIAL© Copyright 2008
12
break, exit and continue Statements
 break
This statement will break from the inner loop and will move
the control to outer loop. Mostly used in conjunction with if
statement. If used in simple loop, will behave like exit.
 exit
This statement will terminate the execution of script and
control will come to command prompt
 continue
This statement will start the next iteration
CONFIDENTIAL© Copyright 2008
13
break, exit and continue Statements
clear
while test expn 1
do
stmt 1
while test expn 2
do
stmt 2
if test expn 3
then
stmt 3
break | continue | exit
fi
stmt 4
done
stmt 5
done
stmt 6
 break statement will pass the control to stmt 5
 continue statement will pass the control to immediate done and will
then proceed to continue second while loop
 exit statement will exit from the script
CONFIDENTIAL© Copyright 2008
14
here Document
 It allows a shell script to get its input from the same file that
holds the script
 Data used this way is said to be in a here document
 To create a ‘here document’, we use the << operator
 Any command using standard input can also have the input
from a ‘here document’
 The ‘here document’ symbol (<<) followed by the data, and
a delimiter (the termination string)
CONFIDENTIAL© Copyright 2008
15
here Document (Contd.)
 $ wc << EOF
>This is for testing the here document.
>It will give us the count of lines, words and characters
> till the terminator given after here document symbol,
> the terminator which is the
EOF
 We will get output of the command as number of lines,
words, characters till first occurrence of the EOF symbol in
the script
 We can use any string as a terminator
CONFIDENTIAL© Copyright 2008
16
Signals and Traps
 Signals in general tend to terminate a process
 trap command allows to handle the signal
Signal Key Combination Code
Value
Hang up Ctrl d 1
Interrupt Ctrl c 2
Terminate kill 15
CONFIDENTIAL© Copyright 2008
17
Signals and Traps (Contd.)
 Syntax: trap “<commands>” <signal-list>
 Example:
trap “ls –al” 2
 Lists the current directory contents when ctrl + c is pressed
 Note:
1. trap command, once it has been read in, lies in wait for those
signals mentioned in the signal list. When such a signal shows
up, the command is performed instead of the default
behavior.
2. trap command will trap the signals only during the execution
of the shell scripts.
CONFIDENTIAL© Copyright 2008
18
Shell Debugging
 Used to trace the script and correct the errors line by line
 Syntax
sh [options] <Name of the Shell Script>
Options Functionality
v
Prints the shell input lines as they are read by
the system
x Prints the commands and their arguments as
they are executed
CONFIDENTIAL© Copyright 2008
19
Shell Debugging (Contd.)
Example:
 (i) sh -v WhileDemo.sh
 (ii) sh –x WhileDemo.sh
CONFIDENTIAL© Copyright 2008
20
Arrays
 Unix provides one-dimensional array variables
 Any variable may be used as an array
 There is no maximum limit on the size of an array, nor any
requirement that members be indexed or assigned
contiguously
CONFIDENTIAL© Copyright 2008
21
Arrays (Contd.)
 Syntax to create an array:
 (i) arrayname[subscript]=value
OR
 (ii) arrayname=(values)
 In first method, the subscript is treated as an arithmetic
expression that must be evaluate to a number greater than
or equal to zero
 In second method, the values needs to be separated by a
space
CONFIDENTIAL© Copyright 2008
22
Arrays (Contd.)
 Element of an array may be referenced using
${array_name[subscript]}
 Referencing an array variable without a subscript is
equivalent to referencing the element zero
 The unset command is used to destroy complete array or
individual array element
unset array_name[subscript]
 Used to destroy the array element at the specified index
subscript
unset array_name
 Used to remove the entire array
CONFIDENTIAL© Copyright 2008
23
Arrays (Contd.)
 Example:
area[11]=23
area[13]=37
area[51]=UFOs
echo -n "area[11] = ${area[11]} "
echo -n "area[13] = ${area[13]} "
echo "Contents of area[51] are ${area[51]}.“
# Contents of uninitialized array variable
print blank (null
# variable)
echo -n "area[43] = ${area[43]} "
fruits=(Apple Mango Banana)
echo ${fruits[1]}
echo ${fruits[@]}
CONFIDENTIAL© Copyright 2008
24
Functions
 The bigger script can be divided into small modules/
functions, this will give us more readability and easy
maintenance.
 A function consists of a group of statements which are
executed together as a bunch.
 A shell function(s) must precede the statements that call it.
 The ‘return’ statement, when present, returns a value
representing the success or failure of a function. This is
optional.
CONFIDENTIAL© Copyright 2008
25
Functions
 The syntax for function definition:
function_name()
{
Command1
Command2
Command3
[ return value ]
}
 When function is invoked it executes all the commands
enclosed by the curly braces
 Call a function by its name only
CONFIDENTIAL© Copyright 2008
26
Functions
 Shell functions can be defined at a number of places:
 At the beginning of script
 In a separate file, so that other scripts can also use them
 In the .profile(.bash_profile for Linux), so that they are
available in the current session
 If we store the functions in a file called function_library, we
must include this file in the script using dot command as
follows:
. function_library
CONFIDENTIAL© Copyright 2008
27
Functions
 The positional parameters made available to shell scripts
externally are not available directly to a shell function
 We have to store these parameters in the shell variables and
then pass them to the function
 The parameters are passed on the function call statement
itself
 Parameters passed to function are accessed inside function
body in the same way we access positional parameters in a
shell script
CONFIDENTIAL© Copyright 2008
28
Functions
 Shell functions can change the variables of the shell, so one
has to be careful while using a variable name in a function
 To return a value from a function, we have to use the echo
command example:
funct_name()
{
---------
---------
echo result
}
 Now comes the function call
var=`funct_name para1 para2`
 The result will be stored in var variable
Tech Mahindra Limited confidential© Tech Mahindra Limited 2008
Introduction to sed
CONFIDENTIAL© Copyright 2008
30
Introduction to sed
 It is a non-interactive stream editor used to perform
repetitive tasks
 Stream oriented input flows through the program and is
directed to std output
sed can be used to :
 Search and replace globally to a single file or a group of files
 Replace text, delete lines, insert new text
 Specify all editing in one place
CONFIDENTIAL© Copyright 2008
31
The sed command
 Syntax :
• sed -option ‘[address action]’ [files/s]
• sed -option sed-script-file [data-file]
CONFIDENTIAL© Copyright 2008
32
sed
 Syntax to insert the text in a file
$ sed ‘<Line Number>i <Text1>
<Text2>
… ‘ <Input File> [ >
<Temporary File> ]
 Example:
$ sed ‘1i Tech Mahindra
ESG-T ‘ ITPGuidelines > Temp
 The above command is used to concatenate the two lines of
inserted text and prints all lines to standard output and then
we redirect it to a temporary file
 Note: Option i stands for insertion
CONFIDENTIAL© Copyright 2008
33
sed (Contd.)
 Syntax to copy the contents of the file to another file
$ sed ‘ /<Search Pattern1>/w <New File Name1>
/<Search Pattern2>/w
<New File Name2>
… ‘ <Input File>
 Example:
i. $ sed ‘/ITP/w Itp_Participant
/CEP/w Cep_Participant’
Training
 The above command is used to used to search for the
patterns ITP and CEP from the file named Training and
copies all the lines which matches the pattern ITP into the
file named ITP_Participant and similarly for the pattern CEP
CONFIDENTIAL© Copyright 2008
34
sed (Contd.)
 Example:
ii. $ sed –n ‘1,5w SplitDemo1
6,15w SplitDemo2’ Training
 The above command is used to split the content of the
training file into two different files one will have the lines
from 1 to 5 and the other will have the line numbers from 6
to 15.
 Note: Option n is required with w command to suppress
printing all the lines on the standard output device
CONFIDENTIAL© Copyright 2008
35
sed (Contd.)
 Syntax to find and replace a particular pattern in a file
$ sed ‘s /<Find Pattern1>/<Replace String>/’
<Input File>
 Example:
i. sed 's/Esgt/Education Services Group/‘ Techm
 Reads the content of the file Techm and replaces Esgt with
Education Services Group
CONFIDENTIAL© Copyright 2008
36
sed (Contd.)
 Syntax to find and replace multiple patterns in the file
ii. sed 's/Th/, Techm/; s/Chn/, Chennai/' Techm
(or)
sed -e 's/Th/, Techm/' -e 's/Chn/,
Chennai' Techm
 Note: Option ‘s’ stands for substitute
CONFIDENTIAL© Copyright 2008
37
sed (Contd.)
 Syntax to delete lines in a file
$ sed ‘/ <Search Pattern 1>/d ‘
<Input File> [ > <Temporary File> ]
 Example
$ sed ‘/ITP/d ‘ Training > Temp
 The above command is used to select all the lines from the
file named Training except ITP and redirects it to the file
named Temp
 Note:
 Option d stands for deletion.
 Option n not to be used with d.
CONFIDENTIAL© Copyright 2008
38
sed (Contd.)
 Syntax to invoke the script files
$ sed –f <Script File> <Input File>
 Example:
$ sed –f sedscript.sed Training
 The above command is used to read the contents of the file
named sedscript.sed and apply those commands to the file
named Training and retrieve the contents.
 Note:Option f directs to take its instructions from the
file
CONFIDENTIAL© Copyright 2008
39
Summary
In this session, we have covered:
 Unix Shell Scripting (Contd. from Day 2)
 Introduction to sed
Tech Mahindra Limited confidential© Tech Mahindra Limited 2008
Thank You

Unix day3 v1.3

  • 1.
    Tech Mahindra Limitedconfidential© Tech Mahindra Limited 2008 UNIX Day 3
  • 2.
    CONFIDENTIAL© Copyright 2008 2 Objectives At the end of this session, you will be able to:  Understand the regular expressions  Understand the grep family of commands  Understand the UNIX Shell features and its environment  Read/Write basic shell scripts using decision making constructs
  • 3.
    CONFIDENTIAL© Copyright 2008 3 Agenda:Day 3  Unix Shell Scripting (Contd. from Day 2)  Introduction to sed
  • 4.
    Tech Mahindra Limitedconfidential© Tech Mahindra Limited 2008 Shell Scripting (Contd. from Day 2)
  • 5.
    CONFIDENTIAL© Copyright 2008 5 Recap...  Advanced Filters  Regular Expressions  Shell Overview  Shell Scripting  Statements in Shell Scripting  Positional Parameters  Command Aliases  .profile files  Command History  Shell & Sub Shell
  • 6.
    CONFIDENTIAL© Copyright 2008 6 LoopingStatements  Looping statements are used to perform the same set of operations for more than one time till the specified condition is true or false. There are 3 types of looping statements: 1. while 2. until 3. for
  • 7.
    CONFIDENTIAL© Copyright 2008 7 ConditionalLooping Statements 1. while  while statement repeatedly performs a set of instructions till the control command returns a true exit status  It is known as an entry-controlled loop  Syntax: while test <condition> do command1 command2 done
  • 8.
    CONFIDENTIAL© Copyright 2008 8 Exampleof While Statement ctrl=1 while test $ctrl –le 4 do echo $ctrl ctrl=`expr $ctrl + 1` done
  • 9.
    CONFIDENTIAL© Copyright 2008 9 ConditionalLooping Statements 2. until  The set of instructions is executed repeatedly as long as the condition remains false  The until statement complements the while statement  Syntax: until test <condition> do command1 command2 done
  • 10.
    CONFIDENTIAL© Copyright 2008 10 UnconditionalLooping Statements 3. for  The loop is executed as many times as there are items in the list. It doesn’t test condition but uses a list instead.  Syntax: for <identifier> in <val1 val2 …> do command1 command2 done
  • 11.
    CONFIDENTIAL© Copyright 2008 11 Exampleof for Loop for v_item in 10 20 30 do echo $v_item done echo “Outside for loop”
  • 12.
    CONFIDENTIAL© Copyright 2008 12 break,exit and continue Statements  break This statement will break from the inner loop and will move the control to outer loop. Mostly used in conjunction with if statement. If used in simple loop, will behave like exit.  exit This statement will terminate the execution of script and control will come to command prompt  continue This statement will start the next iteration
  • 13.
    CONFIDENTIAL© Copyright 2008 13 break,exit and continue Statements clear while test expn 1 do stmt 1 while test expn 2 do stmt 2 if test expn 3 then stmt 3 break | continue | exit fi stmt 4 done stmt 5 done stmt 6  break statement will pass the control to stmt 5  continue statement will pass the control to immediate done and will then proceed to continue second while loop  exit statement will exit from the script
  • 14.
    CONFIDENTIAL© Copyright 2008 14 hereDocument  It allows a shell script to get its input from the same file that holds the script  Data used this way is said to be in a here document  To create a ‘here document’, we use the << operator  Any command using standard input can also have the input from a ‘here document’  The ‘here document’ symbol (<<) followed by the data, and a delimiter (the termination string)
  • 15.
    CONFIDENTIAL© Copyright 2008 15 hereDocument (Contd.)  $ wc << EOF >This is for testing the here document. >It will give us the count of lines, words and characters > till the terminator given after here document symbol, > the terminator which is the EOF  We will get output of the command as number of lines, words, characters till first occurrence of the EOF symbol in the script  We can use any string as a terminator
  • 16.
    CONFIDENTIAL© Copyright 2008 16 Signalsand Traps  Signals in general tend to terminate a process  trap command allows to handle the signal Signal Key Combination Code Value Hang up Ctrl d 1 Interrupt Ctrl c 2 Terminate kill 15
  • 17.
    CONFIDENTIAL© Copyright 2008 17 Signalsand Traps (Contd.)  Syntax: trap “<commands>” <signal-list>  Example: trap “ls –al” 2  Lists the current directory contents when ctrl + c is pressed  Note: 1. trap command, once it has been read in, lies in wait for those signals mentioned in the signal list. When such a signal shows up, the command is performed instead of the default behavior. 2. trap command will trap the signals only during the execution of the shell scripts.
  • 18.
    CONFIDENTIAL© Copyright 2008 18 ShellDebugging  Used to trace the script and correct the errors line by line  Syntax sh [options] <Name of the Shell Script> Options Functionality v Prints the shell input lines as they are read by the system x Prints the commands and their arguments as they are executed
  • 19.
    CONFIDENTIAL© Copyright 2008 19 ShellDebugging (Contd.) Example:  (i) sh -v WhileDemo.sh  (ii) sh –x WhileDemo.sh
  • 20.
    CONFIDENTIAL© Copyright 2008 20 Arrays Unix provides one-dimensional array variables  Any variable may be used as an array  There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously
  • 21.
    CONFIDENTIAL© Copyright 2008 21 Arrays(Contd.)  Syntax to create an array:  (i) arrayname[subscript]=value OR  (ii) arrayname=(values)  In first method, the subscript is treated as an arithmetic expression that must be evaluate to a number greater than or equal to zero  In second method, the values needs to be separated by a space
  • 22.
    CONFIDENTIAL© Copyright 2008 22 Arrays(Contd.)  Element of an array may be referenced using ${array_name[subscript]}  Referencing an array variable without a subscript is equivalent to referencing the element zero  The unset command is used to destroy complete array or individual array element unset array_name[subscript]  Used to destroy the array element at the specified index subscript unset array_name  Used to remove the entire array
  • 23.
    CONFIDENTIAL© Copyright 2008 23 Arrays(Contd.)  Example: area[11]=23 area[13]=37 area[51]=UFOs echo -n "area[11] = ${area[11]} " echo -n "area[13] = ${area[13]} " echo "Contents of area[51] are ${area[51]}.“ # Contents of uninitialized array variable print blank (null # variable) echo -n "area[43] = ${area[43]} " fruits=(Apple Mango Banana) echo ${fruits[1]} echo ${fruits[@]}
  • 24.
    CONFIDENTIAL© Copyright 2008 24 Functions The bigger script can be divided into small modules/ functions, this will give us more readability and easy maintenance.  A function consists of a group of statements which are executed together as a bunch.  A shell function(s) must precede the statements that call it.  The ‘return’ statement, when present, returns a value representing the success or failure of a function. This is optional.
  • 25.
    CONFIDENTIAL© Copyright 2008 25 Functions The syntax for function definition: function_name() { Command1 Command2 Command3 [ return value ] }  When function is invoked it executes all the commands enclosed by the curly braces  Call a function by its name only
  • 26.
    CONFIDENTIAL© Copyright 2008 26 Functions Shell functions can be defined at a number of places:  At the beginning of script  In a separate file, so that other scripts can also use them  In the .profile(.bash_profile for Linux), so that they are available in the current session  If we store the functions in a file called function_library, we must include this file in the script using dot command as follows: . function_library
  • 27.
    CONFIDENTIAL© Copyright 2008 27 Functions The positional parameters made available to shell scripts externally are not available directly to a shell function  We have to store these parameters in the shell variables and then pass them to the function  The parameters are passed on the function call statement itself  Parameters passed to function are accessed inside function body in the same way we access positional parameters in a shell script
  • 28.
    CONFIDENTIAL© Copyright 2008 28 Functions Shell functions can change the variables of the shell, so one has to be careful while using a variable name in a function  To return a value from a function, we have to use the echo command example: funct_name() { --------- --------- echo result }  Now comes the function call var=`funct_name para1 para2`  The result will be stored in var variable
  • 29.
    Tech Mahindra Limitedconfidential© Tech Mahindra Limited 2008 Introduction to sed
  • 30.
    CONFIDENTIAL© Copyright 2008 30 Introductionto sed  It is a non-interactive stream editor used to perform repetitive tasks  Stream oriented input flows through the program and is directed to std output sed can be used to :  Search and replace globally to a single file or a group of files  Replace text, delete lines, insert new text  Specify all editing in one place
  • 31.
    CONFIDENTIAL© Copyright 2008 31 Thesed command  Syntax : • sed -option ‘[address action]’ [files/s] • sed -option sed-script-file [data-file]
  • 32.
    CONFIDENTIAL© Copyright 2008 32 sed Syntax to insert the text in a file $ sed ‘<Line Number>i <Text1> <Text2> … ‘ <Input File> [ > <Temporary File> ]  Example: $ sed ‘1i Tech Mahindra ESG-T ‘ ITPGuidelines > Temp  The above command is used to concatenate the two lines of inserted text and prints all lines to standard output and then we redirect it to a temporary file  Note: Option i stands for insertion
  • 33.
    CONFIDENTIAL© Copyright 2008 33 sed(Contd.)  Syntax to copy the contents of the file to another file $ sed ‘ /<Search Pattern1>/w <New File Name1> /<Search Pattern2>/w <New File Name2> … ‘ <Input File>  Example: i. $ sed ‘/ITP/w Itp_Participant /CEP/w Cep_Participant’ Training  The above command is used to used to search for the patterns ITP and CEP from the file named Training and copies all the lines which matches the pattern ITP into the file named ITP_Participant and similarly for the pattern CEP
  • 34.
    CONFIDENTIAL© Copyright 2008 34 sed(Contd.)  Example: ii. $ sed –n ‘1,5w SplitDemo1 6,15w SplitDemo2’ Training  The above command is used to split the content of the training file into two different files one will have the lines from 1 to 5 and the other will have the line numbers from 6 to 15.  Note: Option n is required with w command to suppress printing all the lines on the standard output device
  • 35.
    CONFIDENTIAL© Copyright 2008 35 sed(Contd.)  Syntax to find and replace a particular pattern in a file $ sed ‘s /<Find Pattern1>/<Replace String>/’ <Input File>  Example: i. sed 's/Esgt/Education Services Group/‘ Techm  Reads the content of the file Techm and replaces Esgt with Education Services Group
  • 36.
    CONFIDENTIAL© Copyright 2008 36 sed(Contd.)  Syntax to find and replace multiple patterns in the file ii. sed 's/Th/, Techm/; s/Chn/, Chennai/' Techm (or) sed -e 's/Th/, Techm/' -e 's/Chn/, Chennai' Techm  Note: Option ‘s’ stands for substitute
  • 37.
    CONFIDENTIAL© Copyright 2008 37 sed(Contd.)  Syntax to delete lines in a file $ sed ‘/ <Search Pattern 1>/d ‘ <Input File> [ > <Temporary File> ]  Example $ sed ‘/ITP/d ‘ Training > Temp  The above command is used to select all the lines from the file named Training except ITP and redirects it to the file named Temp  Note:  Option d stands for deletion.  Option n not to be used with d.
  • 38.
    CONFIDENTIAL© Copyright 2008 38 sed(Contd.)  Syntax to invoke the script files $ sed –f <Script File> <Input File>  Example: $ sed –f sedscript.sed Training  The above command is used to read the contents of the file named sedscript.sed and apply those commands to the file named Training and retrieve the contents.  Note:Option f directs to take its instructions from the file
  • 39.
    CONFIDENTIAL© Copyright 2008 39 Summary Inthis session, we have covered:  Unix Shell Scripting (Contd. from Day 2)  Introduction to sed
  • 40.
    Tech Mahindra Limitedconfidential© Tech Mahindra Limited 2008 Thank You

Editor's Notes

  • #4 &amp;lt;number&amp;gt;
  • #16 mail $1&amp;lt;&amp;lt;EOF “Wishing u happy birthday” EOF
  • #17 Example: passing hang-up signal to a running process. 1. Create prg1.sh with the following code ------------------------------------------------------------- trap &amp;quot;echo Hi&amp;quot; 1 while true do echo “hello world” done ---------------------------------------- 2. Run it as “sh prg1.sh &amp;” and note the pid 3. send the SIGHUP signal i.e. signal number 1 to the same id process with this command and see kill -1 PID