SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
SHELL SCRIPTING
Cyril Soldani
cyril.soldani@uliege.be
OUTLINE
What is it all about?
Why should I care?
A 5-minute introduction to POSIX shell scripts
How not to shoot yourself in the foot
A few recipes?
Discussion
WHAT IS IT ALL ABOUT?
Interactive: Script:
WHAT IS A SHELL SCRIPT?
A shell is a command-line UI to access OS services.
A shell script is a sequence of shell commands.
$ mkdir a_folder
$ cd a_folder
$ echo "Hello"
Hello
$ for n in John Mary; do
for> echo $n
for> done
John
Mary
#!/bin/sh
mkdir a_folder
cd a_folder
echo "Hello"
for n in John Mary; do
echo $n
done
THERE ARE AS MANY SHELL
SCRIPTING LANGUAGES AS
SHELLS
UNIX shells: bash, zsh, ksh, tcsh, ...
Microso shells: command.com, PowerShell.
Most UNIX shells support a standard: POSIX.
bash and POSIX are the most used.
Windows 10 now supports bash (and POSIX).
WHY SHOULD I CARE?
THE SHELL STILL MATTERS
It is more suited than the GUI for many tasks, e.g.
deleting a set of files following a given pattern:
It allows automation through scripting.
Let the machine do the work for you!
It is still used in many places:
initialization scripts, application wrappers, tests,
administration tools, cron jobs, etc.
It allows easy remote administration.
rm *foo*.log *bar*.aux
SHELL SCRIPTS CAN BE SHORT
Get login dates and users according to systemd.
Shell scripts are generally the quickest way to
automate a simple task.
grep 'systemd.*Slice' /var/log/syslog | cut -d' ' -f1-3,11
import re
pat = re.compile('systemd.*Slice')
with open("/var/log/syslog") as f:
for line in f:
if pat.search(line):
words = line.rstrip().split(' ')
print(words[0], words[1], words[2], words[10])
IT IS BEGINNER-FRIENDLY
Learning the shell is useful.
Basic shell use is easy to learn.
Shell scripting comes free if you know the shell!
... but it is hard to master :'(
A 5-MINUTE INTRODUCTION TO
POSIX SHELL SCRIPTS
We all know that time is relative.
A SIMPLE SCRIPT
#!/bin/sh
rate=4000 # Estimated disk rate in kB/s
while sleep 1; do
dirty=$(grep 'Dirty:' /proc/meminfo | tr -s ' ' 
| cut -d' ' -f2)
if [ $dirty -lt 1000 ]; then
break;
fi
secs=$((dirty / rate))
hours=$((secs / 3600)); secs=$((secs - 3600 * hours))
mins=$((secs / 60)); secs=$((secs - 60 * mins))
printf "r%02dh %02dm %02ds" $hours $mins $secs
done
echo
REDIRECTIONS
You can use other file descriptors than 1 (stdout)
and 2 (stderr) to make crazy stuff.
The order can be tricky. The engineer way: test it.
cmd1 | cmd2 # cmd2.stdin = cmd1.stdout
cmd > out.txt # File out.txt = cmd.stdout
cmd 1> out.txt # Idem
cmd >> out.txt # cmd.stdout is appended to out.txt
cmd < in.txt # cmd.stdin = contents of file in.txt
cmd < in.txt > out.txt # You can combine
cmd 2> /dev/null # cmd.stderr written to /dev/null
cmd > out.txt 2>> err.txt
1>&2 cmd # cmd.stdout will go to stderr
ANOTHER SIMPLE SCRIPT
#!/bin/sh
die() {
>&2 echo "$1"
exit 1
}
[ $# -eq 1 ] || die "Usage: lumount DISK-LABEL"
[ -L "/dev/disk/by-label/$1" ] || 
die "No disk with label '$1'."
[ -d "/media/$1" ] || 
die "Cannot find '/media/$1' mount point."
pumount "$1"
HERE DOCUMENTS
cmd <<ZORGLUB
All file content up to next ZORGLUB on its own line is fed
to cmd.stdin.
Note that you can use ${variable}s and
embedded $(echo "commands").
ZORGLUB
#!/bin/sh
# Usage: dhcp_add_machine MAC_ADDRESS HOSTNAME
cat <<EOF >> /etc/dhcp/dhcpd.conf
host $2 {
fixed-address $2.run.montefiore.ulg.ac.be;
hardware ethernet $1;
}
EOF
systemctl restart dhcpd.service
PARAMETER EXPANSION
You can modify variable content in various ways
directly from the shell.
${foo:-bar} # $foo if defined and not null, "bar" otherwise
${foo:+bar} # null if $foo is unset or null, "bar" otherwise
${foo%bar} # removes smallest "bar" suffix from foo
${foo%%bar} # removes largest "bar" suffix from foo
${haystack/pin/needle} # substring replacement (not POSIX)
rate=${1:-4000} # Initialize rate to first argument or 4000
if [ -z ${TARGET_DIR:+set} ]; then
die "TARGET_DIR is not specified!"
fi
${filename%%.*} # removes all extensions from filename
for i in *CurrentEdit*; do
mv "$i" "${i/CurrentEdit/_edit_}"
# trucCurrentEdit42.log -> truc_edit_42.log
done
HOW NOT TO SHOOT YOURSELF
IN THE FOOT
... or anywhere else where it hurts.
WHAT IS WRONG WITH THAT
SCRIPT?
The correct path is /var/srv/mightyapp/tmp
(without dash).
cd will fail, but the script will continue and erase
everything in current directory!
#!/bin/sh
# Clean-up mighty-app temporary files
if pgrep mighty-app >/dev/null; then
>&2 echo "Error: mighty-app is running, stop it first!"
exit 1
fi
cd /var/srv/mighty-app/tmp
rm -rf *
ALWAYS USE set -e
The script will fail if any command fail.
You can still use failing commands in conditions,
and with ||.
You can temporarily disable checking with set +e.
#!/bin/sh
set -e
...
THE PIPE ABSORBS ERRORS!
faulty_cmd fails, grep might fail, but cut will be
happy, and critical_processing will be called
with bogus data!
bash has an option set -o pipefail for this, but
beware of broken pipes:
#!/bin/sh
set -e
data=$(faulty_cmd | grep some_pattern | cut -d' ' -f1)
critical_processing -data $data
#!/bin/bash
set -eo pipefail
first=$(grep "systemd.*Slice" /var/log/syslog | head -n1)
# The above will fail if head exits before grep
WHAT IS WRONG WITH THAT
SCRIPT?
$MIGHTY_APP_TMP_DIR might be undefined.
cd will happily go to your home folder...
... where every file will be deleted!
#!/bin/sh
set -e
# Clean-up mighty-app temporary files
if pgrep mighty-app >/dev/null; then
>&2 echo "Error: mighty-app is running, stop it first!"
exit 1
fi
cd "$MIGHTY_APP_TMP_DIR"
rm -rf *
ALWAYS USE set -u
The script will fail if it tries to use an undefined
variable.
You can test for definition using parameter
expansion.
You can use set +u to disable temporarily, e.g.
before sourcing a more laxist file.
#!/bin/sh
set -eu
...
A FALSE GOOD IDEA
It is shorter and cleaner, right?
No! What if someone does sh your_script.sh?
#!/bin/sh -eu
...
QUOTE LIBERALLY!
cd $1 is expanded to cd My project.
Use cd "$1" instead.
#!/bin/sh
# Add given directory to source control
set -eu
cd $1
git init
...
$ git-add-dir "My project"
git-add-dir: 3: cd: can't cd to My
zsh: exit 2 git-add-dir "My project"
USING COMMANDS WHICH ARE
NOT AVAILABLE
As most shell scripting commands are actually
programs, they must be installed (and in path).
Beware of what you assume will be there.
Commands might also behave differently than what
you expect (e.g. ps on Linux behaves much
differently than the one on FreeBSD).
RELYING ON VARIABLE COMMAND
OUTPUT
If you process the ouptut of some commands in your
scripts, ensure that the command output is well-
defined, and stable.
E.g. the output of ls will vary from system to sytem,
between versions, and even depends on shell and
terminal configuration! Use find instead.
Some commands have flags to switch from a
human-readable format to one that is easily
processed by a machine.
BEWARE OF SUBPROCESSES
The right-hand side of | runs in a subprocess:
Use FIFOs or process susbstitution (bash) instead:
maxVal=-1
get_some_numbers | while read i; do
if [ $i -gt $maxVal ]; then
maxVal=$i
fi
done
# $maxVal is back to -1 here
maxVal=-1
while read i; do
if [ $i -gt $maxVal ]; then
maxVal=$i
fi
done < <(get_some_numbers)
MODIFYING THE OUTER
ENVIRONMENT
A script runs in its own process, it cannot modify the
caller environment (export variables, define
functions or aliases).
Source the file with . instead, it will be included in
the running shell.
$ pyvenv my-venv # Creates a python virtual environment
$ sh my-venv/bin/activate # Does nothing
$ . my-venv/bin/activate
(my-venv) $
HANDLING SIGNALS
Clean-up a er yourself ...
... even if something strange occurred!
tmpdir=$(mktemp -d "myscript.XXXXXX")
trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP
...
IN SUMMARY
Always use set -eu.
Quote liberally.
Program defensively.
Think about your dependencies.
Think about what runs in which process.
Clean-up a er yourself.
Test, test, test.
A FEW RECIPES
WRAPPER SCRIPTS
That pesky program keeps trying to open ou i files
with trucmuche instead of tartempion? Put the
following in bin/trucmuche:
You want that huge terminal font size for beamers?
is all you need.
#!/bin/sh
set -eu
exec tartempion "$@"
#!/bin/sh
set -eu
exec urxvt -fn xft:Mono:size=24 "$@"
DEPLOYMENT HELPERS
You want to package that Java application so that it
looks like a regular program?
#!/bin/sh
set -eu
exec java -jar /usr/libexec/MyApp/bloated.jar 
my.insanely.long.package.name.MyApp "$@"
FOLLOWING A FILE
How to process a log file with a shell script so that it
continues its execution each time a new line is
appended to the log?
#!/bin/bash
set -euo pipefail
process_line() {
# Code to process a line...
}
while read line; do
process_line "$line"
done < <(tail -n +1 -f /var/log/my_log)
USING ANOTHER LANGUAGE
Use here scripts for short snippets:
Or delegate to another interpreter entirely:
Changing the shebang might be all that's required!
#!/bin/sh
myVar=$(some_command -some-argument)
python3 <<EOF
print("myVar =", $myVar) # Note the variable substitution!
EOF
#!/bin/sh
"exec" "python3" "$0" "$@" # Python ignores strings
print("Hello") # Python code from now on
#!/usr/bin/env python3
print("Hello")
DISCUSSION
TO BASH, OR NOT TO BASH?
bash pros:
More expressive (e.g. arrays, process
substitution).
Safer (e.g. pipefail).
POSIX pros:
More portable.
Faster execution.
Mostly forces you to stick to simple tasks!
SHELLSCRIPTOR VS PYTHONISTA
#!/bin/sh
set -eu
LLVM_CONFIG=${LLVM_CONFIG:-llvm-config-4.0}
CFLAGS="$($LLVM_CONFIG --cflags) ${CFLAGS:-}"
LDFLAGS="$($LLVM_CONFIG --ldflags) ${LDFLAGS:-}"
LDLIBS="$($LLVM_CONFIG --libs) ${LDLIBS:-}"
CC="${CC:-clang}"
for src in *.c; do
$CC $CFLAGS -c -o "${src%.c}.o" "$src"
done
$CC -o compiler *.o $LDFLAGS $LDLIBS
SHELLSCRIPTOR VS PYTHONISTA
#!/usr/bin/env python3
import glob, os, subprocess
def config(arg):
llvm_config = os.environ.get("LLVM_CONFIG", "llvm-config-4.0")
return subprocess.check_output([llvm_config, arg]).decode('utf-8')
cflags = config("--cflags") + " " + os.environ.get("CFLAGS", "")
ldflags = config("--ldflags") + " " + os.environ.get("LDFLAGS", "")
libs = config("--libs") + " " + os.environ.get("LDLIBS", "")
cc = os.environ.get("CC", "clang")
for src in glob.glob("*.c"):
obj = src.rsplit(".", 1)[0] + ".o"
subprocess.check_call([cc, cflags, "-c", "-o", obj, src])
objs = glob.glob("*.o")
subprocess.check_call([cc, "-o", "compiler"] + objs + [ldflags, libs])
TO SHELL-SCRIPT, OR NOT TO
SHELL-SCRIPT?
Pros:
Quick way to automate simple tasks.
You (should) already know it.
Always available.
Good for one-shots.
Cons:
Hard to write reliable scripts.
Limited expressivity.
Shell scripts can quickly become cryptic.
Manual dependency tracking.
Limited reuse.

Weitere ähnliche Inhalte

Was ist angesagt?

Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scriptingVIKAS TIWARI
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script Amit Ghosh
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scriptingvceder
 
Linux basics part 1
Linux basics part 1Linux basics part 1
Linux basics part 1Lilesh Pathe
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utilityNirajan Pant
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examplesabclearnn
 
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linuxshravan saini
 
Basic command ppt
Basic command pptBasic command ppt
Basic command pptRohit Kumar
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scriptingAkshay Siwal
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic CommandsHanan Nmr
 
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Ahmed El-Arabawy
 
Linux command ppt
Linux command pptLinux command ppt
Linux command pptkalyanineve
 

Was ist angesagt? (20)

Shell programming
Shell programmingShell programming
Shell programming
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Basic 50 linus command
Basic 50 linus commandBasic 50 linus command
Basic 50 linus command
 
Linux basics part 1
Linux basics part 1Linux basics part 1
Linux basics part 1
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examples
 
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Basic command ppt
Basic command pptBasic command ppt
Basic command ppt
 
Vi editor
Vi editorVi editor
Vi editor
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
 
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
 
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 

Ähnlich wie Shell scripting

Bash is not a second zone citizen programming language
Bash is not a second zone citizen programming languageBash is not a second zone citizen programming language
Bash is not a second zone citizen programming languageRené Ribaud
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentationbrian_dailey
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
Linux: Beyond ls and cd
Linux: Beyond ls and cdLinux: Beyond ls and cd
Linux: Beyond ls and cdjacko91
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scriptsMichael Boelen
 
Shell Scripts
Shell ScriptsShell Scripts
Shell ScriptsDr.Ravi
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologistAjay Murali
 
Andresen 8 21 02
Andresen 8 21 02Andresen 8 21 02
Andresen 8 21 02FNian
 
Apache Hadoop Shell Rewrite
Apache Hadoop Shell RewriteApache Hadoop Shell Rewrite
Apache Hadoop Shell RewriteAllen Wittenauer
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsAbhay Sapru
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsDr.Ravi
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them Allegypt
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsSudharsan S
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 

Ähnlich wie Shell scripting (20)

Bash is not a second zone citizen programming language
Bash is not a second zone citizen programming languageBash is not a second zone citizen programming language
Bash is not a second zone citizen programming language
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Linux: Beyond ls and cd
Linux: Beyond ls and cdLinux: Beyond ls and cd
Linux: Beyond ls and cd
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scripts
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologist
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Andresen 8 21 02
Andresen 8 21 02Andresen 8 21 02
Andresen 8 21 02
 
Apache Hadoop Shell Rewrite
Apache Hadoop Shell RewriteApache Hadoop Shell Rewrite
Apache Hadoop Shell Rewrite
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
unix_commands.ppt
unix_commands.pptunix_commands.ppt
unix_commands.ppt
 
Linux Basics
Linux BasicsLinux Basics
Linux Basics
 
Slides
SlidesSlides
Slides
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Bash 4
Bash 4Bash 4
Bash 4
 

Mehr von Geeks Anonymes

Programmer sous Unreal Engine
Programmer sous Unreal EngineProgrammer sous Unreal Engine
Programmer sous Unreal EngineGeeks Anonymes
 
Implémentation efficace et durable de processus métiers complexes
Implémentation efficace et durable de processus métiers complexesImplémentation efficace et durable de processus métiers complexes
Implémentation efficace et durable de processus métiers complexesGeeks Anonymes
 
Managing Open Source Licenses (Geeks Anonymes)
Managing Open Source Licenses (Geeks Anonymes)Managing Open Source Licenses (Geeks Anonymes)
Managing Open Source Licenses (Geeks Anonymes)Geeks Anonymes
 
Reprendre le contrôle de ses données
Reprendre le contrôle de ses donnéesReprendre le contrôle de ses données
Reprendre le contrôle de ses donnéesGeeks Anonymes
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 
Le rôle du testeur et le Blackbox testing
Le rôle du testeur et le Blackbox testingLe rôle du testeur et le Blackbox testing
Le rôle du testeur et le Blackbox testingGeeks Anonymes
 
Vulnérabilités au cœur des applications Web, menaces et contre-mesures
 Vulnérabilités au cœur des applications Web, menaces et contre-mesures Vulnérabilités au cœur des applications Web, menaces et contre-mesures
Vulnérabilités au cœur des applications Web, menaces et contre-mesuresGeeks Anonymes
 
191121 philippe teuwen cryptographie et attaques materielles
191121 philippe teuwen cryptographie et attaques materielles191121 philippe teuwen cryptographie et attaques materielles
191121 philippe teuwen cryptographie et attaques materiellesGeeks Anonymes
 
"Surfez couverts !" - Conseils de Cyber securité
"Surfez couverts !" - Conseils de Cyber securité "Surfez couverts !" - Conseils de Cyber securité
"Surfez couverts !" - Conseils de Cyber securité Geeks Anonymes
 
Introduction au développement mobile - développer une application iOS et Andr...
Introduction au développement mobile - développer une application iOS et Andr...Introduction au développement mobile - développer une application iOS et Andr...
Introduction au développement mobile - développer une application iOS et Andr...Geeks Anonymes
 
Intelligence artificielle et propriété intellectuelle
Intelligence artificielle et propriété intellectuelleIntelligence artificielle et propriété intellectuelle
Intelligence artificielle et propriété intellectuelleGeeks Anonymes
 
Pour une histoire plophonique du jeu video
Pour une histoire plophonique du jeu videoPour une histoire plophonique du jeu video
Pour une histoire plophonique du jeu videoGeeks Anonymes
 
Become Rick and famous, thanks to Open Source
Become Rick and famous, thanks to Open SourceBecome Rick and famous, thanks to Open Source
Become Rick and famous, thanks to Open SourceGeeks Anonymes
 
Reconnaissance vocale et création artistique
Reconnaissance vocale et création artistiqueReconnaissance vocale et création artistique
Reconnaissance vocale et création artistiqueGeeks Anonymes
 
Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language ProcessingGeeks Anonymes
 
Sécurité, GDPR : vos données ont de la valeur
Sécurité, GDPR : vos données ont de la valeur Sécurité, GDPR : vos données ont de la valeur
Sécurité, GDPR : vos données ont de la valeur Geeks Anonymes
 

Mehr von Geeks Anonymes (20)

Programmer sous Unreal Engine
Programmer sous Unreal EngineProgrammer sous Unreal Engine
Programmer sous Unreal Engine
 
Implémentation efficace et durable de processus métiers complexes
Implémentation efficace et durable de processus métiers complexesImplémentation efficace et durable de processus métiers complexes
Implémentation efficace et durable de processus métiers complexes
 
Managing Open Source Licenses (Geeks Anonymes)
Managing Open Source Licenses (Geeks Anonymes)Managing Open Source Licenses (Geeks Anonymes)
Managing Open Source Licenses (Geeks Anonymes)
 
Reprendre le contrôle de ses données
Reprendre le contrôle de ses donnéesReprendre le contrôle de ses données
Reprendre le contrôle de ses données
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 
Le rôle du testeur et le Blackbox testing
Le rôle du testeur et le Blackbox testingLe rôle du testeur et le Blackbox testing
Le rôle du testeur et le Blackbox testing
 
Kubernetes
KubernetesKubernetes
Kubernetes
 
Vulnérabilités au cœur des applications Web, menaces et contre-mesures
 Vulnérabilités au cœur des applications Web, menaces et contre-mesures Vulnérabilités au cœur des applications Web, menaces et contre-mesures
Vulnérabilités au cœur des applications Web, menaces et contre-mesures
 
191121 philippe teuwen cryptographie et attaques materielles
191121 philippe teuwen cryptographie et attaques materielles191121 philippe teuwen cryptographie et attaques materielles
191121 philippe teuwen cryptographie et attaques materielles
 
"Surfez couverts !" - Conseils de Cyber securité
"Surfez couverts !" - Conseils de Cyber securité "Surfez couverts !" - Conseils de Cyber securité
"Surfez couverts !" - Conseils de Cyber securité
 
Introduction au développement mobile - développer une application iOS et Andr...
Introduction au développement mobile - développer une application iOS et Andr...Introduction au développement mobile - développer une application iOS et Andr...
Introduction au développement mobile - développer une application iOS et Andr...
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Test your code
Test your codeTest your code
Test your code
 
Intelligence artificielle et propriété intellectuelle
Intelligence artificielle et propriété intellectuelleIntelligence artificielle et propriété intellectuelle
Intelligence artificielle et propriété intellectuelle
 
Pour une histoire plophonique du jeu video
Pour une histoire plophonique du jeu videoPour une histoire plophonique du jeu video
Pour une histoire plophonique du jeu video
 
Become Rick and famous, thanks to Open Source
Become Rick and famous, thanks to Open SourceBecome Rick and famous, thanks to Open Source
Become Rick and famous, thanks to Open Source
 
Reconnaissance vocale et création artistique
Reconnaissance vocale et création artistiqueReconnaissance vocale et création artistique
Reconnaissance vocale et création artistique
 
Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language Processing
 
Sécurité, GDPR : vos données ont de la valeur
Sécurité, GDPR : vos données ont de la valeur Sécurité, GDPR : vos données ont de la valeur
Sécurité, GDPR : vos données ont de la valeur
 
Modern sql
Modern sqlModern sql
Modern sql
 

Kürzlich hochgeladen

%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 

Kürzlich hochgeladen (20)

%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 

Shell scripting

  • 2. OUTLINE What is it all about? Why should I care? A 5-minute introduction to POSIX shell scripts How not to shoot yourself in the foot A few recipes? Discussion
  • 3. WHAT IS IT ALL ABOUT?
  • 4. Interactive: Script: WHAT IS A SHELL SCRIPT? A shell is a command-line UI to access OS services. A shell script is a sequence of shell commands. $ mkdir a_folder $ cd a_folder $ echo "Hello" Hello $ for n in John Mary; do for> echo $n for> done John Mary #!/bin/sh mkdir a_folder cd a_folder echo "Hello" for n in John Mary; do echo $n done
  • 5. THERE ARE AS MANY SHELL SCRIPTING LANGUAGES AS SHELLS UNIX shells: bash, zsh, ksh, tcsh, ... Microso shells: command.com, PowerShell. Most UNIX shells support a standard: POSIX. bash and POSIX are the most used. Windows 10 now supports bash (and POSIX).
  • 6. WHY SHOULD I CARE?
  • 7. THE SHELL STILL MATTERS It is more suited than the GUI for many tasks, e.g. deleting a set of files following a given pattern: It allows automation through scripting. Let the machine do the work for you! It is still used in many places: initialization scripts, application wrappers, tests, administration tools, cron jobs, etc. It allows easy remote administration. rm *foo*.log *bar*.aux
  • 8. SHELL SCRIPTS CAN BE SHORT Get login dates and users according to systemd. Shell scripts are generally the quickest way to automate a simple task. grep 'systemd.*Slice' /var/log/syslog | cut -d' ' -f1-3,11 import re pat = re.compile('systemd.*Slice') with open("/var/log/syslog") as f: for line in f: if pat.search(line): words = line.rstrip().split(' ') print(words[0], words[1], words[2], words[10])
  • 9. IT IS BEGINNER-FRIENDLY Learning the shell is useful. Basic shell use is easy to learn. Shell scripting comes free if you know the shell! ... but it is hard to master :'(
  • 10. A 5-MINUTE INTRODUCTION TO POSIX SHELL SCRIPTS We all know that time is relative.
  • 11. A SIMPLE SCRIPT #!/bin/sh rate=4000 # Estimated disk rate in kB/s while sleep 1; do dirty=$(grep 'Dirty:' /proc/meminfo | tr -s ' ' | cut -d' ' -f2) if [ $dirty -lt 1000 ]; then break; fi secs=$((dirty / rate)) hours=$((secs / 3600)); secs=$((secs - 3600 * hours)) mins=$((secs / 60)); secs=$((secs - 60 * mins)) printf "r%02dh %02dm %02ds" $hours $mins $secs done echo
  • 12. REDIRECTIONS You can use other file descriptors than 1 (stdout) and 2 (stderr) to make crazy stuff. The order can be tricky. The engineer way: test it. cmd1 | cmd2 # cmd2.stdin = cmd1.stdout cmd > out.txt # File out.txt = cmd.stdout cmd 1> out.txt # Idem cmd >> out.txt # cmd.stdout is appended to out.txt cmd < in.txt # cmd.stdin = contents of file in.txt cmd < in.txt > out.txt # You can combine cmd 2> /dev/null # cmd.stderr written to /dev/null cmd > out.txt 2>> err.txt 1>&2 cmd # cmd.stdout will go to stderr
  • 13. ANOTHER SIMPLE SCRIPT #!/bin/sh die() { >&2 echo "$1" exit 1 } [ $# -eq 1 ] || die "Usage: lumount DISK-LABEL" [ -L "/dev/disk/by-label/$1" ] || die "No disk with label '$1'." [ -d "/media/$1" ] || die "Cannot find '/media/$1' mount point." pumount "$1"
  • 14. HERE DOCUMENTS cmd <<ZORGLUB All file content up to next ZORGLUB on its own line is fed to cmd.stdin. Note that you can use ${variable}s and embedded $(echo "commands"). ZORGLUB #!/bin/sh # Usage: dhcp_add_machine MAC_ADDRESS HOSTNAME cat <<EOF >> /etc/dhcp/dhcpd.conf host $2 { fixed-address $2.run.montefiore.ulg.ac.be; hardware ethernet $1; } EOF systemctl restart dhcpd.service
  • 15. PARAMETER EXPANSION You can modify variable content in various ways directly from the shell. ${foo:-bar} # $foo if defined and not null, "bar" otherwise ${foo:+bar} # null if $foo is unset or null, "bar" otherwise ${foo%bar} # removes smallest "bar" suffix from foo ${foo%%bar} # removes largest "bar" suffix from foo ${haystack/pin/needle} # substring replacement (not POSIX) rate=${1:-4000} # Initialize rate to first argument or 4000 if [ -z ${TARGET_DIR:+set} ]; then die "TARGET_DIR is not specified!" fi ${filename%%.*} # removes all extensions from filename for i in *CurrentEdit*; do mv "$i" "${i/CurrentEdit/_edit_}" # trucCurrentEdit42.log -> truc_edit_42.log done
  • 16. HOW NOT TO SHOOT YOURSELF IN THE FOOT ... or anywhere else where it hurts.
  • 17. WHAT IS WRONG WITH THAT SCRIPT? The correct path is /var/srv/mightyapp/tmp (without dash). cd will fail, but the script will continue and erase everything in current directory! #!/bin/sh # Clean-up mighty-app temporary files if pgrep mighty-app >/dev/null; then >&2 echo "Error: mighty-app is running, stop it first!" exit 1 fi cd /var/srv/mighty-app/tmp rm -rf *
  • 18. ALWAYS USE set -e The script will fail if any command fail. You can still use failing commands in conditions, and with ||. You can temporarily disable checking with set +e. #!/bin/sh set -e ...
  • 19. THE PIPE ABSORBS ERRORS! faulty_cmd fails, grep might fail, but cut will be happy, and critical_processing will be called with bogus data! bash has an option set -o pipefail for this, but beware of broken pipes: #!/bin/sh set -e data=$(faulty_cmd | grep some_pattern | cut -d' ' -f1) critical_processing -data $data #!/bin/bash set -eo pipefail first=$(grep "systemd.*Slice" /var/log/syslog | head -n1) # The above will fail if head exits before grep
  • 20. WHAT IS WRONG WITH THAT SCRIPT? $MIGHTY_APP_TMP_DIR might be undefined. cd will happily go to your home folder... ... where every file will be deleted! #!/bin/sh set -e # Clean-up mighty-app temporary files if pgrep mighty-app >/dev/null; then >&2 echo "Error: mighty-app is running, stop it first!" exit 1 fi cd "$MIGHTY_APP_TMP_DIR" rm -rf *
  • 21. ALWAYS USE set -u The script will fail if it tries to use an undefined variable. You can test for definition using parameter expansion. You can use set +u to disable temporarily, e.g. before sourcing a more laxist file. #!/bin/sh set -eu ...
  • 22. A FALSE GOOD IDEA It is shorter and cleaner, right? No! What if someone does sh your_script.sh? #!/bin/sh -eu ...
  • 23. QUOTE LIBERALLY! cd $1 is expanded to cd My project. Use cd "$1" instead. #!/bin/sh # Add given directory to source control set -eu cd $1 git init ... $ git-add-dir "My project" git-add-dir: 3: cd: can't cd to My zsh: exit 2 git-add-dir "My project"
  • 24. USING COMMANDS WHICH ARE NOT AVAILABLE As most shell scripting commands are actually programs, they must be installed (and in path). Beware of what you assume will be there. Commands might also behave differently than what you expect (e.g. ps on Linux behaves much differently than the one on FreeBSD).
  • 25. RELYING ON VARIABLE COMMAND OUTPUT If you process the ouptut of some commands in your scripts, ensure that the command output is well- defined, and stable. E.g. the output of ls will vary from system to sytem, between versions, and even depends on shell and terminal configuration! Use find instead. Some commands have flags to switch from a human-readable format to one that is easily processed by a machine.
  • 26. BEWARE OF SUBPROCESSES The right-hand side of | runs in a subprocess: Use FIFOs or process susbstitution (bash) instead: maxVal=-1 get_some_numbers | while read i; do if [ $i -gt $maxVal ]; then maxVal=$i fi done # $maxVal is back to -1 here maxVal=-1 while read i; do if [ $i -gt $maxVal ]; then maxVal=$i fi done < <(get_some_numbers)
  • 27. MODIFYING THE OUTER ENVIRONMENT A script runs in its own process, it cannot modify the caller environment (export variables, define functions or aliases). Source the file with . instead, it will be included in the running shell. $ pyvenv my-venv # Creates a python virtual environment $ sh my-venv/bin/activate # Does nothing $ . my-venv/bin/activate (my-venv) $
  • 28. HANDLING SIGNALS Clean-up a er yourself ... ... even if something strange occurred! tmpdir=$(mktemp -d "myscript.XXXXXX") trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP ...
  • 29. IN SUMMARY Always use set -eu. Quote liberally. Program defensively. Think about your dependencies. Think about what runs in which process. Clean-up a er yourself. Test, test, test.
  • 31. WRAPPER SCRIPTS That pesky program keeps trying to open ou i files with trucmuche instead of tartempion? Put the following in bin/trucmuche: You want that huge terminal font size for beamers? is all you need. #!/bin/sh set -eu exec tartempion "$@" #!/bin/sh set -eu exec urxvt -fn xft:Mono:size=24 "$@"
  • 32. DEPLOYMENT HELPERS You want to package that Java application so that it looks like a regular program? #!/bin/sh set -eu exec java -jar /usr/libexec/MyApp/bloated.jar my.insanely.long.package.name.MyApp "$@"
  • 33. FOLLOWING A FILE How to process a log file with a shell script so that it continues its execution each time a new line is appended to the log? #!/bin/bash set -euo pipefail process_line() { # Code to process a line... } while read line; do process_line "$line" done < <(tail -n +1 -f /var/log/my_log)
  • 34. USING ANOTHER LANGUAGE Use here scripts for short snippets: Or delegate to another interpreter entirely: Changing the shebang might be all that's required! #!/bin/sh myVar=$(some_command -some-argument) python3 <<EOF print("myVar =", $myVar) # Note the variable substitution! EOF #!/bin/sh "exec" "python3" "$0" "$@" # Python ignores strings print("Hello") # Python code from now on #!/usr/bin/env python3 print("Hello")
  • 36. TO BASH, OR NOT TO BASH? bash pros: More expressive (e.g. arrays, process substitution). Safer (e.g. pipefail). POSIX pros: More portable. Faster execution. Mostly forces you to stick to simple tasks!
  • 37. SHELLSCRIPTOR VS PYTHONISTA #!/bin/sh set -eu LLVM_CONFIG=${LLVM_CONFIG:-llvm-config-4.0} CFLAGS="$($LLVM_CONFIG --cflags) ${CFLAGS:-}" LDFLAGS="$($LLVM_CONFIG --ldflags) ${LDFLAGS:-}" LDLIBS="$($LLVM_CONFIG --libs) ${LDLIBS:-}" CC="${CC:-clang}" for src in *.c; do $CC $CFLAGS -c -o "${src%.c}.o" "$src" done $CC -o compiler *.o $LDFLAGS $LDLIBS
  • 38. SHELLSCRIPTOR VS PYTHONISTA #!/usr/bin/env python3 import glob, os, subprocess def config(arg): llvm_config = os.environ.get("LLVM_CONFIG", "llvm-config-4.0") return subprocess.check_output([llvm_config, arg]).decode('utf-8') cflags = config("--cflags") + " " + os.environ.get("CFLAGS", "") ldflags = config("--ldflags") + " " + os.environ.get("LDFLAGS", "") libs = config("--libs") + " " + os.environ.get("LDLIBS", "") cc = os.environ.get("CC", "clang") for src in glob.glob("*.c"): obj = src.rsplit(".", 1)[0] + ".o" subprocess.check_call([cc, cflags, "-c", "-o", obj, src]) objs = glob.glob("*.o") subprocess.check_call([cc, "-o", "compiler"] + objs + [ldflags, libs])
  • 39. TO SHELL-SCRIPT, OR NOT TO SHELL-SCRIPT? Pros: Quick way to automate simple tasks. You (should) already know it. Always available. Good for one-shots. Cons: Hard to write reliable scripts. Limited expressivity. Shell scripts can quickly become cryptic. Manual dependency tracking. Limited reuse.