2. $ whoami • @icchyr • 某濃厚な大学のB3 • Activity • CTF (tuat_mcc, TokyoWesterns) • ICPC (nocow) など
上記の表の通り,Exit Code 1, 2, 126〜165, 255 は特別な意味を持ち,スクリプトやプログラム内で exit に指定するパラメータとしては避けるべきである.とりわけ,Exit Code 127 はトラブルシューティングで混乱の元である("command not found" で終了したのか,プログラム固有のエラーなのか区別できなくなる).しかしながら,多くのスクリプトが exit 1 を一般的な実行を続行できないエラーとして使っている.Exit Code 1 は Bash の一般的なエラーを含め,とても多くのエラーで発生しうるので,デバッグの時に切り分けが大変になるだろう. Exit Code を体系立てて定義する試みはある(/usr/include/sysexits.h)が,これは C と C++ プログラマー向けである.スクリプトに関しても同様な感じにするのが適切
ここ最近、沢山シェルスクリプトを書くようになりました。 元々あまりシェルスクリプトを書いたこと無かったので、色々と勉強しつつ書いてるのですが、 他のプログラミング言語とはちょっと違って独特なクセというか、発見の度におぉー!ってなることが沢山あって楽しいです。 そんなわけで、最近学んだり参考にした中で特に感動したシェルの上手い書き方をまとめてみます。 きっとまだ知らないこととかもっと上手くやる方法なんかが沢山見つかりそうなので、 もっといいやり方あるよ!って方はコメントください 何もしない : (コロン)コマンド シェルを書いていた時に非常に欲しかったコマンドがこれ!何もしない! : というコマンド(?)を利用すると、何もせずに終了ステータス0(つまり正常終了)を返します。 これが様々な事に使える万能コマンドで、これによって面倒なエラー処理を簡潔にできたり、 入力や出力のリダイレクト元/先と
シェルスクリプト入門として, 基本的な書き方をまとめました. 長いですが, 1ページにまとめてみました. 良かったら目次も参考にしてご覧になって下さい. 目次 シェルスクリプトとは 作り方, 実行の仕方 コメント ユーザーからのキーボード入力を受け付ける 変数 通常の変数 特別な変数 演算子 数値計算演算子 比較演算子 コマンドを繋げる演算子 条件文に使える比較演算子 条件文 制御構文(分岐) if文 case文 制御構文(ループ) for文 while文 until文 select文 文字列処理 文字列置換 削除 複数行のテキストの出力(ヒアドキュメント) 関数 シェルスクリプトとは シェルスクリプトとは, シェルの動作をまとめて記述したスクリプトのことです. 決められた文法にしたがって処理を記述することによって, シェルでの処理をまとめて行ったり, 作業を自動化できたりします(例 複数
ASCII Booksのサイトをご利用いただき、ありがとうございます。 2016年12月6日をもちまして、サイトを閉鎖させていただくことになりました。 今までサイトをご利用いただき、ありがとうございました。 アスキー・メディアワークスを引き続き、よろしくお願いいたします。
僕はコマンドラインで使うシェルスクリプトを書くことがけっこうあるんだけど、インターフェイスというか呼び出し方はとても大事だと思ってるので、そこにわりと時間をかけて考えるようにしてる。実装はいつでも変更できるけど呼び出し方を変えた時は利用者にも変更を強いるので、できれば最初から良い設計で作りたいと思っている。 そこで、僕がシェルスクリプトのオプションとか引数とかの仕様を決める上で注意していることをまとめてみた。シェルスクリプトや、その他コマンドラインのツールを作るときに参考にしてほしい。 シェルの種類は bash や zsh を想定してるけど、実装によらない話なのでどんなシェルでも使えると思う。 エラーの時に Usage (使い方ヘルプメッセージ)を表示するのはやめる エラーになった時に Usage (使い方ヘルプメッセージ) を表示するスクリプトがあるけど、やめたほうがいいと思う。例えばこ
※ 2014-04-26 追記並びに一部コマンド部分の修正を行いました。( > => >> に変更 ) 個人用のチラシの裏のつもりが予想以上に反響いただいていたようで非常にびっくりしております。 ちょっとしたバッチ処理的なものはさくっとシェルスクリプトでやっています。 で、ログをとっておくべくリダイレクトを噛ますわけですが、 スマートに書く方法を調べたのでメモ。 元ネタは @sechiro さんの bashのプロセス置換機能を活用して、シェル作業やスクリプト書きを効率化する でございます。 本当に参考になりました。ありがとうございます。 今までは こんなことやってたわけです。 #!/bin/bash LOGFILE=/tmp/script-log command1 >> $LOGFILE 2>&1 command2 >> $LOGFILE 2>&1 ... >> $LOGFILE 2>&1
tailは便利だけど、白黒で見るのはすごく疲れます。 なので、色を付けてみたいと思います。 色をつけるにはANSI エスケープシーケンスを使えばよいらしい。 1、ANSI エスケープシーケンスとは。 ディスプレイに文字を表示する際のエスケープ。 つまり、今自分が使っているターミナルが、 受け取った文字列を表示する際の制御文字みたいなものらしい。 ANSI エスケープシーケンスを使うと、色を変えるだけでなく、 コマンドラインの表示をいろいろカスタムできるらしい。 ANSIエスケープシーケンスを使って端末に時計を表示するとか。 2、ANSIエスケープの書き方。 こんな感じ。 # karinoを赤色で標準出力 echo -e "\033[0;31mkarino\033[0m" # karinoの背景を緑色で標準出力 echo -e "\033[42mkarino\033[0m" # karino
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く