Upgrade to Pro — share decks privately, control downloads, hide ads and more …

思わぬ動きをするコマンドの事例

 思わぬ動きをするコマンドの事例

致命的バグヤロ!?という枯れているはずのコマンド事例

7b687d32752421ed7bdc2428e5e4b8e6?s=128

papiron

June 18, 2016
Tweet

Transcript

  1. ࢥΘ͵ಈ͖Λ͢Δ ίϚϯυͷࣄྫ USP༑ͷձ @papiron

  2. ރΕ͍ͯΔίϚϯυʁ • UNIXͰੲ͔Β࢖ΘΕΔίϚϯυ • ࢖͍ݹ͞Ε͍ͯͯރΕ͍ͯΔ • க໋తόά͕গͳ͍

  3. ރΕ͍ͯΔίϚϯυʁ ιϨμϝʂʂ ʙ͋ͳͨͷৗࣝ͸ඇৗࣝʂʁʙ ʻςϨϏ౦ژʼ

  4. ιϨμϝʂͳࣄྫ • dateίϚϯυ • ࢦఆͨ͠೔෇ͷදࣔ • awkίϚϯυ • ཚ਺ൃੜ ͜ͷൃදͰ͸ԼهίϚϯυͷ

    ιϨμϝʂͳࣄྫΛ঺հ͢Δ
  5. dateίϚϯυ • BSD൛ͱGNU൛Ͱಈ࡞͕ҟͳΔ • Mac΍FreeBSD͸BSDܥͷOS • Linux͸GNU൛͕ඪ४෇ଐ

  6. BSD൛dateίϚϯυ date -j -f ࢦఆformat ೔෇จࣈྻ ग़ྗformat ۩ମతͳར༻ྫ BSD൛dateίϚϯυͰࢦఆͨ͠೔෇ͷදࣔ $

    date -j -f '%Y%m%d' 20160618 ‘+%Y೥%m݄%d೔' 2016೥06݄18೔
  7. ෆਖ਼ͳ೔෇ͷݕূ ෆਖ਼ͳ೔෇Λࢦఆ͢ΔͱΤϥʔ͕ൃੜ $ date -j -f '%Y%m%d' 20160150 '+%Y೥%m݄%d೔' Failed

    conversion of ``20160150'' using format ``%Y%m%d'' $ date -j -f '%Y%m%d' 20160150 '+%Y೥%m݄%d೔' Failed conversion of ``20160150'' using format ``%Y%m%d'' 2016೥1݄50೔ 2016೥13݄1೔
  8. ෆਖ਼ͳ೔෇ͷݕূ Τϥʔ͕ൃੜͯ͠ͳ͍ʂʁʂʁ දࣔ͞Εͨ೔෇͕͓͔͍͠ʁʁ $ date -j -f '%Y%m%d' 20160200 '+%Y೥%m݄%d೔'

    2016೥01݄31೔ $ date -j -f '%Y%m%d' 20150229 '+%Y೥%m݄%d೔' 2015೥03݄01೔ 2016೥2݄0೔ 2015೥2݄29೔
  9. ෆਖ਼ͳ೔෇ͷݕূ GNU൛ͷdateίϚϯυͰ͸Τϥʔൃੜ $ gdate -d '20160200' '+%Y೥%m݄%d೔' gdate: invalid date

    ‘20160200’ $ gdate -d '20150229' '+%Y೥%m݄%d೔' gdate: invalid date ‘20150229’ 2016೥2݄0೔ 2015೥2݄29೔
  10. ݁࿦ʂʁ • dateίϚϯυͰ೔෇ͷݕূ • GNU൛date • ෆਖ਼ͳ೔෇͸Τϥʔൃੜ • BSD൛date •

    Ұ෦ͷෆਖ਼ͳ೔෇ͰΤϥʔ͕ൃੜ͠ͳ͍ • ͜Ε͸க໋తόάͰ͸ͳ͍ͷ͔ʁʁ
  11. ิ଍ BSD൛dateίϚϯυͰͷ೔෇ݕূ͸ ೔ͷ෦෼ͷ਺஋͕31ҎԼ͔Ͳ͏͔͚ͩ ֬ೝ͍ͯ͠ΔΒ͍͠ ʢUSP༑ͷձװࣄͷࠓઘޫ೭͞Μ͕ ιʔείʔυͰ֬ೝʣ

  12. awkίϚϯυ • ෳ਺ͷ࣮૷͕͋Δ • Macඪ४awk • ݹ͍࣮૷ʁॲཧ଎౓͸஗͍ • GNU൛awk •

    GNUಠ֦ࣗுͰଟػೳ • mawk • ߴ଎ॲཧ͕Մೳ
  13. awkͰཚ਺ൃੜ • awk͸rand()ؔ਺Ͱཚ਺ൃੜ • BEGIN۟Ͱsrand()ؔ਺Λ࢖ͬͯseed Λࢦఆ • srand()ؔ਺͸σϑΥϧτͰ೔෇Λ seedʹ࢖͏

  14. ͲΜͳཚ਺͕ൃੜʁ ϚχϡΞϧʹΑΔͱ0͔Β1ͷؒͷ਺஋ • Macඪ४awk • rand random number on (0,1)

    • GNU൛awk • rand() Return a random number N, between 0 and 1, such that 0 <= N < 1. • mawk • rand() Returns a random number between zero and one. ֤छAWKͷਖ਼ࣜϚχϡΞϧΑΓ
  15. awkͰཚ਺ൃੜͷ۩ମྫ $ yes | awk 'BEGIN{srand()}{print rand()}' 0.468882 0.800048 0.259498

    0.369426 0.88324 ... yesίϚϯυͱawkͷ࿈ܞͰແݶʹཚ਺Λൃੜ
  16. awkͰཚ਺ൃੜͷ۩ମྫ $ yes | awk 'BEGIN{srand()}{print rand()}’ ɹ| head -n

    100 ཚ਺Λ100ݸൃੜͤ͞Δ৔߹͸ԼهͷΑ͏ʹɻ $ seq 100 | awk 'BEGIN{srand()}{print rand()}’ headίϚϯυͰ࠷ޙʹ100ߦ෼੾Δ seqίϚϯυͰ100ߦੜ੒
  17. ࠷େ࠷খ஋ͷ֬ೝํ๏ $ yes | awk 'BEGIN{srand()}{print rand()}' | head -n

    100 | sort -n 0.0059382 0.0111584 ... 0.995792 0.999588 sortίϚϯυͷ-nΦϓγϣϯͰ਺஋ॱʹฒ΂ସ͑Δ
  18. ࠷େ࠷খ஋ͷ֬ೝํ๏ ͞ΒʹsedίϚϯυͰ1ߦ໨ͱ࠷ऴߦ໨Λग़ྗɻ $ yes | awk 'BEGIN{srand()}{print rand()}' | head

    -n 100 | sort -n | sed -n ‘1p;$p;’ | xargs 0.0059382 0.999588
  19. ิ଍ $ yes | awk 'BEGIN{srand()}{print rand()}' | head -n

    100 | st --min --max min max 0.0248714 0.999223 stίϚϯυͱ͍͏ศརͳίϚϯυ΋͋Δ ฏۉ஋ɺதԝ஋ɺඪ४ภࠩͳͲ΋ܭࢉՄೳ stίϚϯυͷGithubαΠτ͸ͪ͜Β https://github.com/nferraz/st
  20. ิ଍ $ yes | gawk 'BEGIN{srand()}{print rand()}' | head -n

    100 | awk ‘BEGIN{min=10000;max=-10000} {if($1<min){min=$1};if($1>max){max=$1}} END{print min,max}’ 0.00555304 0.996305 awkͰ΋֬ೝ͸Մೳ
  21. ཚ਺ੜ੒ςετ $ yes | awk 'BEGIN{srand()}{print rand()}' | head -n

    100 | sort -n | sed -n ‘1p;$p’ | xargs 0.0114521 0.991685 Macඪ४awk 0͔Β1ͷൣғ
  22. ཚ਺ੜ੒ςετ $ yes | gawk 'BEGIN{srand()}{print rand()}' | head -n

    100 | sort -n | sed -n ‘1p;$p’ | xargs 0.0114521 0.991685 GNU൛awk 0͔Β1ͷൣғ
  23. ཚ਺ੜ੒ςετ $ yes | mawk 'BEGIN{srand()}{print rand()}' | head -n

    100 | sort -n | sed -n ‘1p;$p' | xargs 0.0307588 1.99062 mawk 0͔Β1ͷൣғ͡Όͳ͍ʁʁ 0͔Β2ͷൣғͬΆ͍ʁʁ
  24. ཚ਺ੜ੒ςετ $ yes | mawk 'BEGIN{srand()}{print rand()}' | head -n

    100 | sort -n | sed -n ‘1p;$p’ | xargs 0.00340401 0.981487 mawk ઌͷ࣮ݧྫ͸MacͰ࣮ߦ ࠓ౓͸LinuxͰ࣮ߦͯ͠ΈΔͱʁ 0͔Β1ͷൣғ
  25. ݁࿦ʂʁ • awkͰ࢖͑Δཚ਺ൃੜrand()ؔ਺ • ਖ਼ࣜϚχϡΞϧͰ͸0͔Β1ͷؒ • BSDܥOSͰಈ͘mawkͰ͸0͔Β2ͷؒʁʁ • ͜Ε͸க໋తόάͰ͸ͳ͍ͷ͔ʁʁ

  26. ݪҼ͸ʁ Twitter্Ͱ γΣϧܳք۾ͷํͱҰॹʹ mawkͷιʔείʔυಡΜ͕ͩ ͦͷ࣌͸ܸ௜ @grethlen͞Μ @gin_135͞Μ ͋Γ͕ͱ͏͍͟͝·ͨ͠

  27. ݪҼ͸ʁ ͦͷޙ ιʔεΛݟͯ ݪҼΛಛఆͯ͠ ͘Εͨํ͕ʂ

  28. ݪҼ͸ʁ $ yes | ./mawk 'BEGIN{srand()}{print rand()}' | head -n

    100 | sort -n | sed -n '1p;$p' | xargs 0.00650974 0.987933 mawk 0͔Β1ʹͳͬͨʂʂ patch͋ͯͨmawkͰ࣮ݧʂ