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
Perl で作る tiny shell
Search
Kei Kamikawa
June 23, 2018
Programming
3
3.7k
Perl で作る tiny shell
Perl で極限に機能を削った shell の作り方を紹介します
Kei Kamikawa
June 23, 2018
Tweet
Share
More Decks by Kei Kamikawa
See All by Kei Kamikawa
どこでも使える Cloudflare Workers!
codehex
6
670
htmx is fun!
codehex
2
320
すぐ使える Cloudflare Workers!
codehex
15
3.3k
Go to Cloudflare Workers
codehex
8
2k
AI Gateway 使っているよ!
codehex
2
1.5k
日時処理の新スタンダード: Synchro によるタイムゾーン安全、楽々開発
codehex
1
2k
Cloudflare Workers は楽しい!
codehex
9
3.2k
gRPC Client on Cloudflare Workers
codehex
2
5.6k
NOT A HOTEL AI コンシェルジュ「Kevin」とキャッシュ
codehex
1
1.6k
Other Decks in Programming
See All in Programming
useSyncExternalStoreを使いまくる
ssssota
6
1k
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
530
CSC305 Lecture 25
javiergs
PRO
0
130
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
From Translations to Multi Dimension Entities
alexanderschranz
2
130
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
130
103 Early Hints
sugi_0000
1
220
MCP with Cloudflare Workers
yusukebe
2
220
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
650
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Gamification - CAS2011
davidbonilla
80
5.1k
KATA
mclloyd
29
14k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Code Reviewing Like a Champion
maltzj
520
39k
Side Projects
sachag
452
42k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Making Projects Easy
brettharned
116
5.9k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Agile that works and the tools we love
rasmusluckow
328
21k
Transcript
1FSMͰ࡞Δ UJOZTIFMM )PLLBJEPQN
!DPEFIFY !$PEF)FY w .FSDBSJ *OD w %FWFMPQJOHNJDSPTFSWJDFT w 0LJOBXBQN w
*MJLF1FSM94(P w IUUQTDPEFIFYIBUFCMPKQ
γΣϧͬͯͬͯ·͔͢
None
/P
$ pwd
$ pwd /Users/codehex
#BTI ;TI pTI
γΣϧʹ ༷ʑͳػೳ͕͋Γ·͢
ྫ͑
ύΠϓ
$ pwd | cat -n
$ pwd | cat -n 1 /Users/codehex
Ξϯυ
$ pwd && echo hello
$ pwd && echo hello /Users/codehex hello
͜ΕΒͷ ࣮Λ͍ͬͯ·͔͢ʁ
ࠓ͢༰
ࠓ͢༰ w ࠷ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ
ࠓ͢༰ w ࠷ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ
࠷ݶͷγΣϧͷఆٛ UJOZTIFMMͷಈ࡞Λఆٛ͢Δ
lUJOZTIFMMzNFBOT ΊͬͪΌখ͍͞γΣϧ
Ͳ͏͍͏ಈ࡞Λ͢Δ͔
Ͳ͏͍͏ಈ࡞Λ͢Δ͔ w ୯ҰίϚϯυͷ࣮ߦ $ ls w ύΠϓͷ࣮ߦ $ ls
| cat w ΞϯυʹΑΔෳ࣮ߦ $ ls && cat w αϒγΣϧόοΫάϥϯυ࣮ߦࠓճؚΊ·ͤΜ
ࠓ͢༰ w ࠷ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ
ߏจղੳ ެจఆٛͱߏจղੳثͷ࡞
$ cat | cat -n && ls
$ cat | cat -n && ls
$ cat | cat -n && ls
$ cat | cat -n && ls
$ cat | cat -n && ls
$ cat | cat -n && ls ࠨ͔Βӈʹղऍ͠·͢
ߏจͷఆٛ
ӈʹ࿈͍݁ͯ͘͠Α͏ʹ ఆ͚ٛΛߦ͏
ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |
[0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
ߏจղੳثͷ࡞
ߏจղੳثʹඞཁͳཁૉ
ߏจղੳثʹඞཁͳཁૉ w จࣈྻͷεΩϟφʔ w ಡΈࠐΈҐஔಡΈऔͬͨจࣈྻΛอ࣋͢Δ w จࣈྻͷϚονϯά w εΩϟφʔͰಘͨจࣈྻ͕ߏจఆٛʹϚονͨ͠ ͔Ͳ͏͔ผ͢Δ
w Ϛονͨ͠จࣈྻʹҙຯΛͨͤΔ τʔΫϯԽ w ߏจͷੜ
1FSMͰʁ
ਖ਼نදݱ
IUUQTQFSMEPDQFSMPSHQFSMPQIUNMD(@BTTFSUJPO@ l"VTFGVMJEJPNGPSAlexAMJLF TDBOOFSTJTA/\G…/gcAʜz
/\G…/gc
/\G…/gc w A/cANPEJpFSͰݱࡏϚον͍ͯ͠ΔҐஔΛ อ࣋͢Δ͜ͱ͕Ͱ͖Δɻ • pos()ͰҐஔΛऔΓग़͢͜ͱ͕Մೳ • \GͰલճAm//gAͰϚονͨ͠ҐஔΛอ࣋͢Δ͜ͱ ͕Մೳ
εΩϟφʔͱϚονϯάͷ ׂΛͭ͜ͱ͕Ͱ͖Δ ?P? W
͜͜Ͱྑ͍ใ
ΊͬͪΌࢀߟʹͳΔϖʔδ w IUUQTHJUIVCDPNLBSVQBOFSVSBQ +40/CMPCNBTUFSMJC+40/1BSTFSQN w IUUQTHJUIVCDPNLBSVQBOFSVSB50.- 1BSTFSCMPCNBTUFSMJC50.-1BSTFSQN
࣮༷ࢠ
ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |
[0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
$.%
ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |
[0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
4)--
ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |
[0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
1*1&
ߏจఆٛ • CMD: [a-zA-Z_-][0-9a-zA-Z_-]* <ARG> • ARG: [0-9a-zA-Z_-]* <ARG> |
[0-9a-zA-Z_-]* • SHLL: <PIPE> '&&' <AND> | <PIPE> • PIPE: <CMD> '|' <PIPE> | <CMD> • AND: <SHLL> '&&' <AND> | <SHLL>
"/%
ΛQSJOU༷ͨ͠ࢠ $ cat | cat -n && ls
"/% 1*1& DBU DBU MT $ cat | cat -n
&& ls
ͪͳΈʹ
$ cat | cat -n && ls
$ cat | cat -n && ls ࠨ͔Βӈʹղऍ͠·͢
ͳͷͰ ΛḷΔͱ͖
"/% 1*1& DBU DBU MT $ cat | cat -n
&& ls ᶃ
"/% 1*1& DBU DBU MT $ cat | cat -n
&& ls ᶃ ᶄ ᶅ
"/% 1*1& DBU DBU MT $ cat | cat -n
&& ls ᶃ ᶄ ᶅ ᶆ ᶇ
͜ΕΛجʹ ࣮ߦ͢ΔػೳΛ࡞͠·͢
ࠓ͢༰ w ࠷ݶͷγΣϧͷఆٛ w ߏจղੳ w ίϚϯυͷ࣮ߦ
ίϚϯυͷ࣮ߦ ߏจΛجʹ࣮ߦ͢Δ
࣮ͷલʹ
ඪ४ετϦʔϜʹ͍ͭͯ
w 45%*/ 4UBOEBSE*OQVU ඪ४ೖྗ w 45%065 4UBOEBSE0VUQVU ඪ४ग़ྗ w 45%&33
4UBOEBSE&SSPS ඪ४Τϥʔग़ྗ ඪ४ετϦʔϜ
͜ΕΒ࣮࣭ ϑΝΠϧͱͯ͠ѻΘΕ·͢
୯ҰίϚϯυͷ ετϦʔϜͷϑϩʔ
$ cat
$ cat cat 45%*/
$ cat cat 45%*/ 45%065 45%&33
$ cat GPSLΛ͏ͱTIΛհ͞ͳ͍Ͱ࣮ߦՄೳ
ύΠϓͷϑϩʔ
$ fortune | cowsay
$ fortune | cowsay 45%*/ PS/PU ᶃ
$ fortune | cowsay 45%*/ PS/PU 45%065 ᶃ ᶄ
$ fortune | cowsay 45%*/ PS/PU 45%065 45%*/ ᶃ ᶄ
ᶅ
$ fortune | cowsay 45%*/ PS/PU 45%065 45%*/ 45%065 ᶃ
ᶄ ᶅ ᶆ
͜ΕΒͷίϚϯυ ผϓϩηεͰಈ͍͍ͯ·͢
$ ps a | cat 1*%
ύΠϓͷ࣮
pipe my ($r, $w);
w $r͕3FBEFS $w͕8SJUFS w 8SJUFSʹσʔλ͕ॻ͖ࠐ·Εͨͱ͖ 3FBEFSͰͦͷσʔλΛಡΈऔΔ͜ͱ͕Մೳ pipe my ($r, $w);
֤ϓϩηεؒͷετϦʔϜΛ pipe()Ͱܨ͛Δ͜ͱͰ࣮Մೳ
ͦͷ࣌ͷϑϩʔ
$ fortune | cowsay 45%*/ PS/PU 45%065 ᶃ ᶄ
$ fortune | cowsay 45%*/ PS/PU 45%065 ᶃ ᶄ ᶅ
ᶆ 8SJUFS 3FBEFS
$ fortune | cowsay 45%*/ PS/PU 45%065 45%*/ 45%065 ᶃ
ᶄ ᶅ ᶆ 8SJUFS 3FBEFS ᶇ ᶈ ᶉ
ͳͷͰ࣮࣭ ͜ͷΑ͏ʹߟ͑ΒΕΔ
$ fortune | cowsay 45%*/ PS/PU 8SJUFS 3FBEFS 45%065 ᶃ
ᶄ ᶅ ᶆ
None
w ίϚϯυOPEFͰ͔ͭOPEF͕ύΠϓͰͳ͍ ߹ଈίϚϯυΛ࣮ߦ w ͦΕҎ֎ίϚϯυΛอ࣋͢Δ w ύΠϓOPEF͔͕ͭύΠϓOPEFͰͳ͍߹ อ࣋͠ଓ͚ͨίϚϯυΛ࣮ߦ
Ξϯυͷϑϩʔ
$ echo hello && echo world
$ echo hello && echo world ᶃ
$ echo hello && echo world ᶃ ᶄ
w ΞϯυOPEFʹདྷΔ·ͰʹࠨͷOPEFΛ શ࣮ͯߦ͢Δ w ޭͨ͠߹ӈͷOPEFΛࠨͷOPEFͱಉ͡Α͏ ʹ࣮ߦ͢Δ
͜Ε͚ͩͰ UJOZTIFMM͕͠·͢ʂ
·ͱΊ
·ͱΊ w ӈʹ࿈͍݁ͯ͘͠Α͏ʹߏจఆٛͨ͠ w γΣϧͷύΠϓɺΞϯυػೳΛཧղͯ͠ɺղੳ࣌ʹ ࣮ߦ͢ΔػೳΛ࡞ͨ͠
࣭Ͳ͏ͧ https://github.com/Code-Hex/p5-Shell-Tiny