Skip to Content
実践 bashによるサイバーセキュリティ対策 ―セキュリティ技術者のためのシェルスクリプト活用術
book

実践 bashによるサイバーセキュリティ対策 ―セキュリティ技術者のためのシェルスクリプト活用術

by Paul Troncone, Carl Albing, 髙橋 基信
April 2020
Intermediate to advanced
352 pages
4h 11m
Japanese
O'Reilly Japan, Inc.
Content preview from 実践 bashによるサイバーセキュリティ対策 ―セキュリティ技術者のためのシェルスクリプト活用術

付録Cbashスクリプトのトレースオプション

宮本 久仁男

本付録は日本語版オリジナルの記事である。本稿では、bashのトレースオプションについて解説する。bashのトレースオプションを指定すると、普通に実行すると何の出力もしないところで「今何を実行しているか」を出力させることが可能になるので、スクリプトのデバッグに役立つ。

C.1 bashのトレースオプション「-x」

bash実行時に、「何を実行しているか」を確認するためには、2通りの方法がある。

  • スクリプトの先頭で#!/bin/bashというようになっているところを、#!/bin/bash -xと指定して実行する。
  • スクリプト実行時に、bash -x [スクリプトファイル]というようにして、当該スクリプト実行時にトレース結果を表示するように指定する。

なお、トレース結果の出力先は、標準エラー出力である。このため、2>という表現を用いて指定ファイルにリダイレクトを行うことが可能である。

さて、bashに対して与える-xオプションだが、bashのmanにはそのものの記述がない。その代わり、-oオプションに与えるパラメータxtraceの説明で、「-xと同じ」という記述がある。

ただ、以下の利用例で-xではなく-o xtraceを使用した場合、一部動かないケースを確認したので、本稿ではトレースオプションの指定には-xを使うこととする。

C.1.1 検証用スクリプトの例

以下の利用例では、ファイル名sample.shというシェルスクリプトを任意のディレクトリに配置し実行する。

なお、sample.shの実行時には、ユーザはsample.shが配置されているディレクトリをカレントディレクトリとしていると仮定する。

#!/bin/bash ...
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

実践 時系列解析 ―統計と機械学習による予測

実践 時系列解析 ―統計と機械学習による予測

Aileen Nielsen, 山崎 邦子, 山崎 康宏
実践 自然言語処理 ―実世界NLPアプリケーション開発のベストプラクティス

実践 自然言語処理 ―実世界NLPアプリケーション開発のベストプラクティス

Sowmya Vajjala, Bodhisattwa Majumder, Anuj Gupta, Harshit Surana, 中山 光樹
情報アーキテクチャ 第4版 ―見つけやすく理解しやすい情報設計

情報アーキテクチャ 第4版 ―見つけやすく理解しやすい情報設計

Louis Rosenfeld, Peter Morville, Jorge Arango, 篠原 稔和, 岡 真由美

Publisher Resources

ISBN: 9784873119052Other