SlideShare ist ein Scribd-Unternehmen logo
1 von 13
©nirneege system all rights reserved.
Bashクローンを作って得た知見
Black screen がBlack boxではなくなった話
©nirneege system all rights reserved.
p. 2
はじめに(おことわり)
• 42 Tokyo というプログラミングスクールでペア課題をやったときの経験の話です
• レビュー時の解説資料を基に、可能な限り専門用語やC言語の知識を排除して再構成し
ました
• 内容が飛躍してる部分があって混乱するかもしれません...あしからず
• 詳細を見ていくと半日かかるので、、、
• 実装方法には触れず、Bash のしくみや得られた知見に絞って話します
• いつも以上に早口かもしれません...お手柔らかに🙇
©nirneege system all rights reserved.
p. 3
シェルの処理ルーチン(超ざっくり)
1. ユーザーの入力を受け取る
2. スペースなどの区切り文字で内容を分けて、トークン(シェル文法の意味単位)に分解
する
3. トークンを解析して、実行順序を構造化した二分木を作る
4. 作成した二分木に沿って、コマンドプログラムを実行させる
5. 実行したプログラムの終了コードを取得する
©nirneege system all rights reserved.
p. 4
トークンに分ける
$ cat -n /etc/passwd | grep y-uchiida
トークン → ここでは、シェルが解釈する言語の、「文法上の単語」のこと
例えば上記のような入力を受け取った場合、シェルが解釈するのは6つの「単語」
©nirneege system all rights reserved.
p. 5
シェルの文法
$ cat -n /etc/passwd | grep y-uchiida
トークンの並び順や、特定のトークンが持つ意味から、コマンド全体の意味を解釈する
実行するべきコマンド名、実行時のオプション、実行の順番、 など
上記の入力を受け取った場合、 cat とgrepがコマンド名 で、 -n はオプションで、
cat を先に処理してパイプで結果を渡す ということを解析してプログラム実行させる
そのために、BNFという、文法を定義するための汎用的な記述方法で、シェルの文法を定
義する
※BNF: バッカス=ナウア記法。言語構造を表す性質上、HTMLの遠~い親戚みたいなやつ
©nirneege system all rights reserved.
p. 6
コマンドを二分木で表現する
$ cat -n /etc/passwd | grep y-uchiida
トークン: "|"
タイプ:パイプ
トークン: "cat"
意味種別:コマンド名
トークン: "-n"
意味種別:コマンド引数
トークン: "cat"
意味種別:コマンド引数
null
null
トークン: "grep"
意味種別:コマンド名
トークン: "y-uchiida"
意味種別:コマンド引数
null
©nirneege system all rights reserved.
p. 7
コマンドを実行する
• コマンドプログラムは、C言語のシステムコール(exec系SC)から実行できる
• 通常のコマンドプログラム(実行ファイル)は、動作が完了すると終了してしまう
• シェルは終了してはいけないので、コマンドプログラムは自身では動かせない
→複製プロセスを作って、そちらに実行させる(プロセスフォーク)
shell
shell
↓
cat
shell
↓
grep
実行終了 実行終了
出力結果
©nirneege system all rights reserved.
p. 8
シェルのコマンドには2種類ある
• 内部コマンドと、外部コマンド
• 内部コマンド: シェルの動作に影響を及ぼすコマンド
• 外部コマンド: 組み込みコマンド以外のコマンド(実行ファイルがある)
• 内部コマンドの例: cd(ディレクトリ移動), export(環境変数設定), su(ユーザー切替)
• なぜ内部コマンドがある?
• シェルのプロセス自身に実行させるため
• プロセスフォークして変更を実行しても、変更完了とともに消えてしまうので意味
がない
• 詳しくはウチイダのブログで!(宣伝)
https://uchiida.com/2021/02/sudo-command-not-found/
©nirneege system all rights reserved.
p. 9
まとめと感想
• 日ごろ使ってるBashが、どんなことをやってるか知れてよかった
• すごい難しいことをしているのかと思っていたけど、基本はシンプルだった
(※周辺機能ではすごい難しいこともしてた)
• (宣伝)42Tokyoおもしろいので、じっくり学びたい方は挑戦してみてください💕
©nirneege system all rights reserved.
2022.
p. 10
補足
©nirneege system all rights reserved.
p. 11
実際に使用したBNF(シェル文法定義書)
/*
** 再帰下降型の構文解析器ためのシェル構文
** 左再帰が発生しない構文に変更してある
** 以下の文法構造に沿って、tokenを抽象構文木に落とし込んでいく
**
** <command line> ::= <job> ';' <command line>
** | <job> ';'
** | <job> '&' <command line>
** | <job> '&'
** | <job>
**
** <job> ::= <command> '|' <job>
** | <command>
**
** <command> ::= <simple command> '<' <filename>
** | <simple command> '>' <filename>
** | <simple command> '>>' <filename>
** | <simple command>
**
**
** <simple command> ::= <pathname>
** | <simple command> <token>
**
** <token list> ::= <token> <token list>
** | (EMPTY)
*/
©nirneege system all rights reserved.
2022.
p. 12
参考資料
©nirneege system all rights reserved.
p. 13
参考資料
1. Architecture of Open Source Applications( 日本語訳版 )
第3章がBashの仕様についての説明になっている
https://m-takagi.github.io/aosa-ja/aosa.pdf
2. Swoorup/mysh
C言語でのシンプルなシェル実装例と仕様解説
https://github.com/Swoorup/mysh/
3. 原理原則で理解するbashの仕組み
Bash の仕様と実際のコードに対する詳細な解説
https://qiita.com/tajima_taso/items/149ca77a2401bf9bf026

Weitere ähnliche Inhalte

Empfohlen

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Empfohlen (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

20221127_LT大会_Bashクローンを作って得た知見.pptx

  • 1. ©nirneege system all rights reserved. Bashクローンを作って得た知見 Black screen がBlack boxではなくなった話
  • 2. ©nirneege system all rights reserved. p. 2 はじめに(おことわり) • 42 Tokyo というプログラミングスクールでペア課題をやったときの経験の話です • レビュー時の解説資料を基に、可能な限り専門用語やC言語の知識を排除して再構成し ました • 内容が飛躍してる部分があって混乱するかもしれません...あしからず • 詳細を見ていくと半日かかるので、、、 • 実装方法には触れず、Bash のしくみや得られた知見に絞って話します • いつも以上に早口かもしれません...お手柔らかに🙇
  • 3. ©nirneege system all rights reserved. p. 3 シェルの処理ルーチン(超ざっくり) 1. ユーザーの入力を受け取る 2. スペースなどの区切り文字で内容を分けて、トークン(シェル文法の意味単位)に分解 する 3. トークンを解析して、実行順序を構造化した二分木を作る 4. 作成した二分木に沿って、コマンドプログラムを実行させる 5. 実行したプログラムの終了コードを取得する
  • 4. ©nirneege system all rights reserved. p. 4 トークンに分ける $ cat -n /etc/passwd | grep y-uchiida トークン → ここでは、シェルが解釈する言語の、「文法上の単語」のこと 例えば上記のような入力を受け取った場合、シェルが解釈するのは6つの「単語」
  • 5. ©nirneege system all rights reserved. p. 5 シェルの文法 $ cat -n /etc/passwd | grep y-uchiida トークンの並び順や、特定のトークンが持つ意味から、コマンド全体の意味を解釈する 実行するべきコマンド名、実行時のオプション、実行の順番、 など 上記の入力を受け取った場合、 cat とgrepがコマンド名 で、 -n はオプションで、 cat を先に処理してパイプで結果を渡す ということを解析してプログラム実行させる そのために、BNFという、文法を定義するための汎用的な記述方法で、シェルの文法を定 義する ※BNF: バッカス=ナウア記法。言語構造を表す性質上、HTMLの遠~い親戚みたいなやつ
  • 6. ©nirneege system all rights reserved. p. 6 コマンドを二分木で表現する $ cat -n /etc/passwd | grep y-uchiida トークン: "|" タイプ:パイプ トークン: "cat" 意味種別:コマンド名 トークン: "-n" 意味種別:コマンド引数 トークン: "cat" 意味種別:コマンド引数 null null トークン: "grep" 意味種別:コマンド名 トークン: "y-uchiida" 意味種別:コマンド引数 null
  • 7. ©nirneege system all rights reserved. p. 7 コマンドを実行する • コマンドプログラムは、C言語のシステムコール(exec系SC)から実行できる • 通常のコマンドプログラム(実行ファイル)は、動作が完了すると終了してしまう • シェルは終了してはいけないので、コマンドプログラムは自身では動かせない →複製プロセスを作って、そちらに実行させる(プロセスフォーク) shell shell ↓ cat shell ↓ grep 実行終了 実行終了 出力結果
  • 8. ©nirneege system all rights reserved. p. 8 シェルのコマンドには2種類ある • 内部コマンドと、外部コマンド • 内部コマンド: シェルの動作に影響を及ぼすコマンド • 外部コマンド: 組み込みコマンド以外のコマンド(実行ファイルがある) • 内部コマンドの例: cd(ディレクトリ移動), export(環境変数設定), su(ユーザー切替) • なぜ内部コマンドがある? • シェルのプロセス自身に実行させるため • プロセスフォークして変更を実行しても、変更完了とともに消えてしまうので意味 がない • 詳しくはウチイダのブログで!(宣伝) https://uchiida.com/2021/02/sudo-command-not-found/
  • 9. ©nirneege system all rights reserved. p. 9 まとめと感想 • 日ごろ使ってるBashが、どんなことをやってるか知れてよかった • すごい難しいことをしているのかと思っていたけど、基本はシンプルだった (※周辺機能ではすごい難しいこともしてた) • (宣伝)42Tokyoおもしろいので、じっくり学びたい方は挑戦してみてください💕
  • 10. ©nirneege system all rights reserved. 2022. p. 10 補足
  • 11. ©nirneege system all rights reserved. p. 11 実際に使用したBNF(シェル文法定義書) /* ** 再帰下降型の構文解析器ためのシェル構文 ** 左再帰が発生しない構文に変更してある ** 以下の文法構造に沿って、tokenを抽象構文木に落とし込んでいく ** ** <command line> ::= <job> ';' <command line> ** | <job> ';' ** | <job> '&' <command line> ** | <job> '&' ** | <job> ** ** <job> ::= <command> '|' <job> ** | <command> ** ** <command> ::= <simple command> '<' <filename> ** | <simple command> '>' <filename> ** | <simple command> '>>' <filename> ** | <simple command> ** ** ** <simple command> ::= <pathname> ** | <simple command> <token> ** ** <token list> ::= <token> <token list> ** | (EMPTY) */
  • 12. ©nirneege system all rights reserved. 2022. p. 12 参考資料
  • 13. ©nirneege system all rights reserved. p. 13 参考資料 1. Architecture of Open Source Applications( 日本語訳版 ) 第3章がBashの仕様についての説明になっている https://m-takagi.github.io/aosa-ja/aosa.pdf 2. Swoorup/mysh C言語でのシンプルなシェル実装例と仕様解説 https://github.com/Swoorup/mysh/ 3. 原理原則で理解するbashの仕組み Bash の仕様と実際のコードに対する詳細な解説 https://qiita.com/tajima_taso/items/149ca77a2401bf9bf026