SlideShare a Scribd company logo
1 of 48
Download to read offline
Getting into BSDM with BASH:
Command Interpolation
Steven Lembark
Workhorse Computing
lembark@wrkhors.com
Commanding the shell
Think interpolating variables is fun?
Commanding the shell
Think interpolating variables is fun?
Commands feel even better :-)
Extract command output into variables.
Control shell script execution.
BASH really is interpreted
Unlike C, C++, Perl, Python, Ruby, Raku, Haskell...
BASH:
$foo;
Valid shell command.
BASH really is interpreted
Unlike C, C++, Perl, Python, Ruby, Raku, Haskell...
$foo $args;
Runs something... maybe.
Control
Real utility: Extract from one ouput.
Feed it into another.
Control
Real utility: Extract from one ouput.
Feed it into another.
Not just pipes.
Control
Real utility: Extract from one ouput.
Feed it into another.
Saves cut+paste.
Use one shell command to control another.
BSDM
BSD used ksh.
Backticks were the way to run commands:
foo=`ls -l bar`;
Q: How many escapes does this require?
foo=`ls -l /var/tmp/`whoami``?
BSDM
BSD used ksh.
Backticks were the way to run commands:
foo=`ls -l bar`;
Avoid BSD Masochism: use BASH.
What branch am I on?
Extract a branch with pipes.
Now you know what to type.
$ git status | head -1 | cut -d ' ' -f3;
master
What branch am I on?
Extract a branch with pipes.
Now you know what to cut+paste.
$ git status | head -1 | cut -d ' ' -f3;
master
What branch am I on?
Save a branch with $().
Now you don’t have to type.
br=$(git status | head -1 | cut -d ' ' -f3);
echo $br;
master
What branch am I on?
Save a branch with $().
Now you don’t have to type.
br=$(git status | head -1 | cut -d ' ' -f3);
echo $br;
master
Resync your fork
br=$(git status | head -1 | cut -d ' ' -f3);
git merge upstream/master;
git checkout master;
git rebase upstream;
git checkout $br; # back to start
Build & install the current release.
Postgresql tags releases as REL_X_Y.
Upgrades or smoke tests use most current branch.
Be nice to automate it.
Build & install the current release.
Find the release tag:
$ git tag | egrep '^REL_[0-9_]*$';
REL_10_0
REL_10_1
REL_10_10
REL_10_11
$ git tag | egrep ... | sort -rn | head -n1;
REL_12_1
Build & install the current release.
rx=’^REL_[0-9_]*$’;
rel=$(git tag|grep “$rx”|sort -rn|head -1);
ver=$(echo ${rel#*_} | tr ‘_’ ‘.’);
prefix=”/opt/postgresql/$ver;
[ -d $prefix ] && exit 1;
git clean -fdx;
git checkout $rel;
./configure --prefix=$prefix ... ;
make -wk all check install 2>&1 | tee ... ;
Build & install the current release.
rx=’^REL_[0-9_]*$’;
rel=$(git tag|grep “$rx”|sort -rn|head -1);
ver=$(echo ${rel#*_} | tr ‘_’ ‘.’);
prefix=”/opt/postgresql/$ver;
[ -d $prefix ] && exit 1;
git clean -fdx;
git checkout $rel;
./configure --prefix=$prefix ... ;
make -wk all check install 2>&1 | tee ... ;
Using force
Re-install an existing version?
Select a REL_* tag?
OK:
foobar --force REL_10_0;
Using force
Re-install an existing version?
Select a REL_* tag?
OK:
foobar --force REL_10_0;
if [ “$1” = ‘--force’ ]; then ... fi;
shift;
Using force
Re-install an existing version?
Select a REL_* tag?
OK:
foobar --force REL_10_0;
if [ “$1” = ‘--force’ ]; then ... fi;
shift;
Using force
Re-install an existing version?
Select a REL_* tag?
OOPS:
foobar --force REL_10_0;
foobar REL_10_0 --force;
Having options
getopt(1) shuffles command line args.
Leaves them suitable for iterating in code.
Having options
getopt(1) shuffles command line args.
Leaves them suitable for iterating in code.
ARGS=$( getopt ... "$@" );
eval set -- "$ARGS”;
while :; do
case $1 in
’--’) shift; break;;
...
done;
Having options
getopt(1) shuffles command line args.
Leaves them suitable for iterating in code.
ARGS=$( getopt ... "$@" );
eval set -- "$ARGS”;
while :; do
case $1 in
’--’) shift; break;;
...
done;
Having options
eval + set
re-assign arguments.
ARGS=$( getopt ... "$@" );
eval set -- "$ARGS”;
while :; do
case $1 in
’--’) shift; break;;
...
done;
Having options
-- marks end of switches.
ARGS=$( getopt ... "$@" );
eval set -- "$ARGS”;
while :; do
case $1 in
’--’) shift; break;;
...
done;
Having options
Leave program args on the stack.
ARGS=$( getopt ... "$@" );
eval set -- "$ARGS”;
while :; do
case $1 in
’--’) shift; break;;
...
done;
Example: Glob vs. File Path
‘--file’ or ‘-f’ for a path.
‘--glob’ or ‘-g’ for a glob.
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help’ 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
-f & -g have args (f:g:).
-h & -? do not (h?).
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
-f & -g have args (f:g:).
-h & -? do not (h?).
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
-f & -g have options, -h & -? do not.
Long options are --file , --glob & --help.
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
Errors use the executable basename.
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
Errors use the executable basename.
With nested commands.
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
Errors use the executable basename.
Without escapes, escaped escapes, doubled backticks...
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
One more reason to use $(...).
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
Example: Glob vs. File Path
two-pass interpolation.
Handles whitespace.
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
eval set -- "$ARGS”;
Example: Glob vs. File Path
Consume with a case.
ARGS=$(
getopt -o 'f:g:h?' 
--long 'file:,glob:,help' 
-n "$(basename $0)" -- "$@”
);
eval set -- "$ARGS”;
while :; # loop until ‘--’ end-of-args.
do
case $1
in
'--') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift;
done;
while :;
do
case $1 # switch always $1
in
'–-') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift;
done;
while :;
do
case $1 # switch always $1
in
'–-') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift; # shift off the switch
done;
while :;
do
case $1 # arg always $2, may be ‘’
in
'–-') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift;
done;
while :;
do
case $1 # help is a one-shot.
in
'–-') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift;
done;
while :;
do
case $1 # anything else is fatal
in
'–-') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift;
done;
while :;
do
case $1 # consumes only switches
in
'–-') shift; break ;;
'-g'|'--glob') glob="$2" ; shift ;;
'-f'|'--file') file="$2" ; shift ;;
'-h'|'--help'|'-?') usage; exit 1 ;;
*) usage; exit -1 ;;
esac
shift;
done;
getopt + eval + case
Handle argument order.
Optional args.
Short & long options.
Unknown arguments.
Avoid infestations!
$(...) saves you from backticks.
See also:
bash(1) set, eval, case
getopt(1)

More Related Content

What's hot

Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Workhorse Computing
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Workhorse Computing
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CDavid Wheeler
 
Utility Modules That You Should Know About
Utility Modules That You Should Know AboutUtility Modules That You Should Know About
Utility Modules That You Should Know Aboutjoshua.mcadams
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Puppet
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in PerlLaurent Dami
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationAttila Balazs
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlordsheumann
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLsAugusto Pascutti
 
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...Puppet
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Puppet
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojobpmedley
 

What's hot (20)

Short Introduction To "perl -d"
Short Introduction To "perl -d"Short Introduction To "perl -d"
Short Introduction To "perl -d"
 
Smoking docker
Smoking dockerSmoking docker
Smoking docker
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Getting Testy With Perl6
Getting Testy With Perl6Getting Testy With Perl6
Getting Testy With Perl6
 
Getting testy with Perl
Getting testy with PerlGetting testy with Perl
Getting testy with Perl
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
 
Utility Modules That You Should Know About
Utility Modules That You Should Know AboutUtility Modules That You Should Know About
Utility Modules That You Should Know About
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet -
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
 
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 

Similar to BSDM with BASH: Command Interpolation

Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl styleBo Hua Yang
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongersbrian d foy
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perldaoswald
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perlworr1244
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on LinuxTushar B Kute
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foobrian_dailey
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it2shortplanks
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming languageYaroslav Tkachenko
 
What's New in Perl? v5.10 - v5.16
What's New in Perl?  v5.10 - v5.16What's New in Perl?  v5.10 - v5.16
What's New in Perl? v5.10 - v5.16Ricardo Signes
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 

Similar to BSDM with BASH: Command Interpolation (20)

Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl style
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Cleancode
CleancodeCleancode
Cleancode
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perl
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language
 
What's New in Perl? v5.10 - v5.16
What's New in Perl?  v5.10 - v5.16What's New in Perl?  v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 

More from Workhorse Computing

Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWorkhorse Computing
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpWorkhorse Computing
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlWorkhorse Computing
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.Workhorse Computing
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Workhorse Computing
 
Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Workhorse Computing
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Workhorse Computing
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Workhorse Computing
 

More from Workhorse Computing (18)

Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
 
mro-every.pdf
mro-every.pdfmro-every.pdf
mro-every.pdf
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add Up
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in Posgresql
 
Effective Benchmarks
Effective BenchmarksEffective Benchmarks
Effective Benchmarks
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 
Light my-fuse
Light my-fuseLight my-fuse
Light my-fuse
 
Paranormal stats
Paranormal statsParanormal stats
Paranormal stats
 
Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.
 
Putting some "logic" in LVM.
Putting some "logic" in LVM.Putting some "logic" in LVM.
Putting some "logic" in LVM.
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
Selenium sandwich-2
Selenium sandwich-2Selenium sandwich-2
Selenium sandwich-2
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
 
Docker perl build
Docker perl buildDocker perl build
Docker perl build
 
Designing net-aws-glacier
Designing net-aws-glacierDesigning net-aws-glacier
Designing net-aws-glacier
 
Lies, Damn Lies, and Benchmarks
Lies, Damn Lies, and BenchmarksLies, Damn Lies, and Benchmarks
Lies, Damn Lies, and Benchmarks
 

Recently uploaded

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
 
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
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
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
 
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
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
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
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
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
 

Recently uploaded (20)

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...
 
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
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
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
 
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
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
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
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
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
 

BSDM with BASH: Command Interpolation

  • 1. Getting into BSDM with BASH: Command Interpolation Steven Lembark Workhorse Computing lembark@wrkhors.com
  • 2. Commanding the shell Think interpolating variables is fun?
  • 3. Commanding the shell Think interpolating variables is fun? Commands feel even better :-) Extract command output into variables. Control shell script execution.
  • 4. BASH really is interpreted Unlike C, C++, Perl, Python, Ruby, Raku, Haskell... BASH: $foo; Valid shell command.
  • 5. BASH really is interpreted Unlike C, C++, Perl, Python, Ruby, Raku, Haskell... $foo $args; Runs something... maybe.
  • 6. Control Real utility: Extract from one ouput. Feed it into another.
  • 7. Control Real utility: Extract from one ouput. Feed it into another. Not just pipes.
  • 8. Control Real utility: Extract from one ouput. Feed it into another. Saves cut+paste. Use one shell command to control another.
  • 9. BSDM BSD used ksh. Backticks were the way to run commands: foo=`ls -l bar`; Q: How many escapes does this require? foo=`ls -l /var/tmp/`whoami``?
  • 10. BSDM BSD used ksh. Backticks were the way to run commands: foo=`ls -l bar`; Avoid BSD Masochism: use BASH.
  • 11. What branch am I on? Extract a branch with pipes. Now you know what to type. $ git status | head -1 | cut -d ' ' -f3; master
  • 12. What branch am I on? Extract a branch with pipes. Now you know what to cut+paste. $ git status | head -1 | cut -d ' ' -f3; master
  • 13. What branch am I on? Save a branch with $(). Now you don’t have to type. br=$(git status | head -1 | cut -d ' ' -f3); echo $br; master
  • 14. What branch am I on? Save a branch with $(). Now you don’t have to type. br=$(git status | head -1 | cut -d ' ' -f3); echo $br; master
  • 15. Resync your fork br=$(git status | head -1 | cut -d ' ' -f3); git merge upstream/master; git checkout master; git rebase upstream; git checkout $br; # back to start
  • 16. Build & install the current release. Postgresql tags releases as REL_X_Y. Upgrades or smoke tests use most current branch. Be nice to automate it.
  • 17. Build & install the current release. Find the release tag: $ git tag | egrep '^REL_[0-9_]*$'; REL_10_0 REL_10_1 REL_10_10 REL_10_11 $ git tag | egrep ... | sort -rn | head -n1; REL_12_1
  • 18. Build & install the current release. rx=’^REL_[0-9_]*$’; rel=$(git tag|grep “$rx”|sort -rn|head -1); ver=$(echo ${rel#*_} | tr ‘_’ ‘.’); prefix=”/opt/postgresql/$ver; [ -d $prefix ] && exit 1; git clean -fdx; git checkout $rel; ./configure --prefix=$prefix ... ; make -wk all check install 2>&1 | tee ... ;
  • 19. Build & install the current release. rx=’^REL_[0-9_]*$’; rel=$(git tag|grep “$rx”|sort -rn|head -1); ver=$(echo ${rel#*_} | tr ‘_’ ‘.’); prefix=”/opt/postgresql/$ver; [ -d $prefix ] && exit 1; git clean -fdx; git checkout $rel; ./configure --prefix=$prefix ... ; make -wk all check install 2>&1 | tee ... ;
  • 20. Using force Re-install an existing version? Select a REL_* tag? OK: foobar --force REL_10_0;
  • 21. Using force Re-install an existing version? Select a REL_* tag? OK: foobar --force REL_10_0; if [ “$1” = ‘--force’ ]; then ... fi; shift;
  • 22. Using force Re-install an existing version? Select a REL_* tag? OK: foobar --force REL_10_0; if [ “$1” = ‘--force’ ]; then ... fi; shift;
  • 23. Using force Re-install an existing version? Select a REL_* tag? OOPS: foobar --force REL_10_0; foobar REL_10_0 --force;
  • 24. Having options getopt(1) shuffles command line args. Leaves them suitable for iterating in code.
  • 25. Having options getopt(1) shuffles command line args. Leaves them suitable for iterating in code. ARGS=$( getopt ... "$@" ); eval set -- "$ARGS”; while :; do case $1 in ’--’) shift; break;; ... done;
  • 26. Having options getopt(1) shuffles command line args. Leaves them suitable for iterating in code. ARGS=$( getopt ... "$@" ); eval set -- "$ARGS”; while :; do case $1 in ’--’) shift; break;; ... done;
  • 27. Having options eval + set re-assign arguments. ARGS=$( getopt ... "$@" ); eval set -- "$ARGS”; while :; do case $1 in ’--’) shift; break;; ... done;
  • 28. Having options -- marks end of switches. ARGS=$( getopt ... "$@" ); eval set -- "$ARGS”; while :; do case $1 in ’--’) shift; break;; ... done;
  • 29. Having options Leave program args on the stack. ARGS=$( getopt ... "$@" ); eval set -- "$ARGS”; while :; do case $1 in ’--’) shift; break;; ... done;
  • 30. Example: Glob vs. File Path ‘--file’ or ‘-f’ for a path. ‘--glob’ or ‘-g’ for a glob. ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help’ -n "$(basename $0)" -- "$@” );
  • 31. Example: Glob vs. File Path -f & -g have args (f:g:). -h & -? do not (h?). ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 32. Example: Glob vs. File Path -f & -g have args (f:g:). -h & -? do not (h?). ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 33. Example: Glob vs. File Path -f & -g have options, -h & -? do not. Long options are --file , --glob & --help. ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 34. Example: Glob vs. File Path Errors use the executable basename. ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 35. Example: Glob vs. File Path Errors use the executable basename. With nested commands. ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 36. Example: Glob vs. File Path Errors use the executable basename. Without escapes, escaped escapes, doubled backticks... ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 37. Example: Glob vs. File Path One more reason to use $(...). ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” );
  • 38. Example: Glob vs. File Path two-pass interpolation. Handles whitespace. ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” ); eval set -- "$ARGS”;
  • 39. Example: Glob vs. File Path Consume with a case. ARGS=$( getopt -o 'f:g:h?' --long 'file:,glob:,help' -n "$(basename $0)" -- "$@” ); eval set -- "$ARGS”;
  • 40. while :; # loop until ‘--’ end-of-args. do case $1 in '--') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; done;
  • 41. while :; do case $1 # switch always $1 in '–-') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; done;
  • 42. while :; do case $1 # switch always $1 in '–-') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; # shift off the switch done;
  • 43. while :; do case $1 # arg always $2, may be ‘’ in '–-') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; done;
  • 44. while :; do case $1 # help is a one-shot. in '–-') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; done;
  • 45. while :; do case $1 # anything else is fatal in '–-') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; done;
  • 46. while :; do case $1 # consumes only switches in '–-') shift; break ;; '-g'|'--glob') glob="$2" ; shift ;; '-f'|'--file') file="$2" ; shift ;; '-h'|'--help'|'-?') usage; exit 1 ;; *) usage; exit -1 ;; esac shift; done;
  • 47. getopt + eval + case Handle argument order. Optional args. Short & long options. Unknown arguments.
  • 48. Avoid infestations! $(...) saves you from backticks. See also: bash(1) set, eval, case getopt(1)