SlideShare uma empresa Scribd logo
1 de 88
Baixar para ler offline
Unix Shell Scripting
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)
Catch Me

LinkedIn   pk.linkedin.com/in/mustafaqasim
Google+    gplus.to/mustu
Twitter    twitter.com/mustafaqasim
Introduction
●
    Name
●
    Job Description
●
    Professional Experience
●
    Expectations from Unix Shell Scripting course
Ideal Participants



Hardworking
              ?
Ideal Participants




        ?
Hardworking




       ŊÖ
Ideal Participants




     LaaazzZZzzyyy....
Let's Start...
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
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)
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
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
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
So, Where comes the Shell?
Shell?
Shell?
Compiler vs Interpreted

Complete Program > Compiler > Executable


Program Fragment > Interpreter > Instant Output
Compiler vs Interpreted (Contd.)
●
    Compiled Languages
    –   C/C++
    –   Pascal
    –   FORTRAN
●
    Interpreted Languages
    –   Unix Shell
    –   Perl
    –   Tcl/Tk
Unix Shell
●
    First shell for Version 6 Unix was written by Ken Thompson
    (Bell Labs) in 1971
●
    It was only Interactive Shell
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
Bourne Shell (Contd.)
●
    Bourne Shell Derivatives
    –   Korn Shell (ksh)
    –   Almquist shell (ash)
    –   Bourne Again Shell (Bash)
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
Korn Shell (ksh)
●
    By David Korn
●
    Backward compatibility with Bourne Shell (sh)
●
    Derivative of Bourne Shell (sh)
Bourne Again Shell (Bash)
●
    GNU Project
●
    Superset of sh and ksh
POSIX
●
    Institute of Electrical and Electronics Engineering (IEEE)
●
    International Organization for Standardization (ISO)
●
    POSIX Compliance
●
    Bash, gawk
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
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
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
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
Types of Commands
1. Aliases
2. Keywords
3. Functions
4. Built-in commands
5. Executable programs
Shell Processes
●
    PID
●
    ps
●
    Linux: pstree
●
    Solaris: ptree
System Calls




                    }
            fork
exit
                         Kernel
             exec

   wait
fork System Call
●
    Parent/Child
●
    Inheritance of environment, open files, real and user
    identifications, umask, current working directory, and
    signals.
fork System Call
wait System Call
●
    Waits for Child's PID & Exit Code
●
    Zombie Process
●
    Orphan Zombie Process
exec System Call
●
    exec call for executable
●
    Executable becomes the child
exit System Call
●
    sigchild signal to parent
●
    Exit code 0 - 255
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
Environment and Inheritance
●
    UID, GID
●
    Ownership
●
    Permission
●
    Variables
●
    Working Directory
User & Group IDs
$ id
uid=502(mustu) gid=502(mustu)
File Creation Mask
●
    Umask


         777 (Directory)         666 (File)
       –022 (umask value)      –022 (umask value)
        -------                ---------
        755                     644
       drwxr-xr-x              -rw-r--r--
Set Ownership & Permission
●
    chown
●
    chmod
Variables
●
    Local Variables
●
    Environment Variables
    –   $env
File Descriptor
●
    0 stdin
●
    1 stdout
●
    2 stderr
I/O Redirection
I/O Redirection
I/O Redirection
Pipe
Signals
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
Let's write a Bash script ;-)
Writing Bash Script
●
    Shebang
    –   #!/bin/bash
●
    Comments
    –   #Getting input for value of age
●
    Make script executable
    –   Chmod +x myscript.sh
●
    Execute Script
    –   ./myscript.sh
Simple Bash Script
#!/bin/bash


# This is a comment
echo “Khamoshi Ka Boycot”
Use Variable
#!/bin/bash


name=”Telenor”
echo “$name Khamoshi Ka Boycot”
Pass Arguments
./myscript ali 15 Lahore


#!/bin/bash


echo “Mr. $1 is $2 years old and belongs to $3”
Get Input
#!/bin/bash


echo "What is your name?"
read uname


echo ” Welcome $uname”
Arithmetic Operators

   ==           Equal to
   !=         Not equal to
   >          Greater then
   >=     Greater then, equal to
   <            Less then
   <=      Less then, equalt o
Logical Operators



  &&          and
  ||          or
  !           not
Conditional Statements
if Construct:


if command
then
       block of statements
fi
Conditional Statements
if/else Construct:


if command
then
       block of statements
else
       block of statements
fi
Conditional Statements
if/else if/else Construct:


if command
then
       block of statements
elif command
then
       block of statements
else
       block of statements
fi
Iterative Statements
for variable in word_list


do
       block of statements
done
Schedule Script
●
    Cron
    –   crontan -l   #List Exiting Crons
    –   crontab -e   #Edit Cron Scheduling File
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.
Tip :)
Your best buddy


http://www.corntab.com/
Regular Expressions
●
    Pattern of characters used to match the same characters in a
    search
●
    Enclosed by forward slash /telenor/
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
Regular Expressions
:1,$s/programming/scripting/g
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
Regular Expression Metacharacters
Meta Character
^


Function
Beginning of line anchor


Example
/^lahore/


What It Matched
Matches all lines beginning with lahore
Regular Expression Metacharacters
Meta Character
$


Function
End of line anchor


Example
/lahore$/


What It Matched
Matches all lines ending with lahore
Regular Expression Metacharacters
Meta Character
.


Function
Matches one character


Example
/la...e/


What It Matched
Matches all starting with la, followed by three characters, followed by e
Regular Expression Metacharacters
Meta 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
Regular Expression Metacharacters
Meta Character
[]


Function
Matches one in the set


Example
/[Ll]ahore/


What It Matched
Matches lines containing Lahore or lahore
Regular Expression Metacharacters
Meta 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
Regular Expression Metacharacters
Meta 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
Regular Expression Metacharacters
Meta 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
Regular Expression Metacharacters
Meta 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)
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)
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)
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)
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)
Sed
●
    Streamlined, non-interactive editor
How Sed Works



abc               abc           abc

Text File        Sed Buffer   Sed Output
Awk
●
    UNIX programming language used for manipulating data and
    generating reports
How Awk Works
                                                  $0
Ahmad       33   LHR
Shakir      35   ISB                     Ahmad          33   LHR
Qasim       28   KHI
                             Awk takes a line and put in an internal variable $0

    Text File




                               $1                  $2                   $3
                            Ahmad                  33                 LHR

                          Line is broken into fields separated by spaces and stored
                          into internal numbered variables starting from $1
How Awk Works

                                 Ahmad          33   LHR
                                 Shakir         35   ISB
                                 Qasim          28   KHI


                                     data.txt


#awk '{print $1, $3}' data.txt

Ahmad LHR
Shakir ISB
Qasim KHI
To Do
●
    Practice Regular Expressions with grep, sed and awk.

Mais conteúdo relacionado

Mais procurados

Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsManav Prasad
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic CommandsHanan Nmr
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Wave Digitech
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell scriptBhavesh Padharia
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Linux Administration
Linux AdministrationLinux Administration
Linux AdministrationHarish1983
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examplesabclearnn
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Scriptsbmguys
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script Amit Ghosh
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Useful Linux and Unix commands handbook
Useful Linux and Unix commands handbookUseful Linux and Unix commands handbook
Useful Linux and Unix commands handbookWave Digitech
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scriptingVIKAS TIWARI
 

Mais procurados (20)

Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
 
Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
Linux Administration
Linux AdministrationLinux Administration
Linux Administration
 
Linux
LinuxLinux
Linux
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examples
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Useful Linux and Unix commands handbook
Useful Linux and Unix commands handbookUseful Linux and Unix commands handbook
Useful Linux and Unix commands handbook
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 

Destaque

OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpen Gurukul
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scriptingvceder
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
Basic command ppt
Basic command pptBasic command ppt
Basic command pptRohit Kumar
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionrohit kumar
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsDr.Ravi
 
Linux command ppt
Linux command pptLinux command ppt
Linux command pptkalyanineve
 
Linux ppt
Linux pptLinux ppt
Linux pptlincy21
 

Destaque (8)

OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Basic command ppt
Basic command pptBasic command ppt
Basic command ppt
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 

Semelhante a Unix Shell Scripting

Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linuxQIANG XU
 
Tips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyTips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyOlivier Bourgeois
 
Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Chander Pandey
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Ahmed El-Arabawy
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.pptKiranMantri
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
Unix_commands_theory
Unix_commands_theoryUnix_commands_theory
Unix_commands_theoryNiti Patel
 
2018-Summer-Tutorial-Intro-to-Linux.pdf
2018-Summer-Tutorial-Intro-to-Linux.pdf2018-Summer-Tutorial-Intro-to-Linux.pdf
2018-Summer-Tutorial-Intro-to-Linux.pdfsanjeevkuraganti
 
Presentation for RHCE in linux
Presentation  for  RHCE in linux Presentation  for  RHCE in linux
Presentation for RHCE in linux Kuldeep Tiwari
 
Linux powerpoint
Linux powerpointLinux powerpoint
Linux powerpointbijanshr
 
Linux: Everyting-as-a-service
Linux: Everyting-as-a-serviceLinux: Everyting-as-a-service
Linux: Everyting-as-a-serviceRohit Sansiya
 
Check Your Privilege (Escalation)
Check Your Privilege (Escalation) Check Your Privilege (Escalation)
Check Your Privilege (Escalation) Bishop Fox
 
Linux Knowledge Transfer
Linux Knowledge TransferLinux Knowledge Transfer
Linux Knowledge TransferTapio Vaattanen
 

Semelhante a Unix Shell Scripting (20)

Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Adhocr T-dose 2012
Adhocr T-dose 2012Adhocr T-dose 2012
Adhocr T-dose 2012
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
Tips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyTips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development Efficiency
 
Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.ppt
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Unix_commands_theory
Unix_commands_theoryUnix_commands_theory
Unix_commands_theory
 
Linux
LinuxLinux
Linux
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Unix Ramblings
Unix RamblingsUnix Ramblings
Unix Ramblings
 
2018-Summer-Tutorial-Intro-to-Linux.pdf
2018-Summer-Tutorial-Intro-to-Linux.pdf2018-Summer-Tutorial-Intro-to-Linux.pdf
2018-Summer-Tutorial-Intro-to-Linux.pdf
 
Presentation for RHCE in linux
Presentation  for  RHCE in linux Presentation  for  RHCE in linux
Presentation for RHCE in linux
 
Linux powerpoint
Linux powerpointLinux powerpoint
Linux powerpoint
 
Linux: Everyting-as-a-service
Linux: Everyting-as-a-serviceLinux: Everyting-as-a-service
Linux: Everyting-as-a-service
 
redhat_by_Cbitss.ppt
redhat_by_Cbitss.pptredhat_by_Cbitss.ppt
redhat_by_Cbitss.ppt
 
Check Your Privilege (Escalation)
Check Your Privilege (Escalation) Check Your Privilege (Escalation)
Check Your Privilege (Escalation)
 
3. introduction of centos
3. introduction of centos3. introduction of centos
3. introduction of centos
 
Linux Knowledge Transfer
Linux Knowledge TransferLinux Knowledge Transfer
Linux Knowledge Transfer
 

Unix Shell Scripting

  • 2. 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)
  • 3. Catch Me LinkedIn pk.linkedin.com/in/mustafaqasim Google+ gplus.to/mustu Twitter twitter.com/mustafaqasim
  • 4. Introduction ● Name ● Job Description ● Professional Experience ● Expectations from Unix Shell Scripting course
  • 6. Ideal Participants ? Hardworking ŊÖ
  • 7. Ideal Participants LaaazzZZzzyyy....
  • 9. 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
  • 10. 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)
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. So, Where comes the Shell?
  • 17. Compiler vs Interpreted Complete Program > Compiler > Executable Program Fragment > Interpreter > Instant Output
  • 18. Compiler vs Interpreted (Contd.) ● Compiled Languages – C/C++ – Pascal – FORTRAN ● Interpreted Languages – Unix Shell – Perl – Tcl/Tk
  • 19. Unix Shell ● First shell for Version 6 Unix was written by Ken Thompson (Bell Labs) in 1971 ● It was only Interactive Shell
  • 20. 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
  • 21. Bourne Shell (Contd.) ● Bourne Shell Derivatives – Korn Shell (ksh) – Almquist shell (ash) – Bourne Again Shell (Bash)
  • 22. 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
  • 23. Korn Shell (ksh) ● By David Korn ● Backward compatibility with Bourne Shell (sh) ● Derivative of Bourne Shell (sh)
  • 24. Bourne Again Shell (Bash) ● GNU Project ● Superset of sh and ksh
  • 25. POSIX ● Institute of Electrical and Electronics Engineering (IEEE) ● International Organization for Standardization (ISO) ● POSIX Compliance ● Bash, gawk
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. Types of Commands 1. Aliases 2. Keywords 3. Functions 4. Built-in commands 5. Executable programs
  • 31. Shell Processes ● PID ● ps ● Linux: pstree ● Solaris: ptree
  • 32. System Calls } fork exit Kernel exec wait
  • 33. fork System Call ● Parent/Child ● Inheritance of environment, open files, real and user identifications, umask, current working directory, and signals.
  • 35. wait System Call ● Waits for Child's PID & Exit Code ● Zombie Process ● Orphan Zombie Process
  • 36. exec System Call ● exec call for executable ● Executable becomes the child
  • 37. exit System Call ● sigchild signal to parent ● Exit code 0 - 255
  • 38. 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
  • 39. Environment and Inheritance ● UID, GID ● Ownership ● Permission ● Variables ● Working Directory
  • 40. User & Group IDs $ id uid=502(mustu) gid=502(mustu)
  • 41. File Creation Mask ● Umask 777 (Directory) 666 (File) –022 (umask value) –022 (umask value) ------- --------- 755 644 drwxr-xr-x -rw-r--r--
  • 42. Set Ownership & Permission ● chown ● chmod
  • 43. Variables ● Local Variables ● Environment Variables – $env
  • 44. File Descriptor ● 0 stdin ● 1 stdout ● 2 stderr
  • 48. Pipe
  • 50. 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
  • 51. Let's write a Bash script ;-)
  • 52. Writing Bash Script ● Shebang – #!/bin/bash ● Comments – #Getting input for value of age ● Make script executable – Chmod +x myscript.sh ● Execute Script – ./myscript.sh
  • 53. Simple Bash Script #!/bin/bash # This is a comment echo “Khamoshi Ka Boycot”
  • 55. Pass Arguments ./myscript ali 15 Lahore #!/bin/bash echo “Mr. $1 is $2 years old and belongs to $3”
  • 56. Get Input #!/bin/bash echo "What is your name?" read uname echo ” Welcome $uname”
  • 57. Arithmetic Operators == Equal to != Not equal to > Greater then >= Greater then, equal to < Less then <= Less then, equalt o
  • 58. Logical Operators && and || or ! not
  • 59. Conditional Statements if Construct: if command then block of statements fi
  • 60. Conditional Statements if/else Construct: if command then block of statements else block of statements fi
  • 61. Conditional Statements if/else if/else Construct: if command then block of statements elif command then block of statements else block of statements fi
  • 62. Iterative Statements for variable in word_list do block of statements done
  • 63. Schedule Script ● Cron – crontan -l #List Exiting Crons – crontab -e #Edit Cron Scheduling File
  • 64. 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.
  • 65. Tip :) Your best buddy http://www.corntab.com/
  • 66. Regular Expressions ● Pattern of characters used to match the same characters in a search ● Enclosed by forward slash /telenor/
  • 67. 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
  • 69. 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
  • 70. Regular Expression Metacharacters Meta Character ^ Function Beginning of line anchor Example /^lahore/ What It Matched Matches all lines beginning with lahore
  • 71. Regular Expression Metacharacters Meta Character $ Function End of line anchor Example /lahore$/ What It Matched Matches all lines ending with lahore
  • 72. Regular Expression Metacharacters Meta Character . Function Matches one character Example /la...e/ What It Matched Matches all starting with la, followed by three characters, followed by e
  • 73. Regular Expression Metacharacters Meta 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
  • 74. Regular Expression Metacharacters Meta Character [] Function Matches one in the set Example /[Ll]ahore/ What It Matched Matches lines containing Lahore or lahore
  • 75. Regular Expression Metacharacters Meta 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
  • 76. Regular Expression Metacharacters Meta 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
  • 77. Regular Expression Metacharacters Meta 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
  • 78. Regular Expression Metacharacters Meta 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)
  • 79. 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)
  • 80. 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)
  • 81. 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)
  • 82. 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)
  • 83. Sed ● Streamlined, non-interactive editor
  • 84. How Sed Works abc abc abc Text File Sed Buffer Sed Output
  • 85. Awk ● UNIX programming language used for manipulating data and generating reports
  • 86. How Awk Works $0 Ahmad 33 LHR Shakir 35 ISB Ahmad 33 LHR Qasim 28 KHI Awk takes a line and put in an internal variable $0 Text File $1 $2 $3 Ahmad 33 LHR Line is broken into fields separated by spaces and stored into internal numbered variables starting from $1
  • 87. How Awk Works Ahmad 33 LHR Shakir 35 ISB Qasim 28 KHI data.txt #awk '{print $1, $3}' data.txt Ahmad LHR Shakir ISB Qasim KHI
  • 88. To Do ● Practice Regular Expressions with grep, sed and awk.