bash

bashでファイルのリダイレクトによる上書きを防止するオプション

自分が運用する環境では、うっかりrm、mv、cpでファイルを消さない/上書きしないようにbashのaliasの機能を使って"-i"オプションを有効にしています。 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'これはこれでいいんですが、このaliasはうっかりフ…

シェルスクリプトで空白をクォートしている引数とコマンドを変数に入れて実行する場合はevalを使う必要がある

何かタイトルが長くてわかりにくいですね。 普通に変数に入れているだけだと、クォートしていても空白区切りで別々の引数として認識される。 foo.sh 【grep "foo bar"】を変数に入れて、それをそのまま実行する。 $ cat foo.sh #!/bin/bash grep='grep "foo …

trap - shell sciprt で SIGNAL を trap

perlでは$SIG{INT} あたりにcoderef を設定すればいいけど、bash/zshではどうやるんだろうー。と悩んでman bash を見ていたらtrap という組み込みコマンドを発見。 trap [-lp] [[arg] sigspec ...] The command arg is to be read and executed when the she…

bashのグロブ展開

^で否定の表現もできたのか。debian 4.0のbash 3.1.17。 $ ls *[^~] => 末尾が"~"以外のファイル名

ubuntuのcommand_not_found_handle

ubuntuは入力したcommandが見つからない、かつそのcommandがaptで用意されているときに、apt-getしたら入るよ、と教えてくれる。 $ sl The program 'sl' is currently not installed. You can install it by typing: sudo apt-get install sl -bash: sl: com…

一時ファイルを作成せずに、コマンドの実行結果のdiffを取る (bashのProcess Substitutionで)

http://tldp.org/LDP/abs/html/process-sub.html コマンドの実行結果を名前付きパイプで読めるらしい。 Command list enclosed within parentheses >(command_list) Process substitution uses /dev/fd/ files to send the results of the process(es) withi…