SlideShare a Scribd company logo
1 of 25
Linux Shell Scripting
Vagrant
• Vagrant box add ‘USER/BOX
• vagrant box add jasonc/centos7 Will download iso(s) from vagrant public
• mkdir testbox01
• cd testbox01
• vagrant init jasonc/centos7
• vagrant up
• Vagrant ssh
• Vagrant halt
• Vagrant status
• etc
The Shell Script File
• Naming shell scripts and file extension
• Shell script file permissions
• Chmod 755 script
• Chmod +x script
Scripts
• Sharp bang
• #!/bin/bash
• Comments
• #
• Variables
• VAR=“value”
Creating Standard Output and Quoting
• The echo builtin
• Single versus double quotes
• “${VAR} gets expanded”
• ‘${VAR} not expand’
Getting Help for Shell Builtins
• Type: type [-afptP] name [name…]
• For each NAME, indicate how it would be interpreted if used as a command
name.
• Help: help [-s] [pattern …]
• Display helpful information about builtin commands
Getting Help for Linux Commands
• Man – format and display the on-line manual pages
Shell Variables
• The shell sets several variables automatically
•
HOSTNAME
RANDOM
UID
…
Command Substitution
• VAR=$(command)
• VAR=`command` # Old style
_________________________________________
• Echo “Output of command: $(command)”
The if statement
• if [[ COMMANDS ]]
then
COMMANDS
else
COMMANDS
fi
Exit Statuses
• 0 = true/ successful
• 1=false/unsuccessful
• Any non-zero exit status represents a failure
• Exit 1
• Echo ${?} # you can also use $?
• $? Will store the last exit status value.
Sanity Checking
if [[ “${UID}” –ne 0 ]]
then
echo ‘Please run as root.’ >&2
exit 1
fi
Obtaining Standard Input
• The read shell builtin.
• Read –p “A prompt: ” VARIABLE_
readonly VERBOSE=‘true’
Is like const where this variable cannot be reassigned any value.
Generating Random Data
• The $RANDOM shell variable
Echo ${RANDOM}
Seemingly random data using checksums.
date +%s%N | sha256sum | head -c8
Positional parameters
• Arguments vs Parameters
• $0 = Stores the script name.
• $1 = Stores the first argument
• $2= Stores the second argument
• $* = When used in quotes: “$1 $2 ….”
• $@ = When used in quotes “$1” “$2”…
• $# = total number of positions parameters
• For var in “$@”
• Do
• Echo “$var”
• Done
• _____________
• ./test.sh 1 2 ‘3 4’
• 1
• 2
• 3 4
• ______________
• Now change “$@” to “$*”
• 1
• 2
• 3
• 4
The for Loop
for VARIABLE in LIST
do
COMMANDS
done
The while Loop
while [[ COMMANDS ]]
do
COMMANDS
done
I/O Redirection - Pipes
• Sending STDOUT as STDIN.
Echo ${PWORD} | passwd –stdin ${USER_NAME}
• String manipulation & data munging with pipes.
echo ‘!@#$%^’ | fold –w1 | shuf | head -c1
File I/O Redirection
• COMMAND > /path/to/file
• COMMAND >> /path/to/file
• COMMAND < /path/to/file
• COMMAND 2> /path/to/file
• COMMAND &> /path/to/file
I/O Redirection
• COMMAND |& COMMAND: to redirect both standard output and standard
error to command (output)
• Head –n3 /etc/passwd /fakefile |& cat -n
• COMMAND &> FILE: to redirect both standard output and standard error to fil
• COMMAND < FILE
• COMMAND >&2
• COMMAND > /dev/null
File descriptors
• 0 stdin
• 1 stdout
• 2 stderr
Case statements
• Case “${1}” in
• start) echo ‘starting’ ;;
• Stop) echo ‘stopping’;;
• Status) echo ‘status’ ;;
• *)
• Echo ‘supply a valid option’ >&2
• Exit 1
• ;;
• esac
Functions
• Usage(){
• Echo “hello $1”
• }
• Usage “world”
Cut and Awk
• Cut takes single character as delimiter
however awk is more flexible
• $echo “abc def” | cut –f 2 –d “ ”
• Def
• ___________
• $echo “abc def” | cut –f 2 –d “ ”
• $echo “ abc def” | cut –f 2 –d “ ”
• abc
______________________
$ echo "abc def" | awk '{ print $2 }'
def
$ echo "abc def" | awk '{ print $2 }'
def
$ echo " abc def" | awk '{ print $2
}' def
• Bar
• Foo
• Baz
• ______________
• $cat test | sort
• $sort < test
• $sort test
• ___________
• Bar
• Baz
• Foo
Sort and Uniq
To sort numerically use $sort –n test
Uniq
• $cat namelist1.txt
• Jones, Bob
• Smith, Mary
• Babbage, Walter
• $cat namelist2.txt
• Jones, Bob
• Jones, Shawn
• Smith, Cathy
• ___________________
• $sort namelist1.txt namelist2.txt | uniq
• Babbage, Walter
• Jones, Bob
• Jones, Shawn
• Smith, Cathy
• Smith, Mary
-d on uniq will output the duplicate line
• $sort namelist1.txt namelist2.txt | uniq –d
• Jones,bob
-c on uniq will provide how many times it has
found each entry
• $sort namelist1.txt namelist2.txt | uniq –c
• 1 Babbage, Walter
• 2 Jones, Bob
• 1 Jones, Shawn
• 1 Smith, Cathy
• 1 Smith, Mary

More Related Content

Similar to Linux Shell Scripting.pptx

Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot CampTroy Miles
 
PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...
PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...
PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...Puppet
 
React Native Evening
React Native EveningReact Native Evening
React Native EveningTroy Miles
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Wen-Tien Chang
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developersAndrew Eddie
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Michelangelo van Dam
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Lie to Me: Bypassing Modern Web Application Firewalls
Lie to Me: Bypassing Modern Web Application FirewallsLie to Me: Bypassing Modern Web Application Firewalls
Lie to Me: Bypassing Modern Web Application FirewallsIvan Novikov
 
Intro to React
Intro to ReactIntro to React
Intro to ReactTroy Miles
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Apoorvi Kapoor
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redisjimbojsb
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl courseMarc Logghe
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN StackTroy Miles
 
SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators
SP24S053 Introduction to PowerShell for SharePoint Developers and AdministratorsSP24S053 Introduction to PowerShell for SharePoint Developers and Administrators
SP24S053 Introduction to PowerShell for SharePoint Developers and AdministratorsMichael Blumenthal (Microsoft MVP)
 

Similar to Linux Shell Scripting.pptx (20)

Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...
PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...
PuppetConf 2017: What's in a Name? Scaling ENC with DNS- Cameron Nicholson, A...
 
React Native Evening
React Native EveningReact Native Evening
React Native Evening
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developers
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Syntax
SyntaxSyntax
Syntax
 
Lie to Me: Bypassing Modern Web Application Firewalls
Lie to Me: Bypassing Modern Web Application FirewallsLie to Me: Bypassing Modern Web Application Firewalls
Lie to Me: Bypassing Modern Web Application Firewalls
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
Gun make
Gun makeGun make
Gun make
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators
SP24S053 Introduction to PowerShell for SharePoint Developers and AdministratorsSP24S053 Introduction to PowerShell for SharePoint Developers and Administrators
SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

Linux Shell Scripting.pptx

  • 2. Vagrant • Vagrant box add ‘USER/BOX • vagrant box add jasonc/centos7 Will download iso(s) from vagrant public • mkdir testbox01 • cd testbox01 • vagrant init jasonc/centos7 • vagrant up • Vagrant ssh • Vagrant halt • Vagrant status • etc
  • 3. The Shell Script File • Naming shell scripts and file extension • Shell script file permissions • Chmod 755 script • Chmod +x script
  • 4. Scripts • Sharp bang • #!/bin/bash • Comments • # • Variables • VAR=“value”
  • 5. Creating Standard Output and Quoting • The echo builtin • Single versus double quotes • “${VAR} gets expanded” • ‘${VAR} not expand’
  • 6. Getting Help for Shell Builtins • Type: type [-afptP] name [name…] • For each NAME, indicate how it would be interpreted if used as a command name. • Help: help [-s] [pattern …] • Display helpful information about builtin commands
  • 7. Getting Help for Linux Commands • Man – format and display the on-line manual pages
  • 8. Shell Variables • The shell sets several variables automatically • HOSTNAME RANDOM UID …
  • 9. Command Substitution • VAR=$(command) • VAR=`command` # Old style _________________________________________ • Echo “Output of command: $(command)”
  • 10. The if statement • if [[ COMMANDS ]] then COMMANDS else COMMANDS fi
  • 11. Exit Statuses • 0 = true/ successful • 1=false/unsuccessful • Any non-zero exit status represents a failure • Exit 1 • Echo ${?} # you can also use $? • $? Will store the last exit status value.
  • 12. Sanity Checking if [[ “${UID}” –ne 0 ]] then echo ‘Please run as root.’ >&2 exit 1 fi
  • 13. Obtaining Standard Input • The read shell builtin. • Read –p “A prompt: ” VARIABLE_ readonly VERBOSE=‘true’ Is like const where this variable cannot be reassigned any value.
  • 14. Generating Random Data • The $RANDOM shell variable Echo ${RANDOM} Seemingly random data using checksums. date +%s%N | sha256sum | head -c8
  • 15. Positional parameters • Arguments vs Parameters • $0 = Stores the script name. • $1 = Stores the first argument • $2= Stores the second argument • $* = When used in quotes: “$1 $2 ….” • $@ = When used in quotes “$1” “$2”… • $# = total number of positions parameters • For var in “$@” • Do • Echo “$var” • Done • _____________ • ./test.sh 1 2 ‘3 4’ • 1 • 2 • 3 4 • ______________ • Now change “$@” to “$*” • 1 • 2 • 3 • 4
  • 16. The for Loop for VARIABLE in LIST do COMMANDS done The while Loop while [[ COMMANDS ]] do COMMANDS done
  • 17. I/O Redirection - Pipes • Sending STDOUT as STDIN. Echo ${PWORD} | passwd –stdin ${USER_NAME} • String manipulation & data munging with pipes. echo ‘!@#$%^’ | fold –w1 | shuf | head -c1
  • 18. File I/O Redirection • COMMAND > /path/to/file • COMMAND >> /path/to/file • COMMAND < /path/to/file • COMMAND 2> /path/to/file • COMMAND &> /path/to/file
  • 19. I/O Redirection • COMMAND |& COMMAND: to redirect both standard output and standard error to command (output) • Head –n3 /etc/passwd /fakefile |& cat -n • COMMAND &> FILE: to redirect both standard output and standard error to fil • COMMAND < FILE • COMMAND >&2 • COMMAND > /dev/null
  • 20. File descriptors • 0 stdin • 1 stdout • 2 stderr
  • 21. Case statements • Case “${1}” in • start) echo ‘starting’ ;; • Stop) echo ‘stopping’;; • Status) echo ‘status’ ;; • *) • Echo ‘supply a valid option’ >&2 • Exit 1 • ;; • esac
  • 22. Functions • Usage(){ • Echo “hello $1” • } • Usage “world”
  • 23. Cut and Awk • Cut takes single character as delimiter however awk is more flexible • $echo “abc def” | cut –f 2 –d “ ” • Def • ___________ • $echo “abc def” | cut –f 2 –d “ ” • $echo “ abc def” | cut –f 2 –d “ ” • abc ______________________ $ echo "abc def" | awk '{ print $2 }' def $ echo "abc def" | awk '{ print $2 }' def $ echo " abc def" | awk '{ print $2 }' def
  • 24. • Bar • Foo • Baz • ______________ • $cat test | sort • $sort < test • $sort test • ___________ • Bar • Baz • Foo Sort and Uniq To sort numerically use $sort –n test Uniq • $cat namelist1.txt • Jones, Bob • Smith, Mary • Babbage, Walter • $cat namelist2.txt • Jones, Bob • Jones, Shawn • Smith, Cathy • ___________________ • $sort namelist1.txt namelist2.txt | uniq • Babbage, Walter • Jones, Bob • Jones, Shawn • Smith, Cathy • Smith, Mary
  • 25. -d on uniq will output the duplicate line • $sort namelist1.txt namelist2.txt | uniq –d • Jones,bob -c on uniq will provide how many times it has found each entry • $sort namelist1.txt namelist2.txt | uniq –c • 1 Babbage, Walter • 2 Jones, Bob • 1 Jones, Shawn • 1 Smith, Cathy • 1 Smith, Mary