SlideShare a Scribd company logo
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 Camp
Troy Miles
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
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 Evening
Troy Miles
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
Kenneth Geisshirt
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developers
Andrew Eddie
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
Michelangelo van Dam
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
Lorna Mitchell
 
Syntax
SyntaxSyntax
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
Ivan Novikov
 
Intro to React
Intro to ReactIntro to React
Intro to React
Troy Miles
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02
Apoorvi Kapoor
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
jimbojsb
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
Marc Logghe
 
Gun make
Gun makeGun make
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
Bozhidar Boshnakov
 
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
Michael 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

Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 

Recently uploaded (20)

Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 

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