SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
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)

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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 a 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 a 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
 

Más de 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
 

Más de 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
 

Último

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

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)