[[PageOutline]] * [http://qiita.com/b4b4r07/items/62d56b7de2b9d6844bb5 シェルスクリプトのコーディングルール2014 - Qiita] * [https://google.github.io/styleguide/shell.xml Shell Style Guide] * getopts, getopt * [http://d.hatena.ne.jp/taiyo/20080211/p1 getopts - コマンド "getopt", "getopts" の使い方を把握する - spikelet days] * [http://qiita.com/b4b4r07/items/dcd6be0bb9c9185475bb bash によるオプション解析 - Qiita] * [http://linux.just4fun.biz/%E9%80%86%E5%BC%95%E3%81%8D%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88/getopts%E3%82%92%E5%88%A9%E7%94%A8%E3%81%97%E3%81%A6%E5%BC%95%E6%95%B0%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%28bash%E3%83%93%E3%83%AB%E3%83%89%E3%82%A4%E3%83%B3%29.html 逆引きシェルスクリプト/getoptsを利用して引数を取得する(bashビルドイン) - Linuxと過ごす] * [http://shellscript.sunone.me/signal_and_trap.html シグナルと trap コマンド - UNIX & Linux コマンド・シェルスクリプト リファレンス] > === trap コマンドの応用 2 > * trap_exit.sh > {{{#!sh > #!/bin/bash > > # EXITシグナルをtrapして終了メッセージを指定する。 > trap "echo '`basename $0`を終了します.'" EXIT > > # 他のシグナルもtrapしておく。 > trap "echo '他のシグナルをtrapしました。'" 1 2 3 15 > > # Ctrl+Cで終了するテストのためにsleepしておく。 > sleep 10 > > exit 0 > }}} > {{{ > $ ./trap_exit.sh > trap_exit.shを終了します. > ※↑終了時に EXIT シグナルを trap したメッセージが表示されている。 > > $ ./trap_exit.sh > ※ ここで Ctrl+c を押す。 > 他のシグナルをtrapしました。 > trap_exit.shを終了します. > ※↑INT シグナルで終了した場合も EXIT シグナルは trap できる。 > }}} * [http://stackoverflow.com/questions/845863/how-to-use-in-an-xargs-command bash - How to use > in an xargs command? - Stack Overflow] * {} の使い方の例(?) * ディレクトリだけを見つける * $ find -exec sh -c '[ -d "$1" ] && echo "$1"' -- {} \; {{{ . ./.ssh ./.cache ./.aptitude ./.debtags ./.screen ./public_html ... }}} * シンボリックリンクを見つける * $ find /etc/apache2/ -exec sh -c '[ -L "$1" ] && echo "$1"' -- {} \; {{{ /etc/apache2/mods-enabled/alias.conf /etc/apache2/mods-enabled/alias.load /etc/apache2/mods-enabled/autoindex.conf /etc/apache2/mods-enabled/autoindex.load /etc/apache2/mods-enabled/dir.conf /etc/apache2/mods-enabled/dir.load ... }}} * [http://stackoverflow.com/questions/6563979/linux-gnu-sort-how-to-use-null-0-as-delimiter bash - linux GNU sort: how to use NULL (\0) as delimiter - Stack Overflow] > -t is the field separator. If you want to use \0 as the line separator then you need to use -z. > > Use the -z option to sort zero-terminated data sets: > {{{ > find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -z | tr '\0' '\n' > }}} * [http://mattn.kaoriya.net/software/lang/shell/20080225124753.htm Big Sky :: findとxargsは便利だけど意外と実行したい処理はsortした結果だったりする場合が多い] > {{{find . -name "*.txt" | grep '/[0-9]¥{8¥}¥.txt' | sort -n | tr ¥¥n ¥¥0 | xargs -0 ...}}} * [http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/ xargs: How To Control and Use Command Line Arguments] * [http://d.hatena.ne.jp/ozuma/20130928/1380380390 shとbashでの変数内の文字列置換など - ろば電子が詰まっている] > {{{ > ${変数名#パターン} → 前方一致でのマッチ部分削除(最短マッチ) > ${変数名##パターン} → 前方一致でのマッチ部分削除(最長マッチ) > ${変数名%パターン} → 後方一致でのマッチ部分削除(最短マッチ) > ${変数名%%パターン} → 後方一致でのマッチ部分削除(最長マッチ) > ${変数名/置換前文字列/置換後文字列} → 文字列置換(最初にマッチしたもののみ) > ${変数名//置換前文字列/置換後文字列} → 文字列置換(マッチしたものすべて) > }}} * [https://stackoverflow.com/questions/16772186/bash-parallelize-md5sum-checksum-on-many-files Bash: parallelize md5sum checksum on many files - Stack Overflow] > {{{find /mnt/data -type f | parallel -j 64 md5sum > md5.txt}}} > > {{{find /mnt/data -type f | xargs -L1 -P24 md5sum > /tmp/result.txt}}} = bash = * [http://fumixlog.blogspot.jp/2009/09/gdm-bashprofile.html インストールと設定の備忘録(第二部): gdm 経由でログインすると .bash_profile が実行されない] > * 直接この中で .bash_profile を呼び出す > * 冒頭の "#!/bin/sh" に "--login" オプションを付加する > しかし今回はもっと簡単に、せっかくこの中で "$HOME/.profile" が呼び出されているのだから、このファイルの中で ". $HOME/.bash_profile" することにした。これで、しばらく様子を見ることにする。 * ~/.profile {{{#!sh if [ -n "$BASH_VERSION" ]; then # include .bash_profile if it exists if [ -f "$HOME/.bash_profile" ]; then . "$HOME/.bash_profile" fi fi }}} * これは、Debian Squeezeでは効果がなかった * /etc/gdm3/Xsession {{{#!sh test -f "$HOME/.bash_profile" && . "$HOME/.bash_profile" }}} * PATH=... などは設定されるが、「/usr/bin/screen -d -RR -U」は実行されない * GUI上でターミナルを開いても、screenセッションを奪わないので逆に便利と言えるかも知れない * [http://d.hatena.ne.jp/hogem/20090411/1239451878 bashのキーバインド(キーボードショートカット) まとめ - readlineとbind、ついでにstty編 - うまい棒blog] * [http://qiita.com/daei/items/76635f9fbf25824b525e |bash| スペースが入ったファイル名の処理 - Qiita] * [http://qiita.com/kawaz/items/6fd4cd86ca98af644a05 パイプ出力を現在のシェル上のwhileに喰わせる上手いやり方 - Qiita] * [http://qiita.com/kawaz/items/98768ba5eea8d6fe6a19 標準出力をファイルのように扱う方法、例えば2つのコマンドの出力結果のdiffを取るとか - Qiita] > {{{diff <(command1) <(command2)}}} * [https://stackoverflow.com/questions/9457233/unlimited-bash-history/12234989#12234989 unix - Unlimited Bash History - Stack Overflow] > In bash 4.3 and later you can also use {{{HISTSIZE=-1 HISTFILESIZE=-1}}}