Download - Unix Shell Scripting
![Page 1: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/1.jpg)
Unix Shell Scripting
![Page 2: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/2.jpg)
Instructor
Mustafa Qasim
Certified Ethical Hacker (CEH)
Oracle Certified Expert Solaris Security Administrator
Oracle Certified Professional Solaris System Administrator
Oracle Certified Associate Solaris 10 Operating System
Oracle Certified Associate MySQL 5.5/5
Cisco Certified Network Associate (CCNA)
![Page 3: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/3.jpg)
Catch Me
LinkedIn pk.linkedin.com/in/mustafaqasim
Google+ gplus.to/mustu
Twitter twitter.com/mustafaqasim
![Page 4: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/4.jpg)
Introduction● Name● Job Description● Professional Experience● Expectations from Unix Shell Scripting course
![Page 5: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/5.jpg)
Ideal Participants
Hardworking?
![Page 6: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/6.jpg)
Ideal Participants
Hardworking?ŊÖ
![Page 7: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/7.jpg)
Ideal Participants
LaaazzZZzzyyy....
![Page 8: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/8.jpg)
Let's Start...
![Page 9: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/9.jpg)
Unix● AT&T Employees at Bell Labs in 1969● Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy,
Michael Lesk and Joe Ossanna● Unix Family OS
– University of California, Berkeley's BSD– Solaris– HP-UX– AIX– Sequent and as well as Darwin
![Page 10: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/10.jpg)
GNU● GNU Not Unix● Richard Stallman, 1983● GNU General Public License (GPL)● GNU Compiler Collection (GCC), the GNU C library (glibc), and
GNU Core Utilities (coreutils),[1] but also the GNU Debugger (GDB), GNU Binary Utilities (binutils),[18] and the bash shell.
● GNU Hurd Kernel● Free Software Foundation (FSF)
![Page 11: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/11.jpg)
GNU/Linux● In April 1991, Linus Torvalds, a 21-year-old
student at the University of Helsinki, Finland started working on some simple ideas for an operating system
● Kernel vs OS
Linus Torvalds
![Page 12: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/12.jpg)
To Do
Make yourself familiar with Unix, Unix Derivatives, GNU, Linux and software license GPL and Free & Open Source Software (FOSS).
http://en.wikipedia.org/wiki/Unix
http://www.gnu.org/philosophy/philosophy.html
http://en.wikipedia.org/wiki/GNU
http://www.gnu.org/gnu/why-gnu-linux.html
http://en.wikipedia.org/wiki/Linux
![Page 13: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/13.jpg)
To Do
Make yourself familiar with Unix, Unix Derivatives, GNU, Linux and software license GPL and Free & Open Source Software (FOSS).
http://en.wikipedia.org/wiki/Unix
http://www.gnu.org/philosophy/philosophy.html
http://en.wikipedia.org/wiki/GNU
http://www.gnu.org/gnu/why-gnu-linux.html
http://en.wikipedia.org/wiki/Linux
![Page 14: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/14.jpg)
So, Where comes the Shell?
![Page 15: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/15.jpg)
Shell?
![Page 16: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/16.jpg)
Shell?
![Page 17: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/17.jpg)
Compiler vs Interpreted
Complete Program > Compiler > Executable
Program Fragment > Interpreter > Instant Output
![Page 18: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/18.jpg)
Compiler vs Interpreted (Contd.)● Compiled Languages
– C/C++– Pascal– FORTRAN
● Interpreted Languages– Unix Shell– Perl– Tcl/Tk
![Page 19: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/19.jpg)
Unix Shell● First shell for Version 6 Unix was written by Ken Thompson
(Bell Labs) in 1971● It was only Interactive Shell
![Page 20: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/20.jpg)
Bourne Shell● Created by Stephen Bourne at AT&T Bell Labs for V7 UNIX● Goals
– Interpreted Language– Scripting
● New Features– Control Flows, Loops, Variables
● Lacked Functions
![Page 21: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/21.jpg)
Bourne Shell (Contd.)● Bourne Shell Derivatives
– Korn Shell (ksh)– Almquist shell (ash)– Bourne Again Shell (Bash)
![Page 22: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/22.jpg)
C Shell (csh)● Bill Joy in 1978 for BSD● Create a scripting language similar to C● Prominent Feature
– Command History● Tenex Enhancements (tcsh)
– Command completion– Command line editing
![Page 23: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/23.jpg)
Korn Shell (ksh)● By David Korn● Backward compatibility with Bourne Shell (sh)● Derivative of Bourne Shell (sh)
![Page 24: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/24.jpg)
Bourne Again Shell (Bash)● GNU Project● Superset of sh and ksh
![Page 25: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/25.jpg)
POSIX● Institute of Electrical and Electronics Engineering (IEEE)● International Organization for Standardization (ISO)● POSIX Compliance● Bash, gawk
![Page 26: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/26.jpg)
Responsibilities of Shell
1. Reading input and parsing the command line
2. Evaluating special characters, such as wildcards and the history character
3. Setting up pipes, redirection, and background processing
4. Handling signals
5. Setting up programs for execution
![Page 27: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/27.jpg)
To Do● Perform Login and Logout on a Unix shell● Perform and understand the following commands
– ls, dir, touch, mkdir, cd, rm, mv, cat, less, more, which, pwd, man, info
● Use vi editor to create and edit files● Understand Unix File System hierarchy especially the
following directories– /bin, /sbin, /home, /root, /var
![Page 28: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/28.jpg)
Unix Startup● OS is booted and very first process is born
– Init, PID 1● OS authenticates the user and provides access to shell
– /bin/login● Data Streams are set
– Stdout, stderr, stdin● A process is forked to run Graphical Desktop
![Page 29: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/29.jpg)
Parsing Commands● Lexical Analysis
– The process of breaking the line up into tokens is called lexical analysis.
● Tokens● Command?
– Built-in command => Execute Internally– Executable command => Fork a Child Shell
● Forking Child Shel
![Page 30: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/30.jpg)
Types of Commands
1. Aliases
2. Keywords
3. Functions
4. Built-in commands
5. Executable programs
![Page 31: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/31.jpg)
Shell Processes● PID● ps● Linux: pstree● Solaris: ptree
![Page 32: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/32.jpg)
System Calls
Kernel
fork
exec
wait
exit }
![Page 33: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/33.jpg)
fork System Call● Parent/Child● Inheritance of environment, open files, real and user
identifications, umask, current working directory, and signals.
![Page 34: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/34.jpg)
fork System Call
![Page 35: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/35.jpg)
wait System Call● Waits for Child's PID & Exit Code● Zombie Process● Orphan Zombie Process
![Page 36: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/36.jpg)
exec System Call● exec call for executable● Executable becomes the child
![Page 37: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/37.jpg)
exit System Call● sigchild signal to parent● Exit code 0 - 255
![Page 38: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/38.jpg)
To Do● Perform and Understand the following commands
– top, ps, pgrep, pstree/ptree, kill● Learn to hunt a specific process and kill it● Learn to hunt a specific instance of a binary running multiple
times simultaneous
![Page 39: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/39.jpg)
Environment and Inheritance● UID, GID● Ownership● Permission● Variables● Working Directory
![Page 40: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/40.jpg)
User & Group IDs
$ id
uid=502(mustu) gid=502(mustu)
![Page 41: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/41.jpg)
File Creation Mask● Umask
777 (Directory) 666 (File)
–022 (umask value) –022 (umask value)
------- ---------
755 644
drwxr-xr-x -rw-r--r--
![Page 42: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/42.jpg)
Set Ownership & Permission● chown● chmod
![Page 43: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/43.jpg)
Variables● Local Variables● Environment Variables
– $env
![Page 44: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/44.jpg)
File Descriptor● 0 stdin● 1 stdout● 2 stderr
![Page 45: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/45.jpg)
I/O Redirection
![Page 46: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/46.jpg)
I/O Redirection
![Page 47: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/47.jpg)
I/O Redirection
![Page 48: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/48.jpg)
Pipe
![Page 49: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/49.jpg)
Signals
![Page 50: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/50.jpg)
To Do● Understand & Practice the following
– Bash Environmental variables– Check your current environment variables (hint: $env)– Check your user's UID & GID– Check a file's permission set for owner, group and others– Change a file/folder permission using both numerical and character format– Change a file/folder owner and group– Redirect stdout and stderr into a file– Pipe or Redirect output of programs
![Page 51: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/51.jpg)
Let's write a Bash script ;-)
![Page 52: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/52.jpg)
Writing Bash Script● Shebang
– #!/bin/bash● Comments
– #Getting input for value of age● Make script executable
– Chmod +x myscript.sh● Execute Script
– ./myscript.sh
![Page 53: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/53.jpg)
Simple Bash Script
#!/bin/bash
# This is a comment
echo “Khamoshi Ka Boycot”
![Page 54: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/54.jpg)
Use Variable
#!/bin/bash
name=”Telenor”
echo “$name Khamoshi Ka Boycot”
![Page 55: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/55.jpg)
Pass Arguments
./myscript ali 15 Lahore
#!/bin/bash
echo “Mr. $1 is $2 years old and belongs to $3”
![Page 56: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/56.jpg)
Get Input
#!/bin/bash
echo "What is your name?"
read uname
echo ” Welcome $uname”
![Page 57: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/57.jpg)
Arithmetic Operators
== Equal to
!= Not equal to
> Greater then
>= Greater then, equal to
< Less then
<= Less then, equalt o
![Page 58: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/58.jpg)
Logical Operators
&& and
|| or
! not
![Page 59: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/59.jpg)
Conditional Statements
if Construct:
if command
then
block of statements
fi
![Page 60: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/60.jpg)
Conditional Statements
if/else Construct:
if command
then
block of statements
else
block of statements
fi
![Page 61: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/61.jpg)
Conditional Statementsif/else if/else Construct:
if command
then
block of statements
elif command
then
block of statements
else
block of statements
fi
![Page 62: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/62.jpg)
Iterative Statements
for variable in word_list
do
block of statements
done
![Page 63: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/63.jpg)
Schedule Script● Cron
– crontan -l #List Exiting Crons– crontab -e #Edit Cron Scheduling File
![Page 64: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/64.jpg)
REMEMBER● Environment Variable Differ in Cron● Use the MAIL parameter to receive cron errors● Use the PATH parameter to set environment variables in cron● If your program needs to use neighbor files to run properly
then in cron script first change directory (cd) to your program's path then run the program.
![Page 65: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/65.jpg)
Tip :)
Your best buddy
http://www.corntab.com/
![Page 66: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/66.jpg)
Regular Expressions● Pattern of characters used to match the same characters in a
search● Enclosed by forward slash /telenor/
![Page 67: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/67.jpg)
Regular Expressions
Hi Asad,
We've a upcoming training on Unix shell Programming later this week. As you've to deal with programming tasks frequently I would highly recommend you to participate in this training. It will improve your programming skills and help you perform daily tasks efficiently.
Thanks
![Page 68: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/68.jpg)
Regular Expressions
:1,$s/programming/scripting/g
![Page 69: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/69.jpg)
Regular Expression Metacharacters
:1,$s/\<[Pp]rogramming\>/scripting/g
1,$ First Line to Last Line
s Substitute
\<, \> Beginning and End of Word
[Pp] Match one of the word
<[Pp]rogramming\> Programming OR programming
g Globally
![Page 70: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/70.jpg)
Regular Expression MetacharactersMeta Character
^
Function
Beginning of line anchor
Example
/^lahore/
What It Matched
Matches all lines beginning with lahore
![Page 71: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/71.jpg)
Regular Expression MetacharactersMeta Character
$
Function
End of line anchor
Example
/lahore$/
What It Matched
Matches all lines ending with lahore
![Page 72: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/72.jpg)
Regular Expression MetacharactersMeta Character
.
Function
Matches one character
Example
/la...e/
What It Matched
Matches all starting with la, followed by three characters, followed by e
![Page 73: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/73.jpg)
Regular Expression MetacharactersMeta Character
*
Function
Matches zero or more of the preceding characters
Example
/ *lahore/
What It Matched
Matches lines with zero or more spaces, followed by the pattern lahore
![Page 74: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/74.jpg)
Regular Expression MetacharactersMeta Character
[]
Function
Matches one in the set
Example
/[Ll]ahore/
What It Matched
Matches lines containing Lahore or lahore
![Page 75: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/75.jpg)
Regular Expression MetacharactersMeta Character
[x-y]
Function
Matches one character within a range in the set
Example
/[A-Z]ahore/
What It Matched
Matches letters from A through Z followed by ahore
![Page 76: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/76.jpg)
Regular Expression MetacharactersMeta Character
[^]
Function
Matches one character not in the set
Example
/[^A-Z]ahore/
What It Matched
Matches any character not in the range between A and Z
![Page 77: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/77.jpg)
Regular Expression MetacharactersMeta Character
\
Function
Used to escape a metacharacter
Example
/lahore\./
What It Matched
Matches lines containing lahore, followed by a literal period; Normally the period matches one of any character
![Page 78: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/78.jpg)
Regular Expression MetacharactersMeta Character
\<
Function
Beginning-of-word anchor
Example
/\<lahore/
What It Matched
Matches lines containing a word that begins with lahore (supported by vi and grep)
![Page 79: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/79.jpg)
Regular Expression Metacharacters Additional
Meta Character
\>
Function
Ending-of-word anchor
Example
/lahore\>/
What It Matched
Matches lines containing a word that ends with lahore (supported by vi and grep)
![Page 80: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/80.jpg)
Regular Expression Metacharacters Additional
Meta Character
\(..\)
Function
Tags match characters to be used later
Example
/\(love\)able \1er/
What It Matched
May use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, the pattern love is saved as tag 1, to be referenced later as \1. In this example, the search pattern consists of lovable followed by lover (supported by sed, vi, and grep)
![Page 81: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/81.jpg)
Regular Expression Metacharacters Additional
Meta Character
\(..\)
Function
Tags match characters to be used later
Example
/\(love\)able \1er/
What It Matched
May use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, the pattern love is saved as tag 1, to be referenced later as \1. In this example, the search pattern consists of lovable followed by lover (supported by sed, vi, and grep)
![Page 82: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/82.jpg)
Regular Expression Metacharacters Additional
Meta Character
x\{m\} or x\{m,\} or x\{m,n\}
Function
Repetition of character x, m times, at least m times, at least m and not more than n times
Example
o\{5,10\}
What It Matched
Matches if line contains between 5 and 10 consecutive occurrences of the letter o (supported by vi and grep)
![Page 83: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/83.jpg)
Sed● Streamlined, non-interactive editor
![Page 84: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/84.jpg)
How Sed Works
abc
Text File
abc
Sed Buffer
abc
Sed Output
![Page 85: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/85.jpg)
Awk● UNIX programming language used for manipulating data and
generating reports
![Page 86: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/86.jpg)
How Awk Works
Ahmad 33 LHRShakir 35 ISBQasim 28 KHI
Text File
Ahmad 33 LHR
Awk takes a line and put in an internal variable $0
Ahmad 33 LHR
Line is broken into fields separated by spaces and stored into internal numbered variables starting from $1
$0
$1 $2 $3
![Page 87: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/87.jpg)
How Awk Works
Ahmad 33 LHRShakir 35 ISBQasim 28 KHI
data.txt
#awk '{print $1, $3}' data.txt
Ahmad LHRShakir ISBQasim KHI
![Page 88: Unix Shell Scripting](https://reader033.vdocument.in/reader033/viewer/2022052301/5562f96dd8b42a62598b490c/html5/thumbnails/88.jpg)
To Do● Practice Regular Expressions with grep, sed and awk.