Skip to Content
bash イディオム
book

bash イディオム

by Carl Albing, JP Vossen
May 2025
Intermediate to advanced
170 pages
2h 21m
Japanese
O'Reilly Media, Inc.
Content preview from bash イディオム

第3章. 念のため

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

多くのプログラミング言語には、if/then/else の文字列の代わりとして使われる、n方向の分岐である「switch」または「match」文が用意されている。 bashにも似たような構文がある。case 文である。強力なパターン・マッチが付属しており、スクリプト作成に非常に便利である。

ケースを作る

キーワードcasein は、様々なパターンと比較したい値を区切る。以下は簡単な例である:

case "$var" in
    yes ) echo "glad you agreed" ;;
    no  )
        echo "sorry; good bye"
        exit
    ;;
    * ) echo "invalid answer. try again" ;;
esac

$var の値が "yes "か "no "かをチェックし、対応する文を実行する。 case文の終わりは、esaccaseの綴りを逆にしたものである。この例はかなり読みやすいが、表面をなぞっただけである。 YESの場合は「ワンライナー」、NOの場合は典型的なブロック(;;...詳しくは後述)を使っている。 どちらを使うかは、あなたが何をしているか、そして可読性のためにどのようにコードを行ないたいかによる。

( にある。case

case 文の構文には、例の") "と一致するようにオプションの"(")が含まれている。 例えば、"yes") の代わりに("yes") と書くこともできるし、他の項目についても同様である。 しかし、このような使い方はほとんど見たことがない。 結局のところ、誰が余計な文字を入力したいのだろうか?

、最もイディオム的な外観を持つcase ステートメントの真の威力は、シェルのパターンマッチングを使用して、さまざまな値の比較を行うことにある:

case "$var" in

    [Nn][Oo]* )
        echo "Fine. Leave then."
        exit
    ;;
    [Yy]?? | [Ss]ure | [Oo][Kk]* )
        echo "OK. Glad we agree."
    ;;
    * ) echo 'Try again.'
        continue
    ;;
esac

コマンドラインのワイルドカード(またはグロブ)としてお馴染みであろう、bashのパターンマッチングの簡単なおさらいである。? は1文字にマッチし、* は任意の数の文字(何もない場合も含む)にマッチし、[] 、括弧は括弧の間に含まれるすべての文字にマッチする。

この例では、[Yy] という構文は、大文字のYか小文字のyにマッチする。[Nn][Oo]* という構文は、大文字のNか小文字のNにマッチし、その後に大文字のOか小文字のOが続き、その後に任意の数の他の文字が続く。 このパターンマッチングは、次の単語(その他も)にマッチする:noNonONOnowayNot Evernope$var の値がnever の場合はマッチしない。

肯定的なケースで考えられる値をいくつか推測できるか? 縦棒は、すべて同じ結果になる異なるパターンを区切る。 (「OR」を考えてほしいが、|| orではない。)YesyesYESyEsyesyupSuresureOKokokfine、「OK why not」という単語はすべて機能するだろう。 しかし、これらの単語は使えない: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Bash クックブック第2版

Bash クックブック第2版

Carl Albing, JP Vossen
UXデザインの法則 第2版 ―最高のプロダクトとサービスを支える心理学

UXデザインの法則 第2版 ―最高のプロダクトとサービスを支える心理学

Jon Yablonski, 相島 雅樹, 磯谷 拓也, 反中 望, 松村 草也

Publisher Resources

ISBN: 9798341651357