SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
BASHing at the CLI
Chris Tankersley
@dragonmantank
Midwest PHP 2018
1
Midwest PHP 2018
Commandline Scripting
2
Midwest PHP 2018
What is Bash?
●
Bourne Shell
– Introduced with Unix System 7 in 1977
– Still present on most systems as `sh`
●
Bourne Again Shell
– 1989 as part of the GNU Project
– Also took parts from C Shell (`csh`) and Korn Shell (`ksh`)
– Added syntactic sugar on top of the Bourne Shell
3
Midwest PHP 2018
Run directly or through scripts
$ for FILE in `ls`; do echo ${FILE}; done
#!/bin/bash
for FILE in `ls`; do
echo ${FILE}
done
4
Midwest PHP 2018
Why use Bash?
●
System Scripts
●
Quick prototyping
●
Ultra-portable scripts*
5
Midwest PHP 2018
Anatomy of a script
#!/bin/bash
// Logic goes here
echo “Hello World”
exit 0
6
Midwest PHP 2018
Variables
7
Midwest PHP 2018
Declaring Variables
MY_VAR=”something”
MY_NUMBER=1
LOG_DIR=”/var/log”
8
Midwest PHP 2018
Using Variables
MY_VAR=”something”
MY_NUMBER=1
LOG_DIR=”/var/log”
APP_LOG=${LOG_DIR}/app.log
echo $MY_VAR
echo ${MY_VAR}
9
Midwest PHP 2018
Declaring Arrays
FILES=(
‘app.log’
‘app.err’
‘access.log’
)
echo ${FILES[0]}
10
Midwest PHP 2018
Declaring Hashes
declare -AANIMALS
ANIMALS[“moo”]=”cow”
ANIMALS[“dog”]=”woof”
echo ${ANIMALS[moo]}
11
Midwest PHP 2018
Storing Output
FILES=`ls /var/log`
FILE_DATA=$(cat /var/log/app.log)
12
Midwest PHP 2018
Built In Variables
$BASH = Location of the bash executable
$FUNCNAME = Name of the current function
$HOME = Home directory of current user
$PATH = Search path for binaries
$PWD = Current working directory
$TMOUT = Time before a script times out
$UID = Current User ID
13
Midwest PHP 2018
Built In Variables
$0, $1, … = Positional Parameters
$# = Number of arguments
“$@” = All arguments as quote delimited words
$! = PID of the last job run in background
$? = Exit code of the last command
$$ = PID of the script itself
http://tldp.org/LDP/abs/html/internalvariables.html
14
Midwest PHP 2018
Flow Control
15
Midwest PHP 2018
Numerical Comparison Operators
-eq - Equal To
-ne - Not Equal To
-gt, > - Greater Than
-ge, >= - Greater Than or Equal
-lt, < - Less Than
-le, <= - Less Than or Equal
16
Midwest PHP 2018
String Comparison Operators
==, = - Equal To
!= - Not Equal
< - Less than ASCII order
> - Greater than ASCII order
-n - Not null
-z - Is Null, 0 length
17
Midwest PHP 2018
Logic Operators
&&, -a – And
||, -o – Or
&& and || short circuit
18
Midwest PHP 2018
If/Else Statements
if [[ ${MY_VAR} -eq 0 ]]; then
echo “MY_VAR equals 0”
elif [[ ${MY_VAR} -eq 1 ]]; then
Echo “MY_VAR equals 1”
else
echo “MY_VAR equals ${MY_VAR}”
fi
19
Midwest PHP 2018
Case Statements
case ${MY_VAR} in
1) echo “1”;;
2) echo “2”;;
*) echo “Some other number”;;
esac
Don’t put strings in quotes
20
Midwest PHP 2018
For Each Loops
FILES=(
‘app.log’
‘app.err’
)
for FILENAME in ${FILES[@]}; do
echo ${FILENAME}
done
21
Midwest PHP 2018
For Each Loops
for FILENAME in `ls /var/log`; do
echo ${FILENAME}
done
22
Midwest PHP 2018
While Loops
COUNTER=0
while [[ ${COUNTER} -lt 10 ]];
do
((COUNTER++))
done
echo ${COUNTER}
23
Midwest PHP 2018
Functions
24
Midwest PHP 2018
Basic Functions
function hello_world()
{
echo “Hello Midwest PHP!”
}
hello_world
25
Midwest PHP 2018
Function Arguments
function app_log()
{
echo “[$(date)] ${1}”
}
app_log “This is my log message”
26
Midwest PHP 2018
Function Return Values
function check_zero()
{
if [[ ${1} -eq 0 ]]; then
return 1
else
return 0
fi
}
IS_ZERO=`check_zero 1`
IS_ZERO=`check_zero 0`
27
Midwest PHP 2018
Variable Scope
MY_VAR=”Midwest PHP”
function hello_world()
{
local MY_VAR=”Hello World”
echo ${MY_VAR}
}
echo ${MY_VAR}
hello_world
28
Midwest PHP 2018
Putting it all together
29
Midwest PHP 2018
Google Shell Style Guide
https://google.github.io/styleguide/shell.xml
30
Midwest PHP 2018
Structuring Larger Scripts
●
Define functions at the top
●
Break functions out into their own files
– source filename.sh
31
Midwest PHP 2018
Function-Only Scripts
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# This script is being invoked as a script
echo 'This script is not intended to be run standalone. Please use with
the system-micro.sh script'
exit 1
fi
32
Midwest PHP 2018
Know when to use a higher level language
33
Midwest PHP 2018
Thank You!
• Software Engineer for InQuest
• Author of “Docker for Developers”
• https://leanpub.com/dockerfordevs
• Co-Host of “Jerks Talk Games”
• http://jerkstalkgames.com
• http://ctankersley.com
• chris@ctankersley.com
• @dragonmantank
34

Weitere ähnliche Inhalte

Was ist angesagt?

Impala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for HadoopImpala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for HadoopAll Things Open
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDayfcofdezc
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
Unit 7 standard i o
Unit 7 standard i oUnit 7 standard i o
Unit 7 standard i oroot_fibo
 
multi-line record grep
multi-line record grepmulti-line record grep
multi-line record grepRyoichi KATO
 
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...GeeksLab Odessa
 
Drupal 8's Multilingual APIs: Building for the Entire World
Drupal 8's Multilingual APIs: Building for the Entire WorldDrupal 8's Multilingual APIs: Building for the Entire World
Drupal 8's Multilingual APIs: Building for the Entire WorldChristian López Espínola
 
Code Smart - Makefile (special release)
Code Smart - Makefile (special release)Code Smart - Makefile (special release)
Code Smart - Makefile (special release)Yu-Chuan Pi
 
Introduction To Distributed Erlang
Introduction To Distributed ErlangIntroduction To Distributed Erlang
Introduction To Distributed ErlangDavid Dossot
 
4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...PROIDEA
 
Assic 16th Lecture
Assic 16th LectureAssic 16th Lecture
Assic 16th Lecturebabak danyal
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1Dr.Ravi
 

Was ist angesagt? (18)

Jose dossantos.doc
Jose dossantos.docJose dossantos.doc
Jose dossantos.doc
 
Impala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for HadoopImpala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for Hadoop
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDay
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Unit 7 standard i o
Unit 7 standard i oUnit 7 standard i o
Unit 7 standard i o
 
runtimestack
runtimestackruntimestack
runtimestack
 
multi-line record grep
multi-line record grepmulti-line record grep
multi-line record grep
 
Tres Gemas De Ruby
Tres Gemas De RubyTres Gemas De Ruby
Tres Gemas De Ruby
 
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
 
Drupal 8's Multilingual APIs: Building for the Entire World
Drupal 8's Multilingual APIs: Building for the Entire WorldDrupal 8's Multilingual APIs: Building for the Entire World
Drupal 8's Multilingual APIs: Building for the Entire World
 
Code Smart - Makefile (special release)
Code Smart - Makefile (special release)Code Smart - Makefile (special release)
Code Smart - Makefile (special release)
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Hd10
Hd10Hd10
Hd10
 
Introduction To Distributed Erlang
Introduction To Distributed ErlangIntroduction To Distributed Erlang
Introduction To Distributed Erlang
 
4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...
 
Assic 16th Lecture
Assic 16th LectureAssic 16th Lecture
Assic 16th Lecture
 
Gore: Go REPL
Gore: Go REPLGore: Go REPL
Gore: Go REPL
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 

Ähnlich wie BASHing at the CLI - Midwest PHP 2018

CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPICombell NV
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit universityMandakini Kumari
 
SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesSymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesjulien pauli
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7julien pauli
 
Symfony live 2017_php7_performances
Symfony live 2017_php7_performancesSymfony live 2017_php7_performances
Symfony live 2017_php7_performancesjulien pauli
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPwahidullah mudaser
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25Techvilla
 
Exakat for PHP : smart code reviewing engine
Exakat for PHP : smart code reviewing engineExakat for PHP : smart code reviewing engine
Exakat for PHP : smart code reviewing engineDamien Seguy
 
Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009PHPBelgium
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting languageElmer Concepcion Jr.
 
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VMInspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VMmhelmich
 

Ähnlich wie BASHing at the CLI - Midwest PHP 2018 (20)

CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPI
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Lecture8
Lecture8Lecture8
Lecture8
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesSymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performances
 
PHP
PHPPHP
PHP
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
Symfony live 2017_php7_performances
Symfony live 2017_php7_performancesSymfony live 2017_php7_performances
Symfony live 2017_php7_performances
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
PHP
PHPPHP
PHP
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25
 
php basics
php basicsphp basics
php basics
 
Exakat for PHP : smart code reviewing engine
Exakat for PHP : smart code reviewing engineExakat for PHP : smart code reviewing engine
Exakat for PHP : smart code reviewing engine
 
Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009
 
Day1
Day1Day1
Day1
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VMInspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
 
Prersentation
PrersentationPrersentation
Prersentation
 

Mehr von Chris Tankersley

Docker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersDocker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersChris Tankersley
 
Bend time to your will with git
Bend time to your will with gitBend time to your will with git
Bend time to your will with gitChris Tankersley
 
Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Chris Tankersley
 
Dead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIDead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIChris Tankersley
 
Killer Docker Workflows for Development
Killer Docker Workflows for DevelopmentKiller Docker Workflows for Development
Killer Docker Workflows for DevelopmentChris Tankersley
 
Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018Chris Tankersley
 
You Were Lied To About Optimization
You Were Lied To About OptimizationYou Were Lied To About Optimization
You Were Lied To About OptimizationChris Tankersley
 
Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017Chris Tankersley
 
Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017Chris Tankersley
 
Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017Chris Tankersley
 
Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017Chris Tankersley
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017Chris Tankersley
 
From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017Chris Tankersley
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPChris Tankersley
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Chris Tankersley
 
How We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open SourceHow We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open SourceChris Tankersley
 
Docker for PHP Developers - ZendCon 2016
Docker for PHP Developers - ZendCon 2016Docker for PHP Developers - ZendCon 2016
Docker for PHP Developers - ZendCon 2016Chris Tankersley
 

Mehr von Chris Tankersley (20)

Docker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersDocker is Dead: Long Live Containers
Docker is Dead: Long Live Containers
 
Bend time to your will with git
Bend time to your will with gitBend time to your will with git
Bend time to your will with git
 
Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)
 
Dead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIDead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPI
 
Killer Docker Workflows for Development
Killer Docker Workflows for DevelopmentKiller Docker Workflows for Development
Killer Docker Workflows for Development
 
You Got Async in my PHP!
You Got Async in my PHP!You Got Async in my PHP!
You Got Async in my PHP!
 
Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018
 
Docker for Developers
Docker for DevelopersDocker for Developers
Docker for Developers
 
They are Watching You
They are Watching YouThey are Watching You
They are Watching You
 
You Were Lied To About Optimization
You Were Lied To About OptimizationYou Were Lied To About Optimization
You Were Lied To About Optimization
 
Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017
 
Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017
 
Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017
 
Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
 
From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHP
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
 
How We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open SourceHow We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open Source
 
Docker for PHP Developers - ZendCon 2016
Docker for PHP Developers - ZendCon 2016Docker for PHP Developers - ZendCon 2016
Docker for PHP Developers - ZendCon 2016
 

Kürzlich hochgeladen

How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 

Kürzlich hochgeladen (20)

How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 

BASHing at the CLI - Midwest PHP 2018

  • 1. BASHing at the CLI Chris Tankersley @dragonmantank Midwest PHP 2018 1
  • 3. Midwest PHP 2018 What is Bash? ● Bourne Shell – Introduced with Unix System 7 in 1977 – Still present on most systems as `sh` ● Bourne Again Shell – 1989 as part of the GNU Project – Also took parts from C Shell (`csh`) and Korn Shell (`ksh`) – Added syntactic sugar on top of the Bourne Shell 3
  • 4. Midwest PHP 2018 Run directly or through scripts $ for FILE in `ls`; do echo ${FILE}; done #!/bin/bash for FILE in `ls`; do echo ${FILE} done 4
  • 5. Midwest PHP 2018 Why use Bash? ● System Scripts ● Quick prototyping ● Ultra-portable scripts* 5
  • 6. Midwest PHP 2018 Anatomy of a script #!/bin/bash // Logic goes here echo “Hello World” exit 0 6
  • 8. Midwest PHP 2018 Declaring Variables MY_VAR=”something” MY_NUMBER=1 LOG_DIR=”/var/log” 8
  • 9. Midwest PHP 2018 Using Variables MY_VAR=”something” MY_NUMBER=1 LOG_DIR=”/var/log” APP_LOG=${LOG_DIR}/app.log echo $MY_VAR echo ${MY_VAR} 9
  • 10. Midwest PHP 2018 Declaring Arrays FILES=( ‘app.log’ ‘app.err’ ‘access.log’ ) echo ${FILES[0]} 10
  • 11. Midwest PHP 2018 Declaring Hashes declare -AANIMALS ANIMALS[“moo”]=”cow” ANIMALS[“dog”]=”woof” echo ${ANIMALS[moo]} 11
  • 12. Midwest PHP 2018 Storing Output FILES=`ls /var/log` FILE_DATA=$(cat /var/log/app.log) 12
  • 13. Midwest PHP 2018 Built In Variables $BASH = Location of the bash executable $FUNCNAME = Name of the current function $HOME = Home directory of current user $PATH = Search path for binaries $PWD = Current working directory $TMOUT = Time before a script times out $UID = Current User ID 13
  • 14. Midwest PHP 2018 Built In Variables $0, $1, … = Positional Parameters $# = Number of arguments “$@” = All arguments as quote delimited words $! = PID of the last job run in background $? = Exit code of the last command $$ = PID of the script itself http://tldp.org/LDP/abs/html/internalvariables.html 14
  • 15. Midwest PHP 2018 Flow Control 15
  • 16. Midwest PHP 2018 Numerical Comparison Operators -eq - Equal To -ne - Not Equal To -gt, > - Greater Than -ge, >= - Greater Than or Equal -lt, < - Less Than -le, <= - Less Than or Equal 16
  • 17. Midwest PHP 2018 String Comparison Operators ==, = - Equal To != - Not Equal < - Less than ASCII order > - Greater than ASCII order -n - Not null -z - Is Null, 0 length 17
  • 18. Midwest PHP 2018 Logic Operators &&, -a – And ||, -o – Or && and || short circuit 18
  • 19. Midwest PHP 2018 If/Else Statements if [[ ${MY_VAR} -eq 0 ]]; then echo “MY_VAR equals 0” elif [[ ${MY_VAR} -eq 1 ]]; then Echo “MY_VAR equals 1” else echo “MY_VAR equals ${MY_VAR}” fi 19
  • 20. Midwest PHP 2018 Case Statements case ${MY_VAR} in 1) echo “1”;; 2) echo “2”;; *) echo “Some other number”;; esac Don’t put strings in quotes 20
  • 21. Midwest PHP 2018 For Each Loops FILES=( ‘app.log’ ‘app.err’ ) for FILENAME in ${FILES[@]}; do echo ${FILENAME} done 21
  • 22. Midwest PHP 2018 For Each Loops for FILENAME in `ls /var/log`; do echo ${FILENAME} done 22
  • 23. Midwest PHP 2018 While Loops COUNTER=0 while [[ ${COUNTER} -lt 10 ]]; do ((COUNTER++)) done echo ${COUNTER} 23
  • 25. Midwest PHP 2018 Basic Functions function hello_world() { echo “Hello Midwest PHP!” } hello_world 25
  • 26. Midwest PHP 2018 Function Arguments function app_log() { echo “[$(date)] ${1}” } app_log “This is my log message” 26
  • 27. Midwest PHP 2018 Function Return Values function check_zero() { if [[ ${1} -eq 0 ]]; then return 1 else return 0 fi } IS_ZERO=`check_zero 1` IS_ZERO=`check_zero 0` 27
  • 28. Midwest PHP 2018 Variable Scope MY_VAR=”Midwest PHP” function hello_world() { local MY_VAR=”Hello World” echo ${MY_VAR} } echo ${MY_VAR} hello_world 28
  • 29. Midwest PHP 2018 Putting it all together 29
  • 30. Midwest PHP 2018 Google Shell Style Guide https://google.github.io/styleguide/shell.xml 30
  • 31. Midwest PHP 2018 Structuring Larger Scripts ● Define functions at the top ● Break functions out into their own files – source filename.sh 31
  • 32. Midwest PHP 2018 Function-Only Scripts if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then # This script is being invoked as a script echo 'This script is not intended to be run standalone. Please use with the system-micro.sh script' exit 1 fi 32
  • 33. Midwest PHP 2018 Know when to use a higher level language 33
  • 34. Midwest PHP 2018 Thank You! • Software Engineer for InQuest • Author of “Docker for Developers” • https://leanpub.com/dockerfordevs • Co-Host of “Jerks Talk Games” • http://jerkstalkgames.com • http://ctankersley.com • chris@ctankersley.com • @dragonmantank 34

Hinweis der Redaktion

  1. &amp;lt;number&amp;gt;