Useless use of *

892 views
837 views

Published on

A presentation on shell/command optimization originally given at SCALE 2007.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
892
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Useless use of *

  1. 1. Useless Use of * Slide 1 Useless Use of * Jan Schaumann jschauma@netmeister.org PGP: 136D 027F DC29 8402 7B42 47D6 7C5B 64AF AF22 6A4CJan Schaumann SCALE5x
  2. 2. Useless Use of * Slide 2whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$Jan Schaumann SCALE5x
  3. 3. Useless Use of * Slide 3whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$ groups ${ME}netbsd sa yahoo$Jan Schaumann SCALE5x
  4. 4. Useless Use of * Slide 4whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$ groups ${ME}netbsd sa yahoo$Jan Schaumann SCALE5x
  5. 5. Useless Use of * Slide 5whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$ groups ${ME}netbsd sa yahoo$Jan Schaumann SCALE5x
  6. 6. Useless Use of * Slide 6whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$ groups ${ME}netbsd sa yahoo$Jan Schaumann SCALE5x
  7. 7. Useless Use of * Slide 7whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$ groups ${ME}netbsd sa yahoo$Jan Schaumann SCALE5x
  8. 8. Useless Use of * Slide 8whoami$ ME=$(id -un)$ grep ${ME} /etc/passwd | cut -d: -f5Jan Schaumann$ groups ${ME}netbsd sa yahoo$ http://pipes.yahoo.comJan Schaumann SCALE5x
  9. 9. Useless Use of * Slide 9Useless Use of... what?Back in the day...Jan Schaumann SCALE5x
  10. 10. Useless Use of * Slide 10Useless Use of... what?Back in the day...Jan Schaumann SCALE5x
  11. 11. Useless Use of * Slide 11Useless Use of... what?Back in the day... The OperatorJan Schaumann SCALE5x
  12. 12. Useless Use of * Slide 12Useless Use of... what?Back in the day... BOFHJan Schaumann SCALE5x
  13. 13. Useless Use of * Slide 13Useless Use of... what?Back in the day...Jan Schaumann SCALE5x
  14. 14. Useless Use of * Slide 14Useless Use of... what?Back in the day...Jan Schaumann SCALE5x
  15. 15. Useless Use of * Slide 15Useless Use of... what?Back in the day...Jan Schaumann SCALE5x
  16. 16. Useless Use of * Slide 16Useless Use of... what?Back in the day...Jan Schaumann SCALE5x
  17. 17. Useless Use of * Slide 17Useless Use of... what? comp.unix.shellJan Schaumann SCALE5x
  18. 18. Useless Use of * Slide 18Useless Use of... what?I have a bunch of text files that contain stringscontaining /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. 19. Useless Use of * Slide 19Useless 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. 20. Useless Use of * Slide 20 Useless Use of CatJan Schaumann SCALE5x
  21. 21. Useless Use of * Slide 21Useful Use of Cat (?)The obvious:cat file | grep patterncat file | awk ’{ print $2; }’Jan Schaumann SCALE5x
  22. 22. Useless Use of * Slide 22Useless Use of CatThe obvious:cat file | grep patterncat file | awk ’{ print $2; }’Jan Schaumann SCALE5x
  23. 23. Useless Use of * Slide 23Useless Use of CatThe obvious:cat file | grep patterngrep pattern filecat file | awk ’{ print $2; }’awk ’{ print $2; }’ < fileJan Schaumann SCALE5x
  24. 24. Useless Use of * Slide 24Useful Use of Cat (?)cat file1 file2 file3 | wc -lcat file1 file2 file3 | wc -wJan Schaumann SCALE5x
  25. 25. Useless Use of * Slide 25Useless Use of Catcat file1 file2 file3 | wc -lcat file1 file2 file3 | wc -wJan Schaumann SCALE5x
  26. 26. Useless Use of * Slide 26Useless Use of Catcat file1 file2 file3 | wc -lawk ’END { print NR }’ file1 file2 file3cat file1 file2 file3 | wc -wawk ’{w = w + NF} END { print w }’ file1 file2 file3Jan Schaumann SCALE5x
  27. 27. Useless Use of * Slide 27Useful Use of Cat (?)cat file1 file2 file3 | grep patternif [ $(cat files | grep -c pattern) -gt 0 ]; thenJan Schaumann SCALE5x
  28. 28. Useless Use of * Slide 28Useless Use of Catcat file1 file2 file3 | grep patternif [ $(cat files | grep -c pattern) -gt 0 ]; thenJan Schaumann SCALE5x
  29. 29. Useless Use of * Slide 29Useless Use of Catcat file1 file2 file3 | grep patterngrep -h pattern file1 file2 file3if [ $(cat files | grep -c pattern) -gt 0 ]; thenJan Schaumann SCALE5x
  30. 30. Useless Use of * Slide 30Useless Use of Catcat file1 file2 file3 | grep patterngrep -h pattern file1 file2 file3awk ’/pattern/ { print }’ file1 file2 file3if [ $(cat files | grep -c pattern) -gt 0 ]; thenJan Schaumann SCALE5x
  31. 31. Useless Use of * Slide 31Useless Use of Catcat file1 file2 file3 | grep patterngrep -h pattern file1 file2 file3awk ’/pattern/ { print }’ file1 file2 file3if [ $(cat files | grep -c pattern) -gt 0 ]; thenif [ -n ”$(grep -l pattern files)” ]; thenJan Schaumann SCALE5x
  32. 32. Useless Use of * Slide 32Useless Use of Catcat file1 file2 file3 | grep patterngrep -h pattern file1 file2 file3awk ’/pattern/ { print }’ file1 file2 file3if [ $(cat files | grep -c pattern) -gt 0 ]; thenif [ -n "$(grep -l pattern files)" ]; thenif grep pattern files >/dev/null 2>&1; thenJan Schaumann SCALE5x
  33. 33. Useless Use of * Slide 33Useful Use of Cat concatenate and print filesJan Schaumann SCALE5x
  34. 34. Useless Use of * Slide 34Useful Use of Cat concatenate and print files (D’oh!) cat * > fileJan Schaumann SCALE5x
  35. 35. Useless Use of * Slide 35Useful Use of Cat concatenate and print files (D’oh!) cat * > file feed file as input to another command in a particular orderJan Schaumann SCALE5x
  36. 36. Useless Use of * Slide 36Useful 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; } | commandJan Schaumann SCALE5x
  37. 37. Useless Use of * Slide 37Useful 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 NOOPJan Schaumann SCALE5x
  38. 38. Useless Use of * Slide 38Useful 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 fiJan Schaumann SCALE5x
  39. 39. Useless Use of * Slide 39Useful 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} | cmd3Jan Schaumann SCALE5x
  40. 40. Useless Use of * Slide 40Useful Use of Cat CAT5 CAT6Jan Schaumann SCALE5x
  41. 41. Useless Use of * Slide 41Why bother?Jan Schaumann SCALE5x
  42. 42. Useless Use of * Slide 42Why bother? $2.95Jan Schaumann SCALE5x
  43. 43. Useless Use of * Slide 43Why bother? $5.90Jan Schaumann SCALE5x
  44. 44. Useless Use of * Slide 44Why bother? $11.80Jan Schaumann SCALE5x
  45. 45. Useless Use of * Slide 45Why bother? $23.60Jan Schaumann SCALE5x
  46. 46. Useless Use of * Slide 46Why bother?Jan Schaumann SCALE5x
  47. 47. Useless Use of * Slide 47Why bother? $47.20Jan Schaumann SCALE5x
  48. 48. Useless Use of * Slide 48Why bother? $94.4Jan Schaumann SCALE5x
  49. 49. Useless Use of * Slide 49Why bother?Jan Schaumann SCALE5x
  50. 50. Useless Use of * Slide 50Why bother?Jan Schaumann SCALE5x
  51. 51. Useless Use of * Slide 51But...Jan Schaumann SCALE5x
  52. 52. Useless Use of * Slide 52But... ... I run my scripts only once!Jan Schaumann SCALE5x
  53. 53. Useless Use of * Slide 53But... ... I have super fast hardware!Jan Schaumann SCALE5x
  54. 54. Useless Use of * Slide 54But... ... I have super fast hardware!$ uptime10:36AM up 254 days, 4 users, load averages: 80.12, 75.51, 72.40$Jan Schaumann SCALE5x
  55. 55. Useless Use of * Slide 55But... ... nobody else but me uses my code!Jan Schaumann SCALE5x
  56. 56. Useless Use of * Slide 56Oh, really?Unfortunately, you really have no control over your code:Jan Schaumann SCALE5x
  57. 57. Useless Use of * Slide 57Oh, really?Unfortunately, you really have no control over your code: code growsJan Schaumann SCALE5x
  58. 58. Useless Use of * Slide 58Oh, really?Unfortunately, you really have no control over your code: code grows code is reusedJan Schaumann SCALE5x
  59. 59. Useless Use of * Slide 59Oh, really?Unfortunately, you really have no control over your code: code grows code is reused code moves with youJan Schaumann SCALE5x
  60. 60. Useless Use of * Slide 60Oh, really?Unfortunately, you really have no control over your code: code grows code is reused code moves with you code is left behindJan Schaumann SCALE5x
  61. 61. Useless Use of * Slide 61Oh, 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. 62. Useless Use of * Slide 62Oh, really?Jan Schaumann SCALE5x
  63. 63. Useless Use of * Slide 63Oh, really?Jan Schaumann SCALE5x
  64. 64. Useless Use of * Slide 64Useless Use of * Useless Use of CatJan Schaumann SCALE5x
  65. 65. Useless Use of * Slide 65Useless Use of * Useless Use of *Jan Schaumann SCALE5x
  66. 66. Useless Use of * Slide 66 Useless Use of GrepJan Schaumann SCALE5x
  67. 67. Useless Use of * Slide 67Useless Use of GrepOf course, all use of grep(1) is useless!Jan Schaumann SCALE5x
  68. 68. Useless Use of * Slide 68Useless Use of GrepOf course, all use of grep(1) is useless!echo g/RE/p | ed -s fileJan Schaumann SCALE5x
  69. 69. Useless Use of * Slide 69Useful Use of Grep (?)host hostname | grep ’has address’ | awk ’{ print $NF }’echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’Jan Schaumann SCALE5x
  70. 70. Useless Use of * Slide 70Useless Use of Grephost hostname | grep ’has address’ | awk ’{ print $NF }’echo ${string} | grep ’pattern’ | sed -e ’s/foo/bar/’Jan Schaumann SCALE5x
  71. 71. Useless Use of * Slide 71Useless Use of Grephost 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. 72. Useless Use of * Slide 72Useless Use of Grephost 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. 73. Useless Use of * Slide 73Useful Use of Grep (?)grep pattern1 file ... | grep -v pattern2grep pattern1 file | grep -v ^# | grep -v pattern2Jan Schaumann SCALE5x
  74. 74. Useless Use of * Slide 74Useless Use of Grepgrep pattern1 file ... | grep -v pattern2grep pattern1 file | grep -v ^# | grep -v pattern2Jan Schaumann SCALE5x
  75. 75. Useless Use of * Slide 75Useless Use of Grepgrep pattern1 file ... | grep -v pattern2awk ’/pattern1/ && !/pattern2/ { print }’ file ...grep pattern1 file | grep -v ^# | grep -v pattern2Jan Schaumann SCALE5x
  76. 76. Useless Use of * Slide 76Useless Use of Grepgrep pattern1 file ... | grep -v pattern2awk ’/pattern1/ && !/pattern2/ { print }’ file ...grep pattern1 file | grep -v ^# | grep -v pattern2awk ’/pattern1/ && !/(ˆ#)|(pattern2)/ { print }’ fileJan Schaumann SCALE5x
  77. 77. Useless Use of * Slide 77Useful Use of Sed (?)for p in $(echo ${PATH} | sed -e ’s/:/ /’); do ls ${p}doneJan Schaumann SCALE5x
  78. 78. Useless Use of * Slide 78Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); do ls ${p}doneJan Schaumann SCALE5x
  79. 79. Useless Use of * Slide 79Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}doneJan Schaumann SCALE5x
  80. 80. Useless Use of * Slide 80Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ SedJan Schaumann SCALE5x
  81. 81. Useless Use of * Slide 81Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Mknod ?Jan Schaumann SCALE5x
  82. 82. Useless Use of * Slide 82Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Groff ?Jan Schaumann SCALE5x
  83. 83. Useless Use of * Slide 83Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Shlock ?Jan Schaumann SCALE5x
  84. 84. Useless Use of * Slide 84Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Route ?Jan Schaumann SCALE5x
  85. 85. Useless Use of * Slide 85Useless Use of Sedfor p in $(echo ${PATH} | sed -e ’s/:/ /’); doIFS=":"; for p in ${PATH}; do ls ${p}done | awk ’BEGIN { srand() } { if (NR == int(rand()*100)) { print }}’ Dump ?Jan Schaumann SCALE5x
  86. 86. Useless Use of * Slide 86Useless Use of SedJan Schaumann SCALE5x
  87. 87. Useless Use of * Slide 87Useful Use of Sed (?)VAR1="foo-bar-baz"VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’)Jan Schaumann SCALE5x
  88. 88. Useless Use of * Slide 88Useless Use of SedVAR1="foo-bar-baz"VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’)Jan Schaumann SCALE5x
  89. 89. Useless Use of * Slide 89Useless Use of SedVAR1="foo-bar-baz"VAR2=$(echo ${VAR1} | sed -e ’s/-baz//’)VAR2=${VAR1%-baz}Jan Schaumann SCALE5x
  90. 90. Useless Use of * Slide 90Useful 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 $idoneJan Schaumann SCALE5x
  91. 91. Useless Use of * Slide 91Useless Use of SedLooping over values in a variable:VAR1="foo-bar-baz"for i in $(echo ${VAR1} | sed -e ’s/-/ /g’); doIFS=-for i in ${VAR1}; do the_needful $idoneJan Schaumann SCALE5x
  92. 92. Useless Use of * Slide 92Useful 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. 93. Useless Use of * Slide 93Useless Use of SedAssigning 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. 94. Useless Use of * Slide 94Useless Use of ...Dude, IFS + shell is Teh Roxor!!!1Look, Ma: Reading a CSV with Shell Only!IFS=","while read -r field1 waste field3 field4 waste; do echo "${field1}: ${field4} --> ${field3}"done <fileJan Schaumann SCALE5x
  95. 95. Useless Use of * Slide 95Useless Use of Shell Only$ cat >script.shIFS=","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. 96. Useless Use of * Slide 96Useless Use of Shell OnlyAwk to the rescue!$ cat >script2.shawk -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. 97. Useless Use of * Slide 97Useful Use of Ls (?)for file in $(ls *pattern*); do the_needfuldoneJan Schaumann SCALE5x
  98. 98. Useless Use of * Slide 98Useless Use of Lsfor file in $(ls *pattern*); dofor file in *pattern*; do the_needfuldoneJan Schaumann SCALE5x
  99. 99. Useless Use of * Slide 99Useful Use of Wc (?)VAR=$(cat file | wc -l | sed -e ’s/ *//g)’Jan Schaumann SCALE5x
  100. 100. Useless Use of * Slide 100Useful Use of Wc (?)VAR=$(cat file | wc -l | sed -e ’s/ *//g’)VAR=$(wc -l <file| sed -e ’s/ *//g)’Jan Schaumann SCALE5x
  101. 101. Useless Use of * Slide 101Useless Use of ... ?VAR=$(cat file | wc -l | sed -e ’s/ *//g’)VAR=$(wc -l <file| sed -e ’s/ *//g)’Jan Schaumann SCALE5x
  102. 102. Useless Use of * Slide 102Useless Use of ... ?VAR=$(cat file | wc -l | sed -e ’s/ *//g’)VAR=$(wc -l <file| sed -e ’s/ *//g)’Jan Schaumann SCALE5x
  103. 103. Useless Use of * Slide 103Useless 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. 104. Useless Use of * Slide 104Useless Use of SedVAR=$(cat file | wc -l | sed -e ’s/ *//g’)VAR=$(wc -l <file| sed -e ’s/ *//g)’VAR=$(wc -l <file)Jan Schaumann SCALE5x
  105. 105. Useless Use of * Slide 105Useless Use of SedVAR=$(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. 106. Useless Use of * Slide 106Useless Use of SedVAR=$(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. 107. Useless Use of * Slide 107Useful Use of Head (?)command1 | head -1 | sed -e ’s/pattern/string/’command1 | head -10 | sed -e ’s/pattern/string/’Jan Schaumann SCALE5x
  108. 108. Useless Use of * Slide 108Useless Use of Headcommand1 | 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. 109. Useless Use of * Slide 109Useless Use of Headcommand1 | 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. 110. Useless Use of * Slide 110Useful Use of Tail (?)command1 | tail -1 | sed -e ’s/pattern/string/’Jan Schaumann SCALE5x
  111. 111. Useless Use of * Slide 111Useless Use of Tailcommand1 | tail -1 | sed -e ’s/pattern/string/’command1 | awk ’END { print gensub("pattern","string",0); exit; }’Jan Schaumann SCALE5x
  112. 112. Useless Use of * Slide 112Useful Use of Tail (?)command1 | tail -10 | sed -e ’s/pattern/string/’Jan Schaumann SCALE5x
  113. 113. Useless Use of * Slide 113Useful Use of Tailcommand1 | tail -10 | sed -e ’s/pattern/string/’Jan Schaumann SCALE5x
  114. 114. Useless Use of * Slide 114Useful Use of Expr (?)echo $(expr $i + $i)Jan Schaumann SCALE5x
  115. 115. Useless Use of * Slide 115Useless Use of Exprecho $(expr $i + $i)echo $(( $i + $i ))Jan Schaumann SCALE5x
  116. 116. Useless Use of * Slide 116Useless Use of Exprecho $(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. 117. Useless Use of * Slide 117Useless Use of Exprecho $(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. 118. Useless Use of * Slide 118Most Egregiously Useless Use of Perlperl -e "print "ynnn";" | cmdJan Schaumann SCALE5x
  119. 119. Useless Use of * Slide 119Most Egregiously Useless Use of Perlperl -e "print "ynnn";" | cmd( echo y; echo n; ) | cmdJan Schaumann SCALE5x
  120. 120. Useless Use of * Slide 120Most Egregiously Useless Use of Perlperl -e "print "ynnn";" | cmd( echo y; echo n; ) | cmd{ echo y; echo n; } | cmdprintf "ynnn" | cmdJan Schaumann SCALE5x
  121. 121. Useless Use of * Slide 121Shell 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. 122. Useless Use of * Slide 122Shell 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/cut133487 /usr/bin/awk12590 /usr/bin/cut$Jan Schaumann SCALE5x
  123. 123. Useless Use of * Slide 123Testing 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 functionJan Schaumann SCALE5x
  124. 124. Useless Use of * Slide 124Shell Coding StyleWriting Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;)Jan Schaumann SCALE5x
  125. 125. Useless Use of * Slide 125Shell Coding StyleWriting Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;)$ cat >script.shset -uecho ${FOO}echo "done"^D$ sh script.shscript.sh: FOO: parameter not set$ echo $?2$Jan Schaumann SCALE5x
  126. 126. Useless Use of * Slide 126Shell Coding StyleWriting Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;)$ cat >script.shset -els /nowhereecho "done"^D$ sh script.shls: /nowhere: No such file or directory$Jan Schaumann SCALE5x
  127. 127. Useless Use of * Slide 127Shell Coding StyleWriting Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;)$ cat >script.shset -eif ! ls /nowhere; then echo "ls failed"fiecho "done"^D$ sh script.shls: /nowhere: No such file or directoryls faileddone$Jan Schaumann SCALE5x
  128. 128. Useless Use of * Slide 128Shell Coding StyleWriting Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables$ cat >script.shreadonly VAR="foo"VAR="bar"echo ${VAR}^D$ sh script.shscript.sh: VAR: is read only$Jan Schaumann SCALE5x
  129. 129. Useless Use of * Slide 129Shell Coding StyleWriting Shell Code is just like writing any other code: set -eu (perl-mongers: think -w and use strict;) use readonly variables use local variablesJan Schaumann SCALE5x
  130. 130. Useless Use of * Slide 130Shell Coding Style: local variables$ cat s.shfunc () { input="${1}" # do something with ${input}}$ cat script.sh. ./s.shinput="var1 var2 var3"for var in ${input}; do func "${var}"doneecho ${input}$ sh script.shvar3$Jan Schaumann SCALE5x
  131. 131. Useless Use of * Slide 131Shell Coding Style: local variables$ cat s.shfunc () { local input="${1}" # do something with ${input}}$ cat script.sh. ./s.shinput="var1 var2 var3"for var in ${input}; do func "${var}"doneecho ${input}$ sh script.shvar1 var2 var3$Jan Schaumann SCALE5x
  132. 132. Useless Use of * Slide 132Shell Coding StyleWriting 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 codeJan Schaumann SCALE5x
  133. 133. Useless Use of * Slide 133Shell Coding StyleWriting 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 codeBad:# this adds 1 to numnum=$(( ${num} + 1 ))Jan Schaumann SCALE5x
  134. 134. Useless Use of * Slide 134Shell Coding StyleWriting 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 codeBetter:input="pair1l:pair1r pair2l:pair2r"# extract first pair in input string, throw away restp1l=${input%%:*}rest=${input#*:}p1r=${rest% *}Jan Schaumann SCALE5x
  135. 135. Useless Use of * Slide 135Shell Coding StyleWriting 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 wayJan Schaumann SCALE5x
  136. 136. Useless Use of * Slide 136Shell Coding StyleWriting 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 codeJan Schaumann SCALE5x
  137. 137. Useless Use of * Slide 137Shell Coding StyleWriting 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 styleJan Schaumann SCALE5x
  138. 138. Useless Use of * Slide 138Shell Coding StyleWriting 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 interfaceJan Schaumann SCALE5x
  139. 139. Useless Use of * Slide 139Shell Coding StyleWriting 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 readabilityJan Schaumann SCALE5x
  140. 140. Useless Use of * Slide 140Shell Coding: Performance vs. Readabilityawk -F: -v ME="${ME}" ’{ if ($0 ~ ME) { print $5 }}’ filevs.grep ${ME} file | cut -d: -f5awk ’{w = w + NF} END { print w }’ file1 file2 file3vs.cat file1 file2 file2 | wc -wJan Schaumann SCALE5x
  141. 141. Useless Use of * Slide 141The KISS Principle Keep It Simple, Stupid!Jan Schaumann SCALE5x
  142. 142. Useless Use of * Slide 142Useless Use of time (?) Thanks!Jan Schaumann SCALE5x

×