SlideShare a Scribd company logo
1 of 57
Download to read offline
ShellCheckを使おう
2021年05月22日
自己紹介
名前: 三宅 英明
Twitter: @mollifier
神戸のプログラマ
ShellCheckを使おう
目次
ShellCheckとは
ShellCheckをインストールする
ShellCheckの使い方
ShellCheckのissueを無視する
ShellCheckとは
ShellCheckとは
bashの文法や構文は、わりと「ゆるふわ」なところが
あります
ShellCheckとは
たとえば、未定義の変数をいきなり参照してもエラー
にならない
ShellCheckとは
message=hello
echo $msg #=> 空行が出力される
ShellCheckとは
それに、細かな記号の使い方によって動作が変わる
ところもあります
ShellCheckとは
message="hello world"
echo $message #=> hello worldと出力される(ス
ペースが1つだけになる)
ShellCheckとは
message="hello world"
echo "$message" #=> hello worldと出力さ
れる
ShellCheckとは
echo $message でも echo "$message" でも、どち
らも文法上は誤りではない。
ただし、""の有無によって動作が変わる。
ShellCheckとは
message="helloworld"
echo $message
echo "$message"
$messageに入っている文字列にスペースやタブが
含まれていないなら、""をつけてもつけなくても結果は
同じ
ShellCheckとは
message="hello world"
echo "$message"
スペースやタブが入っていて、それをそのまま出力し
たいなら""で囲まなければならない
ShellCheckとは
でも、""で囲まなくても文法の間違いではない
ShellCheckとは
なので実行してもエラーにはならない。bashは間違
いを教えてくれない
こういうのはソースコードを見て見つけるか、テストを
して直すしかない
ShellCheckとは
それも大変。
こういう間違いを見つけてくれるやつはないのか?
ShellCheckとは
そういうときにShellCheckが役に立ちます
ShellCheckとは
https://www.shellcheck.net/
ShellCheckとは
ShellCheckとは、bashやshのソースコードを解析し
て、推奨されなかったり不具合を起こしやすかったり
する書き方を探して指摘してくれるツール
ShellCheckをインストールする
ShellCheckをインストールする
Ubuntuではパッケージが用意されているので、すぐイ
ンストールできます
$ sudo apt install shellcheck
ShellCheckをインストールする
CentOSの場合、EPELの中にパッケージがあります
$ dnf install epel-release
$ dnf install ShellCheck
EPEL = Extra Packages for Enterprise Linux
ShellCheckをインストールする
Macの場合、Homebrewのパッケージがあります
$ brew install shellcheck
ShellCheckをインストールする
その他の環境でも、公式のインストール方法の説明を
読めばできるでしょう
https://github.com/koalaman/shellcheck#user
-content-installing
ShellCheckの使い方
ShellCheckの使い方
ShellCheckの基本的な使い方
ShellCheckの使い方
shellcheck チェックしたいファイル、というふうに使う
ShellCheckの使い方
実行している例
$ shellcheck test/check1.sh
ShellCheckの使い方
出力の例
In test/check1.sh line 4:
echo $message
^------^ SC2086: Double quote to
prevent globbing and word splitting.
Did you mean:
echo "$message"
ShellCheckの使い方
出力の例の続き
For more information:
https://www.shellcheck.net/wiki/SC2086
-- Double quote to prevent globbing ...
ShellCheckの使い方
ShellCheckでは見つけた問題のことをissueと呼び
ます
ShellCheckの使い方
さきほどの例では、一つのissueが見つかりました
ShellCheckの使い方
In test/check1.sh line 4:
issueが見つかったファイル名と行番号
ShellCheckの使い方
echo $message
^------^ SC2086: Double quote to
prevent globbing and word splitting.
issueにはそれぞれ一意的な識別子が割り当てられ
ています。 それをshellcheckではcodeと呼んでいま
す。
今回見つかったissueのcodeはSC2086です。
ShellCheckの使い方
^------^ SC2086: Double quote to
prevent globbing and word splitting.
その後ろにはissueのタイトルが出力されています。こ
こを見ればなにが問題なのかの概要が分かります。
ShellCheckの使い方
Did you mean:
echo "$message"
その下には直し方の見本が出力されます
ShellCheckの使い方
For more information:
https://www.shellcheck.net/wiki/SC2086
-- Double quote to prevent globbing ...
最後に、このissueの詳細が書かれているドキュメント
へのリンクURLが紹介されています。
ShellCheckの使い方
ShellCheckのドキュメントは優秀で、すべてのissue
に詳しい説明が書かれています。
ShellCheckの使い方
これを読めば、なぜだめなのか、どういう場合に問題
が発生するのかが分かります
ShellCheckの使い方
中にはExceptions(例外)という項目が書かれた
issueもあります。
こういう場合はしょうがないのでこのissueを無視して
もよいかもね、という説明です
ShellCheckの使い方
ShellCheckのドキュメントを読むだけでも勉強になり
ます。参考にしてください
ShellCheckのissueを無視する
ShellCheckの指摘はかなり厳しいです
実際には、厳しすぎてどうしても一部無視しないとい
けないこともあります
ShellCheckのissueを無視する
そういうときは、ソースコードに次の形式でコメントを
書きます
# shellcheck disable=issueのcode
ShellCheckのissueを無視する
これで、その次の行は指定したcodeのissueは無視さ
れて、出力されません
ShellCheckのissueを無視する
例
message="hello world"
# shellcheck disable=SC2086
echo $message
ShellCheckのissueを無視する
または、ShellCheckの起動オプションで -e または -
-exclude を指定しても無視できます
ShellCheckのissueを無視する
-e issueのcode
または
--exclude=issueのcode
で指定します
ShellCheckのissueを無視する
複数のissueを無視したい場合は , 区切りで並べて
指定します
ShellCheckのissueを無視する
例
$ shellcheck -e SC2086,SC2087 check1.sh
ShellCheckのissueを無視する
ただし、あんまり無視しすぎるとShellCheck使ってる
意味がなくなるので、ほどほどに
ShellCheckのissueを無視する
状況に応じて使ってください
まとめ
まとめ
ShellCheckを使うと、シェルの罠や間違いやすいポイ
ントを回避できます
まとめ
先人たちの英知がつまっているので、きっと知らなか
ったissueがあるはずです
まとめ
ShellCheckを使ってシェルスクリプトの品質を高めま
しょう
まとめ
ありがとうございました
ShellCheckを使おう
目次
ShellCheckとは
ShellCheckをインストールする
ShellCheckの使い方
ShellCheckのissueを無視する

More Related Content

Featured

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
 

Featured (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
 

ShellCheckを使おう