Skip to content

Instantly share code, notes, and snippets.

@yn-misaki
Last active November 28, 2016 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yn-misaki/816f937c313c016837f69f78861f4ed5 to your computer and use it in GitHub Desktop.
Save yn-misaki/816f937c313c016837f69f78861f4ed5 to your computer and use it in GitHub Desktop.
これだけ覚えておけばOK!シェルスクリプトで冪等性を担保するためのTips集 ref: http://qiita.com/yn-misaki/items/3ec0605cba228a7d5c9a
if [ -e 'ファイル名' ]; then
# ファイル/フォルダが存在していれば、ファイル/フォルダを削除するなど
rm ファイル名
fi
if [ ! -e 'ファイル名' ]; then
# そのファイル/フォルダが存在していなければ、ファイル/フォルダを作成するなど
touch ファイル名
fi
if ! type docker >/dev/null 2>&1; then
# コマンドをインストールする処理とか
fi
ssh x.x.x.x > /dev/null 2>&1
if [ $? -ne 255 ]; then # 失敗の場合 if [ $? -eq 255 ]; then
# sshが成功/失敗した時の処理
fi
#
# mysql -u foo -p bar -e "show databases\G" | grep development > /dev/null 2>&1
mysql -u ユーザ名 -p パスワード -e "SQL\G" | grep 検索したい文字列 > /dev/null 2>&1
if [ $? -ne 0 ]; then # 含まれていない場合 if [ $? -eq 0 ]; then
# db:migrateしてみたり、特定のデータ入れてみたり・・・削除してみたり
fi
git diff --exit-code --quiet
if [ $? -eq 1 ]; then
# 編集中のファイルが存在していた時の処理
git stash #
fi
if [ $? -eq 0 ]; then # または if [ $? -ne 1 ]; then
# 編集中のファイルが存在しない時の処理
fi
ps aux | grep [m]ysql # 確認したいプロセス名
if [ $? -eq 0 ]; then
# プロセスの停止など
fi
ps aux | grep [m]ysql # 確認したいプロセス名
if [ $? -ne 0 ]; then
# プロセスの起動など
fi
$ ps aux | grep mysql
=====================
user 33003 0.0 0.0 2435864 784 s000 S+ 9:50AM 0:00.00 grep mysql ← grepをしているプロセス(mysqlのプロセスではないので不要)
user 32995 0.0 2.7 3086248 458860 s000 S 9:50AM 0:00.49 /usr/local/Cellar/mysql56/5.6.29/bin/mysqld --basedir=/usr/local/Cellar/mysql56/5.6.29 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql56/5.6.29/lib/plugin --log-error=/usr/local/var/mysql/L2753.local.err --pid-file=/usr/local/var/mysql/L2753.local.pid
user 32899 0.0 0.0 2446700 1284 s000 S 9:50AM 0:00.02 /bin/sh /usr/local/Cellar/mysql56/5.6.29/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/L2753.local.pid
=====================
$ ps aux | grep [m]ysql
=====================
user 32995 0.0 2.7 3086248 458860 s000 S 9:50AM 0:00.50 /usr/local/Cellar/mysql56/5.6.29/bin/mysqld --basedir=/usr/local/Cellar/mysql56/5.6.29 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql56/5.6.29/lib/plugin --log-error=/usr/local/var/mysql/L2753.local.err --pid-file=/usr/local/var/mysql/L2753.local.pid
user 32899 0.0 0.0 2446700 1284 s000 S 9:50AM 0:00.02 /bin/sh /usr/local/Cellar/mysql56/5.6.29/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/L2753.local.pid
=====================
$ ps -ef | grep mysql | grep -v grep
sudo service mysql status > /dev/null 2>&1
if [ $? -eq 0 ]; then
# プロセスの停止など
fi
if [ grep -q '検索文字列' ファイル名 ]; then
# 検索文字列が含まれていれば、処理を行う(置換処理とか)
fi
sudo service mysql status > /dev/null 2>&1
if [ $? -eq 3 ]; then
# プロセスの起動
fi
# 例:dockerのmysqlが起動するまで待つ
while :
do
docker-compose ps | grep mysql
if [ $? -eq 0 ]; then
break # 起動したら抜ける
fi
echo mysqlのdockerコンテナが起動するまで待ちます(30秒)
sleep 30
done
while :
do
# インストールしたい何か
npm install -g gulp
if [ $? -eq 0 ]; then
break # 成功したらループを抜ける
fi
done
if [ ! grep -q '検索文字列' ファイル名 ]; then
# 検索文字列が含まれていなければ、処理を行う(追記処理とか)
fi
コマンド名 -v > /dev/null 2>&1 # コマンド名 --versionなど
if [ $? -eq 127 ]; then
# コマンドをインストールする処理とか
fi
コマンド名 -v > /dev/null 2>&1 # コマンド名 --versionなど
if [ $? -eq 0 ]; then
# コマンドをアンインストールする処理とか、依存ライブラリをインストールするとか・・・
fi
if [ -e 'コマンドの場所' ]; then #sshコマンドであれば、/usr/bin/ssh
# コマンドをインストールする処理とか
fi
$ which コマンド名
#
$ which ssh
===========
/usr/bin/ssh
===========
if [ ! -e 'コマンドの場所' ]; then
# コマンドをアンインストールする処理とか、依存ライブラリをインストールするとか・・・
fi
if type docker >/dev/null 2>&1; then
# コマンドをアンインストールする処理とか、依存ライブラリをインストールするとか・・・
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment