Midwest PHP 2018 Notes: BASHing at the CLI by Chris Tankersley
Chris explained why we should be using scripting to automate tasks and then when into why to use BASH and some basic examples.
Main Take Aways
It’s easy to get started with BASH scripting but it’s important to know some scripts might be better written in higher level languages. I know I’ve written several in PHP when I needed to use objects from my applications.
Things I’m Going to Do
Try to automate more things using BASH. I already use it in a very limited capacity but this made me want to do more.
My Raw Notes
- Command line has a lot of power
- “Internet held together by bash scripts and hope”
Why use bash
- Ultra-portable - truly write once run anywhere
- “Automation is the key to making your life easier”
Anatomy of a script
Variables
- Can’t be a space between variable, equal, and value
Using Variables
Declaring Arrays
Hashes
Storing output
Built in variables
- $0, $1 … -> positional parameters
- $# -> number of arguments
- $? -> Exit code of the last command
- $! -> PID of lat job run int background
- $@ -> All arguments as quote delimited works # useful for debugging
http://tldp.org/LDP/abs/html/internalvariables.html for all variables
Numerical Comparisons
- -eq - Equal to
- -ne - Not equal to
- -gt, >
- -gt, >=
- -lt, <
- -le, <=
Chris recommends using the flag version because it causes less problems converting numbers to strings
String Comparsion
- ==, =
- !=
- <
-
- -n Not null # null is not actually a data type
- -z Is null, 0 length
Logic Operators
- &&, -a
-
, -o
Note: && and | short circuit (i.e. runs in order of operation) |
if/else
Case
For Each Loops
While Loops
Functions
Arguments
Functions return values
Variable Scope
- By default everything is global even in a function. Can use
local MY_VAR="hello world"
to create local functions. - Reverse of PHP where everything is local
https://google.github.io/styleguide/shell.xml
For structing larger scripts
- Define functions at the top
- Or Break functions out into their own files
source filename.sh
Know when to use a higher level language
- Complex data
- Don’t want to rebuild stuff from scratch - no library support in bash
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows