Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Use Perl as Better Shell Script
Search
karupanerura
May 31, 2025
Programming
0
580
Use Perl as Better Shell Script
Houtou.pm #1
karupanerura
May 31, 2025
Tweet
Share
More Decks by karupanerura
See All by karupanerura
GoでParserを書く
karupanerura
4
900
マイ隙間家具OSSたちのご紹介
karupanerura
2
260
Mustache Template 話 2024
karupanerura
0
20
1つの言語を原点に色々な言語を学ぶということ
karupanerura
4
2.8k
Javaの実装をPerlでテストする
karupanerura
0
1.1k
Go Generics Overview
karupanerura
0
1.3k
Japan.pm 2021 開催まで至る道
karupanerura
0
2.1k
Perl MongersのためのAWS_CDK入門
karupanerura
0
140
Webシステムのパフォーマンス・チューニング
karupanerura
2
830
Other Decks in Programming
See All in Programming
Building an Application with TDD, DDD and Hexagonal Architecture - Isn't it a bit too much?
mufrid
0
370
Efficiency and Rock 'n’ Roll (Really!)
hollycummins
0
580
抽象データ型について学んだ
ryounasso
0
200
型安全なDrag and Dropの設計を考える
yudppp
5
640
TypeScriptのmoduleオプションを改めて整理する
bicstone
4
400
テスト分析入門/Test Analysis Tutorial
goyoki
10
2.6k
primeNumberでのRBS導入の現在 && RBS::Traceでinline RBSを拡充してみた
mnmandahalf
0
230
クラシルリワードにおける iOSアプリ開発の取り組み
funzin
1
770
Perlで痩せる
yuukis
1
580
Practical Domain-Driven Design - Workshop at NDC 2025
mufrid
0
120
REST API設計の実践 – ベストプラクティスとその落とし穴
kentaroutakeda
2
290
バランスを見極めよう!実装の意味を明示するための型定義 TSKaigi 2025 Day2 (5/24)
whatasoda
2
750
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
228
22k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
Site-Speed That Sticks
csswizardry
6
580
We Have a Design System, Now What?
morganepeng
52
7.6k
How to Ace a Technical Interview
jacobian
276
23k
GraphQLとの向き合い方2022年版
quramy
46
14k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Unsuck your backbone
ammeep
671
58k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
105
19k
Transcript
Use Perl as Better Shell Script @karupanerura Houtou.pm #1
@karupanerura • Perl / Go / Java / TypeScript /etc..
• PAUSE: KARUPA • Software Engineer @ DeNA, Co,. LTD. • Chief Director @ Japan Perl Association
ࢁསͱࢲ • όϯυ • Live @ KAZOO HALL • ٢ా͏ͲΜ
• ͖ • ࢁསେֶϫΠϯ • ͏·͍ https://www.yamanashi.ac.jp/social/3131
None
Shell Script
Shell Script • ShellߏจΛͦͷ··ར༻ͨ͠εΫϦϓτ • ίϚϯυΛॻ͍ͯͦͷ··࣮ߦͰ͖ΔͷͰײత • CLIϕʔεͷૢ࡞ΛࣗಈԽ͢Δͷʹศར • WebܥͷਓͳΒ1ॻ͍ͨ͜ͱ͋Δͣʁ
None
؆୯ʂศརʂ
؆୯……ʁ
Shell Scriptͷ͠͞ • ίϚϯυͷڥࠩҟ • macOSͱLinuxͰsedawkͳͲͰBSD/GNUҧ͍͕͋ΔʢΦϓγϣϯ͕ҟͳΔʣ • ref. https://speakerdeck.com/mackee/perl-for-shell-awk-and-sed-programmers •
࣮ࡍͷίϚϯυͱϏϧτΠϯίϚϯυͱγΣϧߏจ͕ࠞࡏ͢Δ • bashͷtestͱ[ͱ[[ͷҧ͍Λઆ໌Ͱ͖ΔͬͯਓͲΕ͘Β͍͍·͔͢ʁ • ෳࡶͳσʔλߏΛѻ͏ͷ͕ۤख
খωλ: testͱ[ͱ[[ͷҧ͍ bash-5.2$ type [ [ γΣϧΈࠐΈؔͰ͢ bash-5.2$ type test
test γΣϧΈࠐΈؔͰ͢ bash-5.2$ type [[ [[ γΣϧͷ༧ޠͰ͢
খωλ: testͱ[ͱ[[ͷҧ͍ bash-5.2$ type [ [ γΣϧΈࠐΈؔͰ͢ bash-5.2$ type test
test γΣϧΈࠐΈؔͰ͢ bash-5.2$ type [[ [[ γΣϧͷ༧ޠͰ͢ ಉ͡ΈࠐΈؔ(alias) ߏจʢਖ਼نදݱͳͲαϙʔτʣ
ෳࡶͳσʔλߏͬͯͨͱ͑ • HashMap(Dict)ͷதʹ͞ΒʹHashMap(Dict)Λ࣋ͭͱ͔ • {"user1":{"user2":true}} • ͦͷͳ͔ʹ͞ΒʹArrayΛ࣋ͭͱ͔ • {"user1":{"user2":[100,200]}}
ෳࡶͳσʔλߏ in Shell Script $ declare -A data=(["user1"]=(["user2"]=true)) bash: ༧ظ͠ͳ͍τʔΫϯ
`(' पลʹߏจΤϥʔ͕͋Γ·͢
ෳࡶͳσʔλߏ in Shell Script $ declare -A data=(["user1"]=(["user2"]=true)) bash: ༧ظ͠ͳ͍τʔΫϯ
`(' पลʹߏจΤϥʔ͕͋Γ·͢ ʢૉʹʣ Ͱ͖ͳ͍
ෳࡶͳ͜ͱΛ͢Δͷ͍͠
͍͜͠ͱΛ͍ͨ͠ͱ͖……?
ಠஅͱภݟʹΑΓϓϩάϥϛϯάݴޠΛൺֱ • Ruby/Go/JavaScript/PHP: ڥʹΑͬͯೖ͍ͬͯͳ͍ • Python • ͍·Ͱ͍͍ͩͨͷڥʹPython3ܥ͕ೖ͍ͬͯΔ • Shellͱߏจ͕͍ͩͿҧ͏ͷͰॻ͖͕͑ͪΐͬͱखؒ
• Perl • ͍͍ͩͨͷڥʹೖ͍ͬͯΔ͠Shellͱߏจ͍ۙ
PerlͱbashͷߏจΛൺֱ #!/usr/bin/perl use strict; use warnings; # コマンドライン引数の取得 my $arg1
= shift @ARGV; # 配列の宣言 my @array; # コマンドの実行 system 'grep status:500 /var/log/ access.log | ...'; #!/bin/bash set -ue -o pipefail # コマンドライン引数の取得 arg1=$1; shift # 配列の宣言 declare -a array # コマンドの実行 grep status:500 /var/log/access.log | ...
PerlͱbashͷߏจΛൺֱ # ίϚϯυΛ࣮ߦͯ݁͠ՌΛมʹೖΕ͍ͨ my $result = `grep status:500 file`; #
݅ذ if ($x > 0) { ... } # ϧʔϓ while ($x > 0) { ... } # ίϚϯυΛ࣮ߦͯ݁͠ՌΛมʹೖΕ͍ͨ result=`grep status:500 file` # ݅ذ if [[ "$x" -gt 0 ]]; then ... fi # ϧʔϓ while [[ "$x" -gt 0 ]]; do ... done
Perl͡ΌΜ (?)
࣮ྫհͷίʔφʔ
ྫ: json-diff https://github.com/karupanerura/dot fi les/blob/master/bin/json-diff
ྫ: jwt https://github.com/karupanerura/dot fi les/blob/master/bin/jwt
༨ஊ: zxͱ͍͏ͭ͋Δ https://github.com/google/zx ׳ΕͯखʹೃછΊΑ͍͔
·ͱΊ
·ͱΊ • Shell Scriptศར͚ͩͲෳࡶͳ͜ͱΛΖ͏ͱ͢Δͱ͍͠ • ෳࡶͳ͜ͱΛΓ͍ͨͱ͖ϓϩάϥϛϯάݴޠʹஔ͖͑ΔͱΑ͍ • PerlShell Scriptͱߏจ͕͍ۙͷͰҠ২ϋʔυϧ͕͍ʢͱࢥ͏ʣ •
ਖ਼ղ͕͋ΔΘ͚Ͱͳ͍ͷͰɺΓ͍͢ํ๏Λࡧ͠·͠ΐ͏