- 実行環境はこちら
user@ubuntu:~$ uname -a Linux ubuntu 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux user@ubuntu:~$
- 環境変数の表示(env, printenv)
普段 echo $[変数名] しか使わないので、今後は env や printenv も活用しようと思った次第。
user@ubuntu:~$ env |grep PATH user@ubuntu:~$ printenv PATH user@ubuntu:~$ echo $PATH
- リダイレクトによるファイルの上書き防止(set -o noclobber, set -C)
自分でシェルスクリプトを書く時は、先頭にこのオプション入れてもいいかも、と思う。
user@ubuntu:~$ echo "xxx" >/tmp/tmpfile.txt user@ubuntu:~$ echo "xxx" >/tmp/tmpfile.txt user@ubuntu:~$ set -o noclobber user@ubuntu:~$ echo "xxx" >/tmp/tmpfile.txt -bash: /tmp/tmpfile.txt: cannot overwrite existing file user@ubuntu:~$ set +o noclobber user@ubuntu:~$ echo "xxx" >/tmp/tmpfile.txt user@ubuntu:~$ set -C user@ubuntu:~$ echo "xxx" >/tmp/tmpfile.txt -bash: /tmp/tmpfile.txt: cannot overwrite existing file user@ubuntu:~$
- ファイルを変更できないようにする(chattr)
こんなコマンドがあるとは知らなかった。
rmなどのコマンドが失敗した場合のエラーハンドリングのテスト時などに使えそう。
user@ubuntu:~$ touch testfile user@ubuntu:~$ lsattr testfile -------------e-- testfile user@ubuntu:~$ sudo chattr +i testfile [sudo] password for user: user@ubuntu:~$ lsattr testfile ----i--------e-- testfile user@ubuntu:~$ ls -l testfile -rw-rw-r-- 1 user user 0 May 24 07:00 testfile user@ubuntu:~$ rm testfile rm: remove write-protected regular empty file ‘testfile’? y rm: cannot remove ‘testfile’: Operation not permitted user@ubuntu:~$ sudo rm -f testfile rm: cannot remove ‘testfile’: Operation not permitted user@ubuntu:~$ sudo chattr -i testfile user@ubuntu:~$ lsattr testfile -------------e-- testfile user@ubuntu:~$ rm testfile user@ubuntu:~$ ls -l testfile ls: cannot access testfile: No such file or directory user@ubuntu:~$
- grepした結果、ヒットしたファイル名だけを表示
今まで grep -rn foobar * |cut -d':' -f1 |sort |uniq とかで乗り切ってたので、-l オプションがある grep は非常に助かる
user@ubuntu:~/r$ grep -l AppleWebKit *01.csv 001-2013-01.csv 001-2014-01.csv 001-2015-01.csv user@ubuntu:~/r$