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
710
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
920
マイ隙間家具OSSたちのご紹介
karupanerura
2
260
Mustache Template 話 2024
karupanerura
0
27
1つの言語を原点に色々な言語を学ぶということ
karupanerura
4
2.9k
Javaの実装をPerlでテストする
karupanerura
0
1.2k
Go Generics Overview
karupanerura
0
1.3k
Japan.pm 2021 開催まで至る道
karupanerura
0
2.1k
Perl MongersのためのAWS_CDK入門
karupanerura
0
140
Webシステムのパフォーマンス・チューニング
karupanerura
2
840
Other Decks in Programming
See All in Programming
Team operations that are not burdened by SRE
kazatohiei
1
310
10 Costly Database Performance Mistakes (And How To Fix Them)
andyatkinson
0
400
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
170
XP, Testing and ninja testing
m_seki
3
250
すべてのコンテキストを、 ユーザー価値に変える
applism118
3
1.3k
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
790
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
210
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
730
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
650
スタートアップの急成長を支えるプラットフォームエンジニアリングと組織戦略
sutochin26
1
6k
AI時代の『改訂新版 良いコード/悪いコードで学ぶ設計入門』 / ai-good-code-bad-code
minodriven
20
7.7k
PicoRuby on Rails
makicamel
2
130
Featured
See All Featured
Practical Orchestrator
shlominoach
189
11k
Gamification - CAS2011
davidbonilla
81
5.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Code Review Best Practice
trishagee
69
19k
Adopting Sorbet at Scale
ufuk
77
9.5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
4 Signs Your Business is Dying
shpigford
184
22k
Code Reviewing Like a Champion
maltzj
524
40k
Faster Mobile Websites
deanohume
307
31k
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ͱߏจ͕͍ۙͷͰҠ২ϋʔυϧ͕͍ʢͱࢥ͏ʣ •
ਖ਼ղ͕͋ΔΘ͚Ͱͳ͍ͷͰɺΓ͍͢ํ๏Λࡧ͠·͠ΐ͏