© 25.10.2018 David Buchmann

Pourquoi s'intéresser à bash?

# .travis.yml
before_script:
  - |
    set -e
    curl -L https://packagecloud.io/varnishcache/varnish60/gpgkey \
        | sudo apt-key add -
    curl -L "https://packagecloud.io/install/repositories/varnishcache/
varnish60/config_file.list?os=ubuntu&dist=trusty&source=script" \
        | sudo tee -a /etc/apt/sources.list
    sudo apt-get update -qq
    sudo apt-get install -qq varnish
            

Pourquoi s'intéresser à bash?

# Dockerfile
...
RUN curl -Lso libvmod-curl.tar.gz https://...libvmod-curl-1.0.4.tar.gz && \
    tar xzf libvmod-curl.tar.gz && \
    rm libvmod-curl.tar.gz && \
    cd libvmod-curl-1.0.4 && \
    sh autogen.sh && \
    sh configure && \
    make && \
    make install && \
    cd .. && \
    rm -rf libvmod-curl-1.0.4
...
            

Pourquoi s'intéresser à bash?

# deploy.sh
composer install --no-dev --prefer-dist --no-scripts \
    --no-interaction --no-progress --no-suggest \
    --ignore-platform-reqs

sed -i -e "s/###LICENSEKEY###/${NEWRELIC_LICENSE}/g" \
    newrelic/varnish-plugin/config/newrelic.json

cf-deploy --manifest=${MANIFEST} --space=${CF_SPACE} \
    --password="${CF_PASSWORD}" --app-check-path=/isalive \
    --hook-dir=.deploy/hooks --additional-domains="${DOMAINS}" \
    --cycle-app-guids
            

Bash

Table des matières




How to bash

Fondations

Mon premier script

#!/usr/bin/env bash

echo "Hello World!"
$ chmod a+x helloscript
$ ./helloscript
"Hello World!"

Chaque ligne exécutée comme sur la CLI

#!/usr/bin/env bash

cd /tmp
touch myfile
echo "hello" > test.txt
cat test.txt
            
$ ./demo
hello
$ ls /tmp
myfile  test.txt
            

Variables

#!/usr/bin/env bash

set -x # output every line before executing

foo=ls
$foo
echo $foo
echo `$foo`
            

Exécution

$ ./variables
+ foo=ls
+ ls
data.dump  notes.txt  variables
+ echo ls
ls
++ ls
+ echo data.dump notes.txt variables
data.dump notes.txt variables
            

Variables du système

#!/usr/bin/env bash

echo $MY_VAR
            
$ MY_VAR="hello" ./envvars
hello
            



How to bash

Lancer des autres applications

La philosophie Unix

«Do one thing and do it well»

Douglas McIlroy,
Computer Pioneer

Opérateurs

cat data.dump | sort -b | head -n 3 > output.txt
            

Trouver le mode d'utilisation




How to bash

Instruction "if"

Condition

if [ condition ]; then
    <instructions>
elif [ condition ]; then
    <instructions>
else
    <instructions>
fi
            

[ condition ] vs [[ condition ]]

condition[ ][[ ]]
fileévalue ? *littéral
string[ -n "$var" ][[ -n $var ]]
arithmetics-lt -gt -eq -ne< > == !=
bool logic-a et -o&& et ||

Autres constructions de bash




How to bash

Gestion des erreurs

Vérifier la réussite

# basic unix feature: return status code.
# 0 means success
# !0 indicates error

do-something-that-might-fail

if [[ 0 != $? ]]; then
    echo "An error happened!"
fi

Interrompre en cas d'erreur

# abort script on error
set -e

# fail immediately if any piped commands fails
set -o pipefail
cat data.notfound | sort -b | head -n 3

# aborts after `cat`, rather than executing `sort` and `head`
            



When not to bash

Limites du scripting

Limites

PHP script

#!/usr/bin/php

echo "hello world";
$ chmod a+x script.php
$ ./script.php
hello world

Alternatives pour Bash



Questions / Input / Feedback ?


Twitter: @dbu

Arguments

#!/usr/bin/env bash
echo $1
            
$ ./parameters my test
my
            

Share Variables

# parent
foo="foobar"
./child
export foo
./child
            
# child
echo "foo is $foo"
            
$ ./parent
foo is
foo is foobar
            

Pop Quiz

# parent
foo="foobar"
./child
export foo
./child
            
$ export foo=myvalue
$ ./parent
$ echo $foo
            
$ export foo=myvalue
$ ./parent
foo is foobar
foo is foobar
$ echo $foo
myvalue
            

Inheritance Rules

Functions

example() {
    local $MY = "some value"
    echo "My $MY function called with arg $1"
}
example "test"