SlideShare uma empresa Scribd logo
1 de 142
Baixar para ler offline
Useless Use of *                    Slide 1




                            Useless Use of *



                                Jan Schaumann
                            jschauma@netmeister.org
            PGP: 136D 027F DC29 8402 7B42 47D6 7C5B 64AF AF22 6A4C




Jan Schaumann                                                        SCALE5x
Useless Use of *    Slide 2

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 3

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$ groups ${ME}
netbsd sa yahoo
$




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 4

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$ groups ${ME}
netbsd sa yahoo
$




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 5

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$ groups ${ME}
netbsd sa yahoo
$




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 6

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$ groups ${ME}
netbsd sa yahoo
$




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 7

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$ groups ${ME}
netbsd sa yahoo
$




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 8

whoami

$ ME=$(id -un)
$ grep ${ME} /etc/passwd | cut -d: -f5
Jan Schaumann
$ groups ${ME}
netbsd sa yahoo
$




                    http://pipes.yahoo.com




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 9

Useless Use of... what?

Back in the day...




Jan Schaumann                                SCALE5x
Useless Use of *    Slide 10

Useless Use of... what?

Back in the day...




Jan Schaumann                                SCALE5x
Useless Use of *    Slide 11

Useless Use of... what?

Back in the day...




                          The Operator




Jan Schaumann                                 SCALE5x
Useless Use of *    Slide 12

Useless Use of... what?

Back in the day...




                             BOFH



Jan Schaumann                                SCALE5x
Useless Use of *    Slide 13

Useless Use of... what?

Back in the day...




Jan Schaumann                                SCALE5x
Useless Use of *    Slide 14

Useless Use of... what?

Back in the day...




Jan Schaumann                                SCALE5x
Useless Use of *    Slide 15

Useless Use of... what?

Back in the day...




Jan Schaumann                                SCALE5x
Useless Use of *    Slide 16

Useless Use of... what?

Back in the day...




Jan Schaumann                                SCALE5x
Useless Use of *    Slide 17

Useless Use of... what?




                          comp.unix.shell




Jan Schaumann                                    SCALE5x
Useless Use of *      Slide 18


Useless Use of... what?


I have a bunch of text files that contain strings
containing /u. I no longer want them to contain
/u ex: /u/appl/ should be /appl/....

Should I use awk? sed? Help!




Jan Schaumann                                  SCALE5x
Useless Use of *        Slide 19


Useless Use of... what?


>   I have a bunch of text files that contain strings
>   containing /u. I no longer want them to contain
>   /u ex: /u/appl/ should be /appl/....
>
>   Should I use awk? sed? Help!

cat file |      sed -e "s!/u/!/!"




Jan Schaumann                                    SCALE5x
Useless Use of *    Slide 20




                Useless Use of Cat




Jan Schaumann                           SCALE5x
Useless Use of *    Slide 21

Useful Use of Cat (?)

The obvious:
cat file | grep pattern
cat file | awk ’{ print $2; }’




Jan Schaumann                                      SCALE5x
Useless Use of *    Slide 22

Useless Use of Cat

The obvious:
cat file | grep pattern
cat file | awk ’{ print $2; }’




Jan Schaumann                                      SCALE5x
Useless Use of *    Slide 23

Useless Use of Cat

The obvious:
cat file | grep pattern
grep pattern file

cat file | awk ’{ print $2; }’
awk ’{ print $2; }’ < file




Jan Schaumann                                      SCALE5x
Useless Use of *    Slide 24

Useful Use of Cat (?)

cat file1 file2 file3 | wc -l
cat file1 file2 file3 | wc -w




Jan Schaumann                                   SCALE5x
Useless Use of *    Slide 25

Useless Use of Cat

cat file1 file2 file3 | wc -l
cat file1 file2 file3 | wc -w




Jan Schaumann                                   SCALE5x
Useless Use of *    Slide 26

Useless Use of Cat

cat file1 file2 file3 | wc -l
awk ’END { print NR }’ file1 file2 file3

cat file1 file2 file3 | wc -w
awk ’{w = w + NF} END { print w }’ file1 file2 file3




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 27

Useful Use of Cat (?)

cat file1 file2 file3 | grep pattern
if [ $(cat files | grep -c pattern) -gt 0 ]; then




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 28

Useless Use of Cat

cat file1 file2 file3 | grep pattern
if [ $(cat files | grep -c pattern) -gt 0 ]; then




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 29

Useless Use of Cat

cat file1 file2 file3 | grep pattern
grep -h pattern file1 file2 file3

if [ $(cat files | grep -c pattern) -gt 0 ]; then




Jan Schaumann                                          SCALE5x
Useless Use of *    Slide 30

Useless Use of Cat

cat file1 file2 file3 | grep pattern
grep -h pattern file1 file2 file3
awk ’/pattern/ { print }’ file1 file2 file3

if [ $(cat files | grep -c pattern) -gt 0 ]; then




Jan Schaumann                                          SCALE5x
Useless Use of *    Slide 31

Useless Use of Cat

cat file1 file2 file3 | grep pattern
grep -h pattern file1 file2 file3
awk ’/pattern/ { print }’ file1 file2 file3

if [ $(cat files | grep -c pattern) -gt 0 ]; then
if [ -n ”$(grep -l pattern files)” ]; then




Jan Schaumann                                          SCALE5x
Useless Use of *    Slide 32

Useless Use of Cat

cat file1 file2 file3 | grep pattern
grep -h pattern file1 file2 file3
awk ’/pattern/ { print }’ file1 file2 file3

if [ $(cat files | grep -c pattern) -gt 0 ]; then
if [ -n "$(grep -l pattern files)" ]; then
if grep pattern files >/dev/null 2>&1; then




Jan Schaumann                                          SCALE5x
Useless Use of *    Slide 33

Useful Use of Cat

    concatenate and print files




Jan Schaumann                                       SCALE5x
Useless Use of *    Slide 34

Useful Use of Cat

    concatenate and print files (D’oh!)

    cat * > file




Jan Schaumann                                           SCALE5x
Useless Use of *             Slide 35

Useful Use of Cat

    concatenate and print files (D’oh!)

    cat * > file

    feed file as input to another command in a particular order




Jan Schaumann                                                    SCALE5x
Useless Use of *             Slide 36

Useful Use of Cat

    concatenate and print files (D’oh!)

    cat * > file

    feed file as input to another command in a particular order

    { echo $VAR1; cat file; cmd1; } | command




Jan Schaumann                                                    SCALE5x
Useless Use of *             Slide 37

Useful Use of Cat

    concatenate and print files (D’oh!)

    cat * > file

    feed file as input to another command in a particular order

    { echo $VAR1; cat file; cmd1; } | command

    use as a NOOP




Jan Schaumann                                                    SCALE5x
Useless Use of *             Slide 38

Useful Use of Cat

    concatenate and print files (D’oh!)

    cat * > file

    feed file as input to another command in a particular order

    { echo $VAR1; cat file; cmd1; } | command

    use as a NOOP

    if condition; then
            cmd1 | cmd2 | cmd3
    else
            cmd1 | cmd3
    fi




Jan Schaumann                                                    SCALE5x
Useless Use of *             Slide 39

Useful Use of Cat

    concatenate and print files (D’oh!)

    cat * > file

    feed file as input to another command in a particular order

    { echo $VAR1; cat file; cmd1; } | command

    use as a NOOP

    if condition; then
            filter=cmd2
    else
            filter=cat
    fi
    cmd1 | ${filter} | cmd3




Jan Schaumann                                                    SCALE5x
Useless Use of *      Slide 40

Useful Use of Cat




                                  CAT5




                       CAT6


Jan Schaumann                            SCALE5x
Useless Use of *    Slide 41

Why bother?




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 42

Why bother?




                   $2.95




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 43

Why bother?




                   $5.90




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 44

Why bother?




                  $11.80




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 45

Why bother?




                  $23.60



Jan Schaumann                      SCALE5x
Useless Use of *    Slide 46

Why bother?




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 47

Why bother?




                  $47.20




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 48

Why bother?




                   $94.4




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 49

Why bother?




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 50

Why bother?




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 51

But...




Jan Schaumann                      SCALE5x
Useless Use of *        Slide 52

But...




                ... I run my scripts only once!




Jan Schaumann                                     SCALE5x
Useless Use of *       Slide 53

But...




                ... I have super fast hardware!




Jan Schaumann                                     SCALE5x
Useless Use of *                     Slide 54

But...




                 ... I have super fast hardware!
$ uptime
10:36AM up 254 days, 4 users, load averages: 80.12, 75.51, 72.40
$




Jan Schaumann                                                      SCALE5x
Useless Use of *          Slide 55

But...




                ... nobody else but me uses my code!




Jan Schaumann                                          SCALE5x
Useless Use of *        Slide 56

Oh, really?

Unfortunately, you really have no control over your code:




Jan Schaumann                                               SCALE5x
Useless Use of *        Slide 57

Oh, really?

Unfortunately, you really have no control over your code:
    code grows




Jan Schaumann                                               SCALE5x
Useless Use of *        Slide 58

Oh, really?

Unfortunately, you really have no control over your code:
    code grows
    code is reused




Jan Schaumann                                               SCALE5x
Useless Use of *        Slide 59

Oh, really?

Unfortunately, you really have no control over your code:
    code grows
    code is reused
    code moves with you




Jan Schaumann                                               SCALE5x
Useless Use of *        Slide 60

Oh, really?

Unfortunately, you really have no control over your code:
    code grows
    code is reused
    code moves with you
    code is left behind




Jan Schaumann                                               SCALE5x
Useless Use of *        Slide 61

Oh, really?

Unfortunately, you really have no control over your code:
    code grows
    code is reused
    code moves with you
    code is left behind




                                      It’s alive!

Jan Schaumann                                               SCALE5x
Useless Use of *    Slide 62

Oh, really?




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 63

Oh, really?




Jan Schaumann                      SCALE5x
Useless Use of *    Slide 64

Useless Use of *




                   Useless Use of Cat




Jan Schaumann                              SCALE5x
Useless Use of *    Slide 65

Useless Use of *




                   Useless Use of *




Jan Schaumann                             SCALE5x
Useless Use of *    Slide 66




                Useless Use of Grep




Jan Schaumann                            SCALE5x
Useless Use of *    Slide 67

Useless Use of Grep

Of course, all use of grep(1) is useless!




Jan Schaumann                                            SCALE5x
Useless Use of *    Slide 68

Useless Use of Grep

Of course, all use of grep(1) is useless!

echo g/RE/p | ed -s file




Jan Schaumann                                            SCALE5x
Useless Use of *            Slide 69

Useful Use of Grep (?)

host hostname | grep ’has address’ | awk ’{ print $NF }’
echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’




Jan Schaumann                                              SCALE5x
Useless Use of *            Slide 70

Useless Use of Grep

host hostname | grep ’has address’ | awk ’{ print $NF }’
echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’




Jan Schaumann                                              SCALE5x
Useless Use of *            Slide 71

Useless Use of Grep

host hostname | grep ’has address’ | awk ’{ print $NF }’
host hostname | awk ’/has address/ { print $NF }’

echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’




Jan Schaumann                                              SCALE5x
Useless Use of *            Slide 72

Useless Use of Grep

host hostname | grep ’has address’ | awk ’{ print $NF }’
host hostname | awk ’/has address/ { print $NF }’

echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’
echo ${string} | sed -ne ’/pattern/ s/foo/bar/p’




Jan Schaumann                                              SCALE5x
Useless Use of *    Slide 73

Useful Use of Grep (?)

grep pattern1 file ... | grep -v pattern2
grep pattern1 file | grep -v ^# | grep -v pattern2




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 74

Useless Use of Grep

grep pattern1 file ... | grep -v pattern2
grep pattern1 file | grep -v ^# | grep -v pattern2




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 75

Useless Use of Grep

grep pattern1 file ... | grep -v pattern2
awk ’/pattern1/ && !/pattern2/ { print }’ file ...

grep pattern1 file | grep -v ^# | grep -v pattern2




Jan Schaumann                                         SCALE5x
Useless Use of *     Slide 76

Useless Use of Grep

grep pattern1 file ... | grep -v pattern2
awk ’/pattern1/ && !/pattern2/ { print }’ file ...

grep pattern1 file | grep -v ^# | grep -v pattern2
awk ’/pattern1/ && !/(ˆ#)|(pattern2)/ { print }’ file




Jan Schaumann                                          SCALE5x
Useless Use of *    Slide 77

Useful Use of Sed (?)

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
        ls ${p}
done




Jan Schaumann                                      SCALE5x
Useless Use of *    Slide 78

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
        ls ${p}
done




Jan Schaumann                                      SCALE5x
Useless Use of *    Slide 79

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
        ls ${p}
done




Jan Schaumann                                      SCALE5x
Useless Use of *                    Slide 80

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
      ls ${p}
done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’




                                    Sed

Jan Schaumann                                                      SCALE5x
Useless Use of *                    Slide 81

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
      ls ${p}
done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’




                                Mknod ?

Jan Schaumann                                                      SCALE5x
Useless Use of *                    Slide 82

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
      ls ${p}
done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’




                                  Groff ?

Jan Schaumann                                                      SCALE5x
Useless Use of *                    Slide 83

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
      ls ${p}
done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’




                                Shlock ?

Jan Schaumann                                                      SCALE5x
Useless Use of *                    Slide 84

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
      ls ${p}
done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’




                                 Route ?

Jan Schaumann                                                      SCALE5x
Useless Use of *                    Slide 85

Useless Use of Sed

for p in $(echo ${PATH} | sed -e ’s/:/ /’); do
IFS=":"; for p in ${PATH}; do
      ls ${p}
done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’




                                 Dump ?


Jan Schaumann                                                      SCALE5x
Useless Use of *    Slide 86

Useless Use of Sed




Jan Schaumann                           SCALE5x
Useless Use of *    Slide 87

Useful Use of Sed (?)

VAR1="foo-bar-baz"
VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’)




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 88

Useless Use of Sed

VAR1="foo-bar-baz"
VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’)




Jan Schaumann                                     SCALE5x
Useless Use of *    Slide 89

Useless Use of Sed

VAR1="foo-bar-baz"
VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’)
VAR2=${VAR1%-baz}




Jan Schaumann                                      SCALE5x
Useless Use of *    Slide 90

Useful Use of Sed (?)

Looping over values in a variable:
VAR1="foo-bar-baz"
for i in $(echo ${VAR1} | sed -e ’s/-/ /g’); do
        the_needful $i
done




Jan Schaumann                                           SCALE5x
Useless Use of *    Slide 91

Useless Use of Sed

Looping over values in a variable:
VAR1="foo-bar-baz"
for i in $(echo ${VAR1} | sed -e ’s/-/ /g’); do
IFS=-
for i in ${VAR1}; do
        the_needful $i
done




Jan Schaumann                                      SCALE5x
Useless Use of *             Slide 92

Useful Use of Sed (?)

Assigning variables:
VAR1="foo-bar-baz"
VAR_A=$(echo ${VAR} | sed -e ’s/-*//’)
VAR_B=$(echo ${VAR} | sed -e ’s/[^-]*-([^-]*)-.*/1/’)
VAR_C=$(echo ${VAR} | sed -e ’s/.*-//’)




Jan Schaumann                                              SCALE5x
Useless Use of *       Slide 93

Useless Use of Sed

Assigning variables:
VAR1="foo-bar-baz"
VAR A=$(echo ${VAR} | sed -e ’s/-*//’)
VAR B=$(echo ${VAR} | sed -e ’s/[^-]*-[ˆ−]-.*/1/’)
VAR C=$(echo ${VAR} | sed -e ’s/.*-//’)
IFS=-
set -- ${VAR1}
VAR_A="${1}"
VAR_B="${2}"
VAR_C="${3}"




Jan Schaumann                                         SCALE5x
Useless Use of *    Slide 94

Useless Use of ...

Dude, IFS + shell is Teh Roxor!!!1
Look, Ma: Reading a CSV with Shell Only!


IFS=","
while read -r field1 waste field3 field4 waste; do
        echo "${field1}: ${field4} --> ${field3}"
done <file




Jan Schaumann                                         SCALE5x
Useless Use of *      Slide 95

Useless Use of Shell Only

$ cat >script.sh
IFS=","
while read -r waste field2 field3 waste; do
         echo "${field1}: ${field4} --> ${field3}"
done <file
^D
$ wc -l file
   111061 file
$ time sh script.sh >/dev/null
    12.33s real     3.30s user     8.54s system
$




Jan Schaumann                                        SCALE5x
Useless Use of *      Slide 96

Useless Use of Shell Only

Awk to the rescue!
$ cat >script2.sh
awk -F"," ’{ print $1 ": " $4 " --> " $3 }’ <file
^D
$ time sh script2.sh >/dev/null
    1.08s real     1.08s user     0.00s system
$




Jan Schaumann                                       SCALE5x
Useless Use of *    Slide 97

Useful Use of Ls (?)

for file in $(ls *pattern*); do
           the_needful
done




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 98

Useless Use of Ls

for file in $(ls *pattern*); do
for file in *pattern*; do
           the_needful
done




Jan Schaumann                                        SCALE5x
Useless Use of *    Slide 99

Useful Use of Wc (?)

VAR=$(cat file | wc -l | sed -e ’s/ *//g)’




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 100

Useful Use of Wc (?)

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 101

Useless Use of ... ?

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 102

Useless Use of ... ?

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 103

Useless Use of ... ?

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’
VAR=$(awk ’END { print NR }’ file)




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 104

Useless Use of Sed

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’
VAR=$(wc -l <file)




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 105

Useless Use of Sed

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’
VAR=$(wc -l <file)
echo "${VAR}"




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 106

Useless Use of Sed

VAR=$(cat file | wc -l | sed -e ’s/ *//g’)
VAR=$(wc -l <file| sed -e ’s/ *//g)’
VAR=$(wc -l <file)
echo "${VAR}"
echo ${VAR}




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 107

Useful Use of Head (?)

command1 | head -1 | sed -e ’s/pattern/string/’

command1 | head -10 | sed -e ’s/pattern/string/’




Jan Schaumann                                       SCALE5x
Useless Use of *   Slide 108

Useless Use of Head

command1 | head -1 | sed -e ’s/pattern/string/’
command1 | sed -e ’s/pattern/string/;q’

command1 | head -10 | sed -e ’s/pattern/string/’




Jan Schaumann                                       SCALE5x
Useless Use of *                     Slide 109

Useless Use of Head

command1 | head -1 | sed -e ’s/pattern/string/’
command1 | sed -e ’s/pattern/string/;q’

command1 | head -10 | sed -e ’s/pattern/string/’
command1 | awk ’{ if (NR <= 10) { print gensub("pattern","string",0); } }’




Jan Schaumann                                                         SCALE5x
Useless Use of *   Slide 110

Useful Use of Tail (?)

command1 | tail -1 | sed -e ’s/pattern/string/’




Jan Schaumann                                       SCALE5x
Useless Use of *                    Slide 111

Useless Use of Tail

command1 | tail -1 | sed -e ’s/pattern/string/’
command1 | awk ’END { print gensub("pattern","string",0); exit; }’




Jan Schaumann                                                        SCALE5x
Useless Use of *   Slide 112

Useful Use of Tail (?)

command1 | tail -10 | sed -e ’s/pattern/string/’




Jan Schaumann                                       SCALE5x
Useless Use of *   Slide 113

Useful Use of Tail

command1 | tail -10 | sed -e ’s/pattern/string/’




Jan Schaumann                                       SCALE5x
Useless Use of *   Slide 114

Useful Use of Expr (?)

echo $(expr $i + $i)




Jan Schaumann                               SCALE5x
Useless Use of *   Slide 115

Useless Use of Expr

echo $(expr $i + $i)
echo $(( $i + $i ))




Jan Schaumann                             SCALE5x
Useless Use of *                             Slide 116

Useless Use of Expr

echo $(expr $i + $i)
echo $(( $i + $i ))
echo $(( $i << 1 ))

This even lets you do binary manipulation (binary and, or, xor, not, leftshift, rightshift).




Jan Schaumann                                                                        SCALE5x
Useless Use of *                             Slide 117

Useless Use of Expr

echo $(expr $i + $i)
echo $(( $i + $i ))
echo $(( $i << 1 ))

This even lets you do binary manipulation (binary and, or, xor, not, leftshift, rightshift).


$ x=5
$ y=12
$ x=$(( ${x} ^ ${y} ))
$ y=$(( ${x} ^ ${y} ))
$ x=$(( ${x} ^ ${y} ))
$ echo "x=${x}; y=${y}"
x=12; y=5
$




Jan Schaumann                                                                        SCALE5x
Useless Use of *   Slide 118

Most Egregiously Useless Use of Perl

perl -e "print "ynnn";" | cmd




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 119

Most Egregiously Useless Use of Perl

perl -e "print "ynnn";" | cmd

( echo y; echo n; ) | cmd




Jan Schaumann                                      SCALE5x
Useless Use of *   Slide 120

Most Egregiously Useless Use of Perl

perl -e "print "ynnn";" | cmd

( echo y; echo n; ) | cmd
{ echo y; echo n; } | cmd
printf "ynnn" | cmd




Jan Schaumann                                      SCALE5x
Useless Use of *         Slide 121

Shell Coding: Performance

    avoid file I/O
    avoid Useless Use of *:
        use builtins when you can
        avoid builtins when it makes sense
        use the right tool for the job
    avoid spawning subshells (+1 fork) or pipes (n+1 forks)




Jan Schaumann                                                 SCALE5x
Useless Use of *         Slide 122

Shell Coding: Performance

    avoid file I/O
    avoid Useless Use of *:
        use builtins when you can
        avoid builtins when it makes sense
        use the right tool for the job
    avoid spawning subshells (+1 fork) or pipes (n+1 forks)
cut -f2 vs. awk ’{print $2}’
$ stat -f "%z     %N" /usr/bin/awk /usr/bin/cut
133487 /usr/bin/awk
12590   /usr/bin/cut
$




Jan Schaumann                                                 SCALE5x
Useless Use of *          Slide 123

Testing Shell Code

    make your code modular
    clearly define each function:
        pre-conditions
        valid input
        post-conditions
    prepare valid input and desired output of each function
    prepare invalid input and desired output of each function




Jan Schaumann                                                   SCALE5x
Useless Use of *   Slide 124

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 125

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
$ cat >script.sh
set -u
echo ${FOO}
echo "done"
^D
$ sh script.sh
script.sh: FOO: parameter not set
$ echo $?
2
$




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 126

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
$ cat >script.sh
set -e
ls /nowhere
echo "done"
^D
$ sh script.sh
ls: /nowhere: No such file or directory
$




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 127

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
$ cat >script.sh
set -e
if ! ls /nowhere; then
        echo "ls failed"
fi
echo "done"
^D
$ sh script.sh
ls: /nowhere: No such file or directory
ls failed
done
$




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 128

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
$ cat >script.sh
readonly VAR="foo"
VAR="bar"
echo ${VAR}
^D
$ sh script.sh
script.sh: VAR: is read only
$




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 129

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 130

Shell Coding Style: local variables

$ cat s.sh
func () {
        input="${1}"
        # do something with ${input}
}
$ cat script.sh
. ./s.sh
input="var1 var2 var3"
for var in ${input}; do
        func "${var}"
done
echo ${input}
$ sh script.sh
var3
$



Jan Schaumann                                     SCALE5x
Useless Use of *   Slide 131

Shell Coding Style: local variables

$ cat s.sh
func () {
           local input="${1}"
           # do something with ${input}
}
$ cat script.sh
. ./s.sh
input="var1 var2 var3"
for var in ${input}; do
        func "${var}"
done
echo ${input}
$ sh script.sh
var1 var2 var3
$


Jan Schaumann                                        SCALE5x
Useless Use of *   Slide 132

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 133

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code


Bad:
# this adds 1 to num
num=$(( ${num} + 1 ))




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 134

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code


Better:
input="pair1l:pair1r pair2l:pair2r"
# extract first pair in input string, throw away rest
p1l=${input%%:*}
rest=${input#*:}
p1r=${rest% *}




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 135

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code
    write your code in a modular way




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 136

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code
    write your code in a modular way
    write test cases for your shell code




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 137

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code
    write your code in a modular way
    write test cases for your shell code
    be consistent in your style




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 138

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code
    write your code in a modular way
    write test cases for your shell code
    be consistent in your style
    be consistent in your user interface




Jan Schaumann                                             SCALE5x
Useless Use of *   Slide 139

Shell Coding Style

Writing Shell Code is just like writing any other code:
    set -eu (perl-mongers: think -w and use strict;)
    use readonly variables
    use local variables
    comment your code
    write your code in a modular way
    write test cases for your shell code
    be consistent in your style
    be consistent in your user interface
    be willing to sacrifice performance for readability




Jan Schaumann                                             SCALE5x
Useless Use of *            Slide 140

Shell Coding: Performance vs. Readability

awk -F: -v ME="${ME}" ’{ if ($0 ~ ME) { print $5 }}’ file
vs.
grep ${ME} file | cut -d: -f5




awk ’{w = w + NF} END { print w }’ file1 file2 file3
vs.
cat file1 file2 file2 | wc -w




Jan Schaumann                                               SCALE5x
Useless Use of *   Slide 141

The KISS Principle




                     Keep It Simple, Stupid!


Jan Schaumann                                  SCALE5x
Useless Use of *   Slide 142

Useless Use of time (?)




                          Thanks!

Jan Schaumann                                SCALE5x

Mais conteúdo relacionado

Mais de Jan Schaumann

Primum non nocere - Ethical Obligations in Internet Operations
Primum non nocere - Ethical Obligations in Internet OperationsPrimum non nocere - Ethical Obligations in Internet Operations
Primum non nocere - Ethical Obligations in Internet OperationsJan Schaumann
 
Protecting Data in Untrusted Locations
Protecting Data in Untrusted LocationsProtecting Data in Untrusted Locations
Protecting Data in Untrusted LocationsJan Schaumann
 
Headless Host Scanning
Headless Host ScanningHeadless Host Scanning
Headless Host ScanningJan Schaumann
 
Safely Drinking from the Data Waterhose
Safely Drinking from the Data WaterhoseSafely Drinking from the Data Waterhose
Safely Drinking from the Data WaterhoseJan Schaumann
 
L3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load Balancing
L3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load BalancingL3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load Balancing
L3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load BalancingJan Schaumann
 
Building better tools
Building better toolsBuilding better tools
Building better toolsJan Schaumann
 

Mais de Jan Schaumann (11)

Defense at Scale
Defense at ScaleDefense at Scale
Defense at Scale
 
Primum non nocere - Ethical Obligations in Internet Operations
Primum non nocere - Ethical Obligations in Internet OperationsPrimum non nocere - Ethical Obligations in Internet Operations
Primum non nocere - Ethical Obligations in Internet Operations
 
Protecting Data in Untrusted Locations
Protecting Data in Untrusted LocationsProtecting Data in Untrusted Locations
Protecting Data in Untrusted Locations
 
Headless Host Scanning
Headless Host ScanningHeadless Host Scanning
Headless Host Scanning
 
Safely Drinking from the Data Waterhose
Safely Drinking from the Data WaterhoseSafely Drinking from the Data Waterhose
Safely Drinking from the Data Waterhose
 
PGP for Smarties
PGP for SmartiesPGP for Smarties
PGP for Smarties
 
Fancy pants
Fancy pantsFancy pants
Fancy pants
 
Ipv6 basics
Ipv6 basicsIpv6 basics
Ipv6 basics
 
L3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load Balancing
L3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load BalancingL3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load Balancing
L3DSR - Overcoming Layer 2 Limitations of Direct Server Return Load Balancing
 
Building better tools
Building better toolsBuilding better tools
Building better tools
 
DST @ Yahoo!
DST @ Yahoo!DST @ Yahoo!
DST @ Yahoo!
 

Último

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Useless use of *

  • 1. Useless Use of * Slide 1 Useless Use of * Jan Schaumann jschauma@netmeister.org PGP: 136D 027F DC29 8402 7B42 47D6 7C5B 64AF AF22 6A4C Jan Schaumann SCALE5x
  • 2. Useless Use of * Slide 2 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ Jan Schaumann SCALE5x
  • 3. Useless Use of * Slide 3 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ groups ${ME} netbsd sa yahoo $ Jan Schaumann SCALE5x
  • 4. Useless Use of * Slide 4 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ groups ${ME} netbsd sa yahoo $ Jan Schaumann SCALE5x
  • 5. Useless Use of * Slide 5 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ groups ${ME} netbsd sa yahoo $ Jan Schaumann SCALE5x
  • 6. Useless Use of * Slide 6 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ groups ${ME} netbsd sa yahoo $ Jan Schaumann SCALE5x
  • 7. Useless Use of * Slide 7 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ groups ${ME} netbsd sa yahoo $ Jan Schaumann SCALE5x
  • 8. Useless Use of * Slide 8 whoami $ ME=$(id -un) $ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann $ groups ${ME} netbsd sa yahoo $ http://pipes.yahoo.com Jan Schaumann SCALE5x
  • 9. Useless Use of * Slide 9 Useless Use of... what? Back in the day... Jan Schaumann SCALE5x
  • 10. Useless Use of * Slide 10 Useless Use of... what? Back in the day... Jan Schaumann SCALE5x
  • 11. Useless Use of * Slide 11 Useless Use of... what? Back in the day... The Operator Jan Schaumann SCALE5x
  • 12. Useless Use of * Slide 12 Useless Use of... what? Back in the day... BOFH Jan Schaumann SCALE5x
  • 13. Useless Use of * Slide 13 Useless Use of... what? Back in the day... Jan Schaumann SCALE5x
  • 14. Useless Use of * Slide 14 Useless Use of... what? Back in the day... Jan Schaumann SCALE5x
  • 15. Useless Use of * Slide 15 Useless Use of... what? Back in the day... Jan Schaumann SCALE5x
  • 16. Useless Use of * Slide 16 Useless Use of... what? Back in the day... Jan Schaumann SCALE5x
  • 17. Useless Use of * Slide 17 Useless Use of... what? comp.unix.shell Jan Schaumann SCALE5x
  • 18. Useless Use of * Slide 18 Useless Use of... what? I have a bunch of text files that contain strings containing /u. I no longer want them to contain /u ex: /u/appl/ should be /appl/.... Should I use awk? sed? Help! Jan Schaumann SCALE5x
  • 19. Useless Use of * Slide 19 Useless Use of... what? > I have a bunch of text files that contain strings > containing /u. I no longer want them to contain > /u ex: /u/appl/ should be /appl/.... > > Should I use awk? sed? Help! cat file | sed -e "s!/u/!/!" Jan Schaumann SCALE5x
  • 20. Useless Use of * Slide 20 Useless Use of Cat Jan Schaumann SCALE5x
  • 21. Useless Use of * Slide 21 Useful Use of Cat (?) The obvious: cat file | grep pattern cat file | awk ’{ print $2; }’ Jan Schaumann SCALE5x
  • 22. Useless Use of * Slide 22 Useless Use of Cat The obvious: cat file | grep pattern cat file | awk ’{ print $2; }’ Jan Schaumann SCALE5x
  • 23. Useless Use of * Slide 23 Useless Use of Cat The obvious: cat file | grep pattern grep pattern file cat file | awk ’{ print $2; }’ awk ’{ print $2; }’ < file Jan Schaumann SCALE5x
  • 24. Useless Use of * Slide 24 Useful Use of Cat (?) cat file1 file2 file3 | wc -l cat file1 file2 file3 | wc -w Jan Schaumann SCALE5x
  • 25. Useless Use of * Slide 25 Useless Use of Cat cat file1 file2 file3 | wc -l cat file1 file2 file3 | wc -w Jan Schaumann SCALE5x
  • 26. Useless Use of * Slide 26 Useless Use of Cat cat file1 file2 file3 | wc -l awk ’END { print NR }’ file1 file2 file3 cat file1 file2 file3 | wc -w awk ’{w = w + NF} END { print w }’ file1 file2 file3 Jan Schaumann SCALE5x
  • 27. Useless Use of * Slide 27 Useful Use of Cat (?) cat file1 file2 file3 | grep pattern if [ $(cat files | grep -c pattern) -gt 0 ]; then Jan Schaumann SCALE5x
  • 28. Useless Use of * Slide 28 Useless Use of Cat cat file1 file2 file3 | grep pattern if [ $(cat files | grep -c pattern) -gt 0 ]; then Jan Schaumann SCALE5x
  • 29. Useless Use of * Slide 29 Useless Use of Cat cat file1 file2 file3 | grep pattern grep -h pattern file1 file2 file3 if [ $(cat files | grep -c pattern) -gt 0 ]; then Jan Schaumann SCALE5x
  • 30. Useless Use of * Slide 30 Useless Use of Cat cat file1 file2 file3 | grep pattern grep -h pattern file1 file2 file3 awk ’/pattern/ { print }’ file1 file2 file3 if [ $(cat files | grep -c pattern) -gt 0 ]; then Jan Schaumann SCALE5x
  • 31. Useless Use of * Slide 31 Useless Use of Cat cat file1 file2 file3 | grep pattern grep -h pattern file1 file2 file3 awk ’/pattern/ { print }’ file1 file2 file3 if [ $(cat files | grep -c pattern) -gt 0 ]; then if [ -n ”$(grep -l pattern files)” ]; then Jan Schaumann SCALE5x
  • 32. Useless Use of * Slide 32 Useless Use of Cat cat file1 file2 file3 | grep pattern grep -h pattern file1 file2 file3 awk ’/pattern/ { print }’ file1 file2 file3 if [ $(cat files | grep -c pattern) -gt 0 ]; then if [ -n "$(grep -l pattern files)" ]; then if grep pattern files >/dev/null 2>&1; then Jan Schaumann SCALE5x
  • 33. Useless Use of * Slide 33 Useful Use of Cat concatenate and print files Jan Schaumann SCALE5x
  • 34. Useless Use of * Slide 34 Useful Use of Cat concatenate and print files (D’oh!) cat * > file Jan Schaumann SCALE5x
  • 35. Useless Use of * Slide 35 Useful Use of Cat concatenate and print files (D’oh!) cat * > file feed file as input to another command in a particular order Jan Schaumann SCALE5x
  • 36. Useless Use of * Slide 36 Useful Use of Cat concatenate and print files (D’oh!) cat * > file feed file as input to another command in a particular order { echo $VAR1; cat file; cmd1; } | command Jan Schaumann SCALE5x
  • 37. Useless Use of * Slide 37 Useful Use of Cat concatenate and print files (D’oh!) cat * > file feed file as input to another command in a particular order { echo $VAR1; cat file; cmd1; } | command use as a NOOP Jan Schaumann SCALE5x
  • 38. Useless Use of * Slide 38 Useful Use of Cat concatenate and print files (D’oh!) cat * > file feed file as input to another command in a particular order { echo $VAR1; cat file; cmd1; } | command use as a NOOP if condition; then cmd1 | cmd2 | cmd3 else cmd1 | cmd3 fi Jan Schaumann SCALE5x
  • 39. Useless Use of * Slide 39 Useful Use of Cat concatenate and print files (D’oh!) cat * > file feed file as input to another command in a particular order { echo $VAR1; cat file; cmd1; } | command use as a NOOP if condition; then filter=cmd2 else filter=cat fi cmd1 | ${filter} | cmd3 Jan Schaumann SCALE5x
  • 40. Useless Use of * Slide 40 Useful Use of Cat CAT5 CAT6 Jan Schaumann SCALE5x
  • 41. Useless Use of * Slide 41 Why bother? Jan Schaumann SCALE5x
  • 42. Useless Use of * Slide 42 Why bother? $2.95 Jan Schaumann SCALE5x
  • 43. Useless Use of * Slide 43 Why bother? $5.90 Jan Schaumann SCALE5x
  • 44. Useless Use of * Slide 44 Why bother? $11.80 Jan Schaumann SCALE5x
  • 45. Useless Use of * Slide 45 Why bother? $23.60 Jan Schaumann SCALE5x
  • 46. Useless Use of * Slide 46 Why bother? Jan Schaumann SCALE5x
  • 47. Useless Use of * Slide 47 Why bother? $47.20 Jan Schaumann SCALE5x
  • 48. Useless Use of * Slide 48 Why bother? $94.4 Jan Schaumann SCALE5x
  • 49. Useless Use of * Slide 49 Why bother? Jan Schaumann SCALE5x
  • 50. Useless Use of * Slide 50 Why bother? Jan Schaumann SCALE5x
  • 51. Useless Use of * Slide 51 But... Jan Schaumann SCALE5x
  • 52. Useless Use of * Slide 52 But... ... I run my scripts only once! Jan Schaumann SCALE5x
  • 53. Useless Use of * Slide 53 But... ... I have super fast hardware! Jan Schaumann SCALE5x
  • 54. Useless Use of * Slide 54 But... ... I have super fast hardware! $ uptime 10:36AM up 254 days, 4 users, load averages: 80.12, 75.51, 72.40 $ Jan Schaumann SCALE5x
  • 55. Useless Use of * Slide 55 But... ... nobody else but me uses my code! Jan Schaumann SCALE5x
  • 56. Useless Use of * Slide 56 Oh, really? Unfortunately, you really have no control over your code: Jan Schaumann SCALE5x
  • 57. Useless Use of * Slide 57 Oh, really? Unfortunately, you really have no control over your code: code grows Jan Schaumann SCALE5x
  • 58. Useless Use of * Slide 58 Oh, really? Unfortunately, you really have no control over your code: code grows code is reused Jan Schaumann SCALE5x
  • 59. Useless Use of * Slide 59 Oh, really? Unfortunately, you really have no control over your code: code grows code is reused code moves with you Jan Schaumann SCALE5x
  • 60. Useless Use of * Slide 60 Oh, really? Unfortunately, you really have no control over your code: code grows code is reused code moves with you code is left behind Jan Schaumann SCALE5x
  • 61. Useless Use of * Slide 61 Oh, really? Unfortunately, you really have no control over your code: code grows code is reused code moves with you code is left behind It’s alive! Jan Schaumann SCALE5x
  • 62. Useless Use of * Slide 62 Oh, really? Jan Schaumann SCALE5x
  • 63. Useless Use of * Slide 63 Oh, really? Jan Schaumann SCALE5x
  • 64. Useless Use of * Slide 64 Useless Use of * Useless Use of Cat Jan Schaumann SCALE5x
  • 65. Useless Use of * Slide 65 Useless Use of * Useless Use of * Jan Schaumann SCALE5x
  • 66. Useless Use of * Slide 66 Useless Use of Grep Jan Schaumann SCALE5x
  • 67. Useless Use of * Slide 67 Useless Use of Grep Of course, all use of grep(1) is useless! Jan Schaumann SCALE5x
  • 68. Useless Use of * Slide 68 Useless Use of Grep Of course, all use of grep(1) is useless! echo g/RE/p | ed -s file Jan Schaumann SCALE5x
  • 69. Useless Use of * Slide 69 Useful Use of Grep (?) host hostname | grep ’has address’ | awk ’{ print $NF }’ echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’ Jan Schaumann SCALE5x
  • 70. Useless Use of * Slide 70 Useless Use of Grep host hostname | grep ’has address’ | awk ’{ print $NF }’ echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’ Jan Schaumann SCALE5x
  • 71. Useless Use of * Slide 71 Useless Use of Grep host hostname | grep ’has address’ | awk ’{ print $NF }’ host hostname | awk ’/has address/ { print $NF }’ echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’ Jan Schaumann SCALE5x
  • 72. Useless Use of * Slide 72 Useless Use of Grep host hostname | grep ’has address’ | awk ’{ print $NF }’ host hostname | awk ’/has address/ { print $NF }’ echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’ echo ${string} | sed -ne ’/pattern/ s/foo/bar/p’ Jan Schaumann SCALE5x
  • 73. Useless Use of * Slide 73 Useful Use of Grep (?) grep pattern1 file ... | grep -v pattern2 grep pattern1 file | grep -v ^# | grep -v pattern2 Jan Schaumann SCALE5x
  • 74. Useless Use of * Slide 74 Useless Use of Grep grep pattern1 file ... | grep -v pattern2 grep pattern1 file | grep -v ^# | grep -v pattern2 Jan Schaumann SCALE5x
  • 75. Useless Use of * Slide 75 Useless Use of Grep grep pattern1 file ... | grep -v pattern2 awk ’/pattern1/ && !/pattern2/ { print }’ file ... grep pattern1 file | grep -v ^# | grep -v pattern2 Jan Schaumann SCALE5x
  • 76. Useless Use of * Slide 76 Useless Use of Grep grep pattern1 file ... | grep -v pattern2 awk ’/pattern1/ && !/pattern2/ { print }’ file ... grep pattern1 file | grep -v ^# | grep -v pattern2 awk ’/pattern1/ && !/(ˆ#)|(pattern2)/ { print }’ file Jan Schaumann SCALE5x
  • 77. Useless Use of * Slide 77 Useful Use of Sed (?) for p in $(echo ${PATH} | sed -e ’s/:/ /’); do ls ${p} done Jan Schaumann SCALE5x
  • 78. Useless Use of * Slide 78 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do ls ${p} done Jan Schaumann SCALE5x
  • 79. Useless Use of * Slide 79 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done Jan Schaumann SCALE5x
  • 80. Useless Use of * Slide 80 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Sed Jan Schaumann SCALE5x
  • 81. Useless Use of * Slide 81 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Mknod ? Jan Schaumann SCALE5x
  • 82. Useless Use of * Slide 82 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Groff ? Jan Schaumann SCALE5x
  • 83. Useless Use of * Slide 83 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Shlock ? Jan Schaumann SCALE5x
  • 84. Useless Use of * Slide 84 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Route ? Jan Schaumann SCALE5x
  • 85. Useless Use of * Slide 85 Useless Use of Sed for p in $(echo ${PATH} | sed -e ’s/:/ /’); do IFS=":"; for p in ${PATH}; do ls ${p} done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Dump ? Jan Schaumann SCALE5x
  • 86. Useless Use of * Slide 86 Useless Use of Sed Jan Schaumann SCALE5x
  • 87. Useless Use of * Slide 87 Useful Use of Sed (?) VAR1="foo-bar-baz" VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’) Jan Schaumann SCALE5x
  • 88. Useless Use of * Slide 88 Useless Use of Sed VAR1="foo-bar-baz" VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’) Jan Schaumann SCALE5x
  • 89. Useless Use of * Slide 89 Useless Use of Sed VAR1="foo-bar-baz" VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’) VAR2=${VAR1%-baz} Jan Schaumann SCALE5x
  • 90. Useless Use of * Slide 90 Useful Use of Sed (?) Looping over values in a variable: VAR1="foo-bar-baz" for i in $(echo ${VAR1} | sed -e ’s/-/ /g’); do the_needful $i done Jan Schaumann SCALE5x
  • 91. Useless Use of * Slide 91 Useless Use of Sed Looping over values in a variable: VAR1="foo-bar-baz" for i in $(echo ${VAR1} | sed -e ’s/-/ /g’); do IFS=- for i in ${VAR1}; do the_needful $i done Jan Schaumann SCALE5x
  • 92. Useless Use of * Slide 92 Useful Use of Sed (?) Assigning variables: VAR1="foo-bar-baz" VAR_A=$(echo ${VAR} | sed -e ’s/-*//’) VAR_B=$(echo ${VAR} | sed -e ’s/[^-]*-([^-]*)-.*/1/’) VAR_C=$(echo ${VAR} | sed -e ’s/.*-//’) Jan Schaumann SCALE5x
  • 93. Useless Use of * Slide 93 Useless Use of Sed Assigning variables: VAR1="foo-bar-baz" VAR A=$(echo ${VAR} | sed -e ’s/-*//’) VAR B=$(echo ${VAR} | sed -e ’s/[^-]*-[ˆ−]-.*/1/’) VAR C=$(echo ${VAR} | sed -e ’s/.*-//’) IFS=- set -- ${VAR1} VAR_A="${1}" VAR_B="${2}" VAR_C="${3}" Jan Schaumann SCALE5x
  • 94. Useless Use of * Slide 94 Useless Use of ... Dude, IFS + shell is Teh Roxor!!!1 Look, Ma: Reading a CSV with Shell Only! IFS="," while read -r field1 waste field3 field4 waste; do echo "${field1}: ${field4} --> ${field3}" done <file Jan Schaumann SCALE5x
  • 95. Useless Use of * Slide 95 Useless Use of Shell Only $ cat >script.sh IFS="," while read -r waste field2 field3 waste; do echo "${field1}: ${field4} --> ${field3}" done <file ^D $ wc -l file 111061 file $ time sh script.sh >/dev/null 12.33s real 3.30s user 8.54s system $ Jan Schaumann SCALE5x
  • 96. Useless Use of * Slide 96 Useless Use of Shell Only Awk to the rescue! $ cat >script2.sh awk -F"," ’{ print $1 ": " $4 " --> " $3 }’ <file ^D $ time sh script2.sh >/dev/null 1.08s real 1.08s user 0.00s system $ Jan Schaumann SCALE5x
  • 97. Useless Use of * Slide 97 Useful Use of Ls (?) for file in $(ls *pattern*); do the_needful done Jan Schaumann SCALE5x
  • 98. Useless Use of * Slide 98 Useless Use of Ls for file in $(ls *pattern*); do for file in *pattern*; do the_needful done Jan Schaumann SCALE5x
  • 99. Useless Use of * Slide 99 Useful Use of Wc (?) VAR=$(cat file | wc -l | sed -e ’s/ *//g)’ Jan Schaumann SCALE5x
  • 100. Useless Use of * Slide 100 Useful Use of Wc (?) VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ Jan Schaumann SCALE5x
  • 101. Useless Use of * Slide 101 Useless Use of ... ? VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ Jan Schaumann SCALE5x
  • 102. Useless Use of * Slide 102 Useless Use of ... ? VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ Jan Schaumann SCALE5x
  • 103. Useless Use of * Slide 103 Useless Use of ... ? VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ VAR=$(awk ’END { print NR }’ file) Jan Schaumann SCALE5x
  • 104. Useless Use of * Slide 104 Useless Use of Sed VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ VAR=$(wc -l <file) Jan Schaumann SCALE5x
  • 105. Useless Use of * Slide 105 Useless Use of Sed VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ VAR=$(wc -l <file) echo "${VAR}" Jan Schaumann SCALE5x
  • 106. Useless Use of * Slide 106 Useless Use of Sed VAR=$(cat file | wc -l | sed -e ’s/ *//g’) VAR=$(wc -l <file| sed -e ’s/ *//g)’ VAR=$(wc -l <file) echo "${VAR}" echo ${VAR} Jan Schaumann SCALE5x
  • 107. Useless Use of * Slide 107 Useful Use of Head (?) command1 | head -1 | sed -e ’s/pattern/string/’ command1 | head -10 | sed -e ’s/pattern/string/’ Jan Schaumann SCALE5x
  • 108. Useless Use of * Slide 108 Useless Use of Head command1 | head -1 | sed -e ’s/pattern/string/’ command1 | sed -e ’s/pattern/string/;q’ command1 | head -10 | sed -e ’s/pattern/string/’ Jan Schaumann SCALE5x
  • 109. Useless Use of * Slide 109 Useless Use of Head command1 | head -1 | sed -e ’s/pattern/string/’ command1 | sed -e ’s/pattern/string/;q’ command1 | head -10 | sed -e ’s/pattern/string/’ command1 | awk ’{ if (NR <= 10) { print gensub("pattern","string",0); } }’ Jan Schaumann SCALE5x
  • 110. Useless Use of * Slide 110 Useful Use of Tail (?) command1 | tail -1 | sed -e ’s/pattern/string/’ Jan Schaumann SCALE5x
  • 111. Useless Use of * Slide 111 Useless Use of Tail command1 | tail -1 | sed -e ’s/pattern/string/’ command1 | awk ’END { print gensub("pattern","string",0); exit; }’ Jan Schaumann SCALE5x
  • 112. Useless Use of * Slide 112 Useful Use of Tail (?) command1 | tail -10 | sed -e ’s/pattern/string/’ Jan Schaumann SCALE5x
  • 113. Useless Use of * Slide 113 Useful Use of Tail command1 | tail -10 | sed -e ’s/pattern/string/’ Jan Schaumann SCALE5x
  • 114. Useless Use of * Slide 114 Useful Use of Expr (?) echo $(expr $i + $i) Jan Schaumann SCALE5x
  • 115. Useless Use of * Slide 115 Useless Use of Expr echo $(expr $i + $i) echo $(( $i + $i )) Jan Schaumann SCALE5x
  • 116. Useless Use of * Slide 116 Useless Use of Expr echo $(expr $i + $i) echo $(( $i + $i )) echo $(( $i << 1 )) This even lets you do binary manipulation (binary and, or, xor, not, leftshift, rightshift). Jan Schaumann SCALE5x
  • 117. Useless Use of * Slide 117 Useless Use of Expr echo $(expr $i + $i) echo $(( $i + $i )) echo $(( $i << 1 )) This even lets you do binary manipulation (binary and, or, xor, not, leftshift, rightshift). $ x=5 $ y=12 $ x=$(( ${x} ^ ${y} )) $ y=$(( ${x} ^ ${y} )) $ x=$(( ${x} ^ ${y} )) $ echo "x=${x}; y=${y}" x=12; y=5 $ Jan Schaumann SCALE5x
  • 118. Useless Use of * Slide 118 Most Egregiously Useless Use of Perl perl -e "print "ynnn";" | cmd Jan Schaumann SCALE5x
  • 119. Useless Use of * Slide 119 Most Egregiously Useless Use of Perl perl -e "print "ynnn";" | cmd ( echo y; echo n; ) | cmd Jan Schaumann SCALE5x
  • 120. Useless Use of * Slide 120 Most Egregiously Useless Use of Perl perl -e "print "ynnn";" | cmd ( echo y; echo n; ) | cmd { echo y; echo n; } | cmd printf "ynnn" | cmd Jan Schaumann SCALE5x
  • 121. Useless Use of * Slide 121 Shell Coding: Performance avoid file I/O avoid Useless Use of *: use builtins when you can avoid builtins when it makes sense use the right tool for the job avoid spawning subshells (+1 fork) or pipes (n+1 forks) Jan Schaumann SCALE5x
  • 122. Useless Use of * Slide 122 Shell Coding: Performance avoid file I/O avoid Useless Use of *: use builtins when you can avoid builtins when it makes sense use the right tool for the job avoid spawning subshells (+1 fork) or pipes (n+1 forks) cut -f2 vs. awk ’{print $2}’ $ stat -f "%z %N" /usr/bin/awk /usr/bin/cut 133487 /usr/bin/awk 12590 /usr/bin/cut $ Jan Schaumann SCALE5x
  • 123. Useless Use of * Slide 123 Testing Shell Code make your code modular clearly define each function: pre-conditions valid input post-conditions prepare valid input and desired output of each function prepare invalid input and desired output of each function Jan Schaumann SCALE5x
  • 124. Useless Use of * Slide 124 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) Jan Schaumann SCALE5x
  • 125. Useless Use of * Slide 125 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) $ cat >script.sh set -u echo ${FOO} echo "done" ^D $ sh script.sh script.sh: FOO: parameter not set $ echo $? 2 $ Jan Schaumann SCALE5x
  • 126. Useless Use of * Slide 126 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) $ cat >script.sh set -e ls /nowhere echo "done" ^D $ sh script.sh ls: /nowhere: No such file or directory $ Jan Schaumann SCALE5x
  • 127. Useless Use of * Slide 127 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) $ cat >script.sh set -e if ! ls /nowhere; then echo "ls failed" fi echo "done" ^D $ sh script.sh ls: /nowhere: No such file or directory ls failed done $ Jan Schaumann SCALE5x
  • 128. Useless Use of * Slide 128 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables $ cat >script.sh readonly VAR="foo" VAR="bar" echo ${VAR} ^D $ sh script.sh script.sh: VAR: is read only $ Jan Schaumann SCALE5x
  • 129. Useless Use of * Slide 129 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables Jan Schaumann SCALE5x
  • 130. Useless Use of * Slide 130 Shell Coding Style: local variables $ cat s.sh func () { input="${1}" # do something with ${input} } $ cat script.sh . ./s.sh input="var1 var2 var3" for var in ${input}; do func "${var}" done echo ${input} $ sh script.sh var3 $ Jan Schaumann SCALE5x
  • 131. Useless Use of * Slide 131 Shell Coding Style: local variables $ cat s.sh func () { local input="${1}" # do something with ${input} } $ cat script.sh . ./s.sh input="var1 var2 var3" for var in ${input}; do func "${var}" done echo ${input} $ sh script.sh var1 var2 var3 $ Jan Schaumann SCALE5x
  • 132. Useless Use of * Slide 132 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code Jan Schaumann SCALE5x
  • 133. Useless Use of * Slide 133 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code Bad: # this adds 1 to num num=$(( ${num} + 1 )) Jan Schaumann SCALE5x
  • 134. Useless Use of * Slide 134 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code Better: input="pair1l:pair1r pair2l:pair2r" # extract first pair in input string, throw away rest p1l=${input%%:*} rest=${input#*:} p1r=${rest% *} Jan Schaumann SCALE5x
  • 135. Useless Use of * Slide 135 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code write your code in a modular way Jan Schaumann SCALE5x
  • 136. Useless Use of * Slide 136 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code write your code in a modular way write test cases for your shell code Jan Schaumann SCALE5x
  • 137. Useless Use of * Slide 137 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code write your code in a modular way write test cases for your shell code be consistent in your style Jan Schaumann SCALE5x
  • 138. Useless Use of * Slide 138 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code write your code in a modular way write test cases for your shell code be consistent in your style be consistent in your user interface Jan Schaumann SCALE5x
  • 139. Useless Use of * Slide 139 Shell Coding Style Writing Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variables comment your code write your code in a modular way write test cases for your shell code be consistent in your style be consistent in your user interface be willing to sacrifice performance for readability Jan Schaumann SCALE5x
  • 140. Useless Use of * Slide 140 Shell Coding: Performance vs. Readability awk -F: -v ME="${ME}" ’{ if ($0 ~ ME) { print $5 }}’ file vs. grep ${ME} file | cut -d: -f5 awk ’{w = w + NF} END { print w }’ file1 file2 file3 vs. cat file1 file2 file2 | wc -w Jan Schaumann SCALE5x
  • 141. Useless Use of * Slide 141 The KISS Principle Keep It Simple, Stupid! Jan Schaumann SCALE5x
  • 142. Useless Use of * Slide 142 Useless Use of time (?) Thanks! Jan Schaumann SCALE5x