• No results found

Useless Use of * Slide 1. Useless Use of * Jan Schaumann PGP: 136D 027F DC B42 47D6 7C5B 64AF AF22 6A4C

N/A
N/A
Protected

Academic year: 2021

Share "Useless Use of * Slide 1. Useless Use of * Jan Schaumann PGP: 136D 027F DC B42 47D6 7C5B 64AF AF22 6A4C"

Copied!
142
0
0

Loading.... (view fulltext now)

Full text

(1)

Useless Use of * Slide 1

Useless Use of *

Jan Schaumann

[email protected]

(2)

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

(3)

Useless Use of * Slide 3

whoami

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

$ groups ${ME} netbsd sa yahoo $

(4)

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

$ groups ${ME} netbsd sa yahoo $

(5)

Useless Use of * Slide 5

whoami

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

$ groups ${ME} netbsd sa yahoo $

(6)

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

$ groups ${ME} netbsd sa yahoo $

(7)

Useless Use of * Slide 7

whoami

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

$ groups ${ME} netbsd sa yahoo $

(8)

$ ME=$(id -un)

$ grep ${ME} /etc/passwd | cut -d: -f5 Jan Schaumann

$ groups ${ME} netbsd sa yahoo $

(9)

Useless Use of * Slide 9

Useless Use of... what?

(10)
(11)

Useless Use of * Slide 11

Useless Use of... what?

Back in the day...

(12)
(13)

Useless Use of * Slide 13

Useless Use of... what?

(14)
(15)

Useless Use of * Slide 15

Useless Use of... what?

(16)
(17)

Useless Use of * Slide 17

Useless Use of... what?

(18)

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/....

(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/!/!"

(20)
(21)

Useless Use of * Slide 21

Useful Use of Cat (?)

The obvious:

cat file | grep pattern

(22)

cat file | grep pattern

(23)

Useless Use of * Slide 23

Useless Use of Cat

The obvious:

cat file | grep pattern

grep pattern file

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

(24)
(25)

Useless Use of * Slide 25

Useless Use of Cat

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

(26)

awk ’END { print NR }’ file1 file2 file3

cat file1 file2 file3 | wc -w

(27)

Useless Use of * Slide 27

Useful Use of Cat (?)

cat file1 file2 file3 | grep pattern

(28)
(29)

Useless Use of * Slide 29

Useless Use of Cat

cat file1 file2 file3 | grep pattern

grep -h pattern file1 file2 file3

(30)

grep -h pattern file1 file2 file3

awk ’/pattern/ { print }’ file1 file2 file3

(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

(32)

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

(33)

Useless Use of * Slide 33

Useful Use of Cat

(34)

concatenate and print files (D’oh!)

(35)

Useless Use of * Slide 35

Useful Use of Cat

concatenate and print files (D’oh!)

cat * > file

(36)

concatenate and print files (D’oh!)

cat * > file

feed file as input to another command in a particular order

(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

(38)

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

(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

(40)

CAT5

(41)

Useless Use of * Slide 41

Why bother?

(42)
(43)

Useless Use of * Slide 43

Why bother?

(44)
(45)

Useless Use of * Slide 45

Why bother?

(46)
(47)

Useless Use of * Slide 47

Why bother?

(48)
(49)

Useless Use of * Slide 49

Why bother?

(50)
(51)

Useless Use of * Slide 51

But...

(52)
(53)

Useless Use of * Slide 53

But...

(54)

... I have super fast hardware!

$ uptime

10:36AM up 254 days, 4 users, load averages: 80.12, 75.51, 72.40 $

(55)

Useless Use of * Slide 55

But...

(56)
(57)

Useless Use of * Slide 57

Oh, really?

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

(58)

code grows code is reused

(59)

Useless Use of * Slide 59

Oh, really?

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

code is reused

(60)

code grows code is reused

code moves with you code is left behind

(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

(62)
(63)

Useless Use of * Slide 63

Oh, really?

(64)
(65)

Useless Use of * Slide 65

Useless Use of *

(66)
(67)

Useless Use of * Slide 67

Useless Use of Grep

(68)
(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/’

(70)
(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 }’

(72)

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

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

(73)

Useless Use of * Slide 73

Useful Use of Grep (?)

grep pattern1 file ... | grep -v pattern2

(74)
(75)

Useless Use of * Slide 75

Useless Use of Grep

grep pattern1 file ... | grep -v pattern2

awk ’/pattern1/ && !/pattern2/ { print }’ file ...

(76)

awk ’/pattern1/ && !/pattern2/ { print }’ file ...

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

(77)

Useless Use of * Slide 77

Useful Use of Sed (?)

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

(78)

ls ${p} done

(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

(80)

IFS=":"; for p in ${PATH}; do ls ${p}

done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’

(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}

(82)

IFS=":"; for p in ${PATH}; do ls ${p}

done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’

(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}

(84)

IFS=":"; for p in ${PATH}; do ls ${p}

done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’

(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}

(86)
(87)

Useless Use of * Slide 87

Useful Use of Sed (?)

VAR1="foo-bar-baz"

(88)

VAR1="foo-bar-baz"

(89)

Useless Use of * Slide 89

Useless Use of Sed

VAR1="foo-bar-baz"

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

(90)

VAR1="foo-bar-baz"

for i in $(echo ${VAR1} | sed -e ’s/-/ /g’); do the_needful $i

(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

(92)

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/.*-//’)

(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}"

(94)

Look, Ma: Reading a CSV with Shell Only!

IFS=","

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

(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

(96)

$ 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

(97)

Useless Use of * Slide 97

Useful Use of Ls (?)

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

(98)

for file in *pattern*; do the_needful

(99)

Useless Use of * Slide 99

Useful Use of Wc (?)

(100)
(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)’

(102)
(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)’

(104)

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

(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)

(106)

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

VAR=$(wc -l <file)

echo "${VAR}" echo ${VAR}

(107)

Useless Use of * Slide 107

Useful Use of Head (?)

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

(108)

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

(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/’

(110)
(111)

Useless Use of * Slide 111

Useless Use of Tail

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

(112)
(113)

Useless Use of * Slide 113

Useful Use of Tail

(114)
(115)

Useless Use of * Slide 115

Useless Use of Expr

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

(116)

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

(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 $

(118)
(119)

Useless Use of * Slide 119

Most Egregiously Useless Use of Perl

perl -e "print \"y\\nn\\n\";" | cmd ( echo y; echo n; ) | cmd

(120)

( echo y; echo n; ) | cmd

{ echo y; echo n; } | cmd printf "y\nn\n" | cmd

(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

(122)

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

(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

(124)
(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 $

(126)

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 $

(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 $

(128)

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 $

(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

(130)

$ 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 $

(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

(132)

set -eu (perl-mongers: think -w and use strict;) use readonly variables

use local variables comment your code

(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 ))

(134)

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% *}

(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

(136)

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

(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

(138)

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

(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

(140)

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.

(141)

Useless Use of * Slide 141

The KISS Principle

(142)

References

Related documents

(Check all that apply.) Please also rank your top THREE concerns by writing the corresponding letter in the appropriate space below. 17.8 f) The applications and media I want to use

In the sentences above, Jamie’s utterances express that she is noticing Landon's appearance. In a positive politeness strategy, it is included in the first strategy which notices

First, write test cases for the methods of each class, and then fill in the methods with code that will make your test cases pass.. The template test methods are a guideline you can

The effect of ration size on muscle fatty acid (FA) composition and mRNA expression levels of key regulatory enzymes of lipid and lipo- protein metabolism have been addressed

Although published studies include a review of the relevant existing literature, in many of the reviewed documents there was no indication that findings or evidence from

The gallery experience of moving in and through space is one of the factors that alters the conditions for the taking-form of the event: Campbell’s piece proposes movement

2 cups fresh spinach 1/2 cup strawberries 1/2 cup blueberries 1 teaspoon ground cinnamon 2 tablespoons golden flaxseed meal 1 tablespoon English walnuts 1 cup coconut milk Water

1) The RJ plug on the system data cable must be passed through the cable holes at the bottom part of the cabinet and connected to the LT/OP socket on the MB38s motherboard. 2)