シェルスクリプトメモ
目次
まとめ
Tips
- パスワード認証は
sudo -vで sed -i.bkでバックアップを作成して編集- 権限制御付きコピーは cp ではなく install で
置換
- コマンド置換:コマンドの結果の文字列に置換
- プロセス置換:コマンドの結果を内容に持つファイルに置換
bash -c "$(curl -L https://raw.githubusercontent.com/applejxd/dotfiles/master/install.sh)" # コマンド置換
source <(curl -L https://raw.githubusercontent.com/applejxd/dotfiles/master/deploy.sh) # プロセス置換
リダイレクト
- ファイル・ディスクリプター(FD)
- stdin(入力のデフォルト):0
- stdout(出力のデフォルト):1
- stderr:2
- その他:3, 4, …
- 演算子
- [n]<word:n に入力(word を n で読み込み・参照)
- [n]>word:n を出力(word を n で書き込み・参照)
- [n]>>word:n を追加出力(word を n で書き込み・参照)
- [n]>&word:出力の複製(word の複製を n で書き込み・参照)
- ケーススタディ
- stdout と stderr の両方を file に書き出し:
command > file 2>&1command \> file (FD1 := file)2 \>&1 (FD2 := FD1 のコピー = file)
- stdout を file に. stderr を stdout に:
command 2>&1 > file- command 2>&1 (FD2 := FD1のコピー = stdout)
- > file (FD1 := file)
- stdout と stderr の両方を file に書き出し:
実行環境に応じた分岐
有無に応じて処理
-
コマンドの有無
if type "command1" > /dev/null 2>&1; then command2 fi -
ファイル書き込みの有無
grep -q RegExp /file/to/path if [ $? -ne 0 ]; then command fi
OS 独自の処理
-
Mac OS X
if [[ "$OSTYPE" == "darwin"* ]]; then command fi -
Ubuntu
if [[ -e /etc/lsb-release ]]; then command fi -
Linux
if [[ "$OSTYPE" == "linux-gnu" ]]; then command fi -
WSL
# WSL2 のみ真 [[ "$(uname -r)" =~ microsoft ]] # WSL2 のみ真 [[ "$(uname -r)" =~ WSL2 ]] # WSL1 のみ真 [[ "$(uname -r)" =~ Microsoft ]] # WSL1/WSL2/Distrod で真 [[ "$(uname -r)" =~ (M|m)icrosoft ]] # Distrod 以外 [[ -f /proc/sys/fs/binfmt_misc/WSLInterop ]]