シェルプログラミング実用テクニック
https://gyazo.com/a7fe439c5e2c4edd9b16885736bb5de1
さらっと読んでメモ書きを残していく。
コマンド自体にある程度馴染みがあるレベル感で読むと良い。実際のやりたいことやケースに合わせてどのようにコマンドを組み合わせればいいかの実例が豊富。コマンドの組み立ての考え方を知ることができる。
こんなこともできるのか、というのも分かるので理解で終わるだけじゃなくて利用シーンの想像力も広がりそう。
dev/nullを使う
大量のエラーに出力が埋もれてしまう時
grep hoge /etc/ 2> /dev/null としておくとエラー以外の情報だけ出力できる
ベンチマークなどで出力結果が不要な時
time seq 1 10000000 > /dev/null
テストコマンド 多用しないこと。文字列や数字を複数回比較する場合は、AWKやgrepを使うことを検討する
[で引数で渡された条件を判定して、真なら0、偽なら1 、変な引数が指定されたらそれ以外を返す
[ -f "/etc/passwd" ] || exit 1 あるファイルがなければ、シェルスクリプトを終了する処理
|| 左側のコマンドの終了ステータスが0でなければ、右側のコマンドを実行する
&& 上記を&&に帰ると、終了ステータスが0のとき、右側のコマンドを終了する
AWKとsed
共にスクリプト言語である
NFは予約語で、各行の列の数。下記の場合は列の数から1引いた列(つまり最後から2つ目の列)を指定している
NRも同様。行の数を表す。
awkでは、字を"で囲むと文字列扱い、囲まないと数値扱い
code:awk
19:38 ~/test1 $ cat data | awk '{print $(NF-1)}'
4
9
19:39 ~/test1 $ cat data
1 2 3 4 5
6 7 8 9 10
# 5列目が5
19:43 ~/test1 $ cat data | awk '$5==5'
1 2 3 4 5
# 4列目が5以上
19:44 ~/test1 $ cat data | awk '$4>=5'
6 7 8 9 10
# 5列目が文字列の5
19:44 ~/test1 $ cat data | awk '$5=="5"'
1 2 3 4 5
19:44 ~/test1 $
19:40 ~/test1 $ echo {a..g} | xargs -n 1
a
b
c
d
e
f
g
19:42 ~/test1 $ echo {a..g} | xargs -n 1 | awk 'NR>=4'
d
e
f
g
19:42 ~/test1 $
正規表現を使う
code:正規表現
20:01 ~ $ grep '80/tucdp' /etc/services | wc -l 102
20:01 ~ $ grep '80/..p' /etc/services | wc -l
102
20:01 ~ $ grep '[^80/tucdp' /etc/services | wc -l 360
20:02 ~ $ grep '^80/tucdp' /etc/services | wc -l 7864
20:02 ~ $ grep '^ 80/tucdp' /etc/services | wc -l 7864
20:02 ~ $ grep '^2-8000/tucdp' /etc/services | wc -l 16
20:02 ~ $ grep '0.*/tucdp' /etc/services | wc -l 3045
20:03 ~ $ grep '0*/tucdp' /etc/services | wc -l 9913
20:03 ~ $ sudo grep -r key /etc/ssh
Password:
/etc/ssh/sshd_config:#HostKey /etc/ssh/ssh_host_rsa_key
/etc/ssh/sshd_config:#HostKey /etc/ssh/ssh_host_ecdsa_key
/etc/ssh/sshd_config:#HostKey /etc/ssh/ssh_host_ed25519_key
/etc/ssh/sshd_config:# Ciphers and keying
/etc/ssh/sshd_config:#RekeyLimit default none
/etc/ssh/sshd_config:#PubkeyAuthentication yes
/etc/ssh/sshd_config:# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
/etc/ssh/sshd_config:# but this is overridden so installations will only check .ssh/authorized_keys
/etc/ssh/sshd_config:AuthorizedKeysFile .ssh/authorized_keys
/etc/ssh/sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
/etc/ssh/sshd_config:# Change to no to disable s/key passwords
/etc/ssh/ssh_config:# RekeyLimit 1G 1h
20:04 ~ $
trとかsedを使って、いい感じに整形した後にgrepしたりwc -mしたりする
code:sed, tr
$ echo "takasitakeshi" | sed 's/taka/take/'
takesitakeshi
takasetakese
$ echo "takashitakeshi" | sed 's/shi/se/'
takasetakeshi
$ cat /etc/services | grep http | tr '\t' ' ' | grep ' 80/' # tr '置換前' '置換後'
http 80/udp www www-http # World Wide Web HTTP
http 80/tcp www www-http # World Wide Web HTTP
$
cp -p 元のファイルのタイムスタンプを保持したままコピー
rsync
/home/ueda/ を /var/temp/ の下に丸ごとコピーする
/home/ueda/ とすると、 /home/ueda/ の下、/home/uedaとすると、uedaディレクトリそのもの
スラッシュなしを使わないようにする
上記スラなしの実行をしたい場合は、宛先ディレクトリを/var/temp/ueda/とすれば良い
オプション=-n ドライラン、-u 古いファイルでの上書きを禁止
code:rsync
# -a ファイルの属性をなるべく残す、-v コピーしたファイルなどのログを出力する
$ rsync -av /home/ueda/ /var/temp/
# test2ディレクトリの下にtest2を作って、そこにtest2の中身をrsyncする
$ rsync -avn test2/ test1/test2/
building file list ... done
created directory test1/test2
./
addfile.txt
command.txt
ipaddress.txt
rsynclog.txt
snippets.md
testfile
test1/
test1/command.txt
test1/data
test1/getopt
test1/ipaddress.txt
test1/optgetsample
test1/read.bash
test1/snippets.md
test1/testfile
sent 492 bytes received 116 bytes 1216.00 bytes/sec
total size is 9841 speedup is 16.19
$
日付を扱う findに-mtimeをオプションで渡すと、modified timeで検索できる
-daystartというオプションで0時始まりにできる
code:find
$ find . -mtime 10 | tail -n 3
$ find . -mtime 10 | tail -n 3
find: ./Library/Saved Application State/com.bitrock.appinstaller.uninstall.savedState: Permission denied
find: ./Library/Saved Application State/com.installbuilder.appinstaller.savedState: Permission denied
find: ./Library/Saved Application State/com.bitrock.appinstaller.savedState: Permission denied
^C
$ find . -mtime 10 | head -n 3 2> /dev/null
./Library/Application Support/Google/Chrome/Safe Browsing/UrlCsdDownloadWhitelist.store
./Library/Application Support/Google/Chrome/Profile 1/IndexedDB/https_drive.google.com_0.indexeddb.blob/5/01/1d0
./Library/Application Support/Google/Chrome/Profile 1/IndexedDB/https_drive.google.com_0.indexeddb.blob/5/01/1d7
$