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 イディオム

第9章. ファイルとその他

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

何が古いファイルをシェルスクリプトにするのか? どうやってファイルを読み取るのか? この章では、そのすべてと、それ以上のことについて話そう。

読み取りファイル

bashスクリプトにファイルを読み込むには、主に3つの方法がある。 ファイル全体を "スルッと "メモリに読み込む方法と、1行ずつ読み込む方法だ。

読み取り

第2章でキーと値のペアを処理するためにすでにread「ユーザ入力の取得」でもう一度見ることになるが、それ以外の主な使い方は、ファイルを読み取り、入力を1行ずつ解析することだ:

$ grep '^nobody' /etc/passwd | read -d':' user shadow uid gid gecos home shell

$ echo "$user | $shadow | $uid | $gid | $gecos | $home | $shell"
 |  |  |  |  |  |

待って、何が起きたの? 僕のデータはどこだ? パイプ(| )で作成されたサブシェルに入ってしまって、出てこないんだ。 これはどうだ?

$ grep '^nobody' /etc/passwd | { \
    read -d':' user shadow uid gid gecos home shell; \
    echo "$user | $shadow | $uid | $gid | $gecos | $home | $shell" \
  }
nobody |  |  |  |  |  |

少しはマシになったが、残りはどこだ? まあ、-d は行末の区切り文字であって、フィールド・セパレーター($IFS )ではない。 もう一回やってみよう:

$ grep '^nobody' /etc/passwd | { \
    read -d':' user shadow uid gid gecos home shell; \
    echo "$user | $shadow | $uid | $gid | $gecos | $home | $shell" \
  }
nobody | x | 65534 | 65534 | nobody | /nonexistent | /usr/sbin/nologin

「楽しみと利益のために$IFSをいじってファイルを読み取る」も参照のこと。

lastpipe

bash 4+を実行している場合、shopt -s lastpipe をセットすると、パイプラインの最後のコマンドを現在のShellで実行し、スクリプトが環境を確認できるようになる。 これは、ジョブ制御が無効になっている場合にのみ機能することに注意する。 、スクリプトではデフォルトだが、対話型セッションでは無効になっていない。 set +m を使ってジョブ制御を無効にすることはできるが、CTRL-C、CTRL-Z、fg 、およびbg が無効になってしまうので、お勧めしない。

マップファイル

mapfile は とも呼ばれるが、同じコマンドである。 bash v4で追加された は、ファイルを配列(リスト)に読み取り、一度に 行だけを読み取ったり、 行をスキップしたり、進捗インジケータ( / )を表示したりするオプションがある。 これは他のメソッドよりずっと使いやすい。 readarray mapfile -n count -s count ...

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