Last active
November 28, 2016 10:31
-
-
Save yn-misaki/816f937c313c016837f69f78861f4ed5 to your computer and use it in GitHub Desktop.
これだけ覚えておけばOK!シェルスクリプトで冪等性を担保するためのTips集 ref: http://qiita.com/yn-misaki/items/3ec0605cba228a7d5c9a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ -e 'ファイル名' ]; then | |
# ファイル/フォルダが存在していれば、ファイル/フォルダを削除するなど | |
rm ファイル名 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ ! -e 'ファイル名' ]; then | |
# そのファイル/フォルダが存在していなければ、ファイル/フォルダを作成するなど | |
touch ファイル名 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ! type docker >/dev/null 2>&1; then | |
# コマンドをインストールする処理とか | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssh x.x.x.x > /dev/null 2>&1 | |
if [ $? -ne 255 ]; then # 失敗の場合 if [ $? -eq 255 ]; then | |
# sshが成功/失敗した時の処理 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 例 | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git diff --exit-code --quiet | |
if [ $? -eq 1 ]; then | |
# 編集中のファイルが存在していた時の処理 | |
git stash # 例 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ $? -eq 0 ]; then # または if [ $? -ne 1 ]; then | |
# 編集中のファイルが存在しない時の処理 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ps aux | grep [m]ysql # 確認したいプロセス名 | |
if [ $? -eq 0 ]; then | |
# プロセスの停止など | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ps aux | grep [m]ysql # 確認したいプロセス名 | |
if [ $? -ne 0 ]; then | |
# プロセスの起動など | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 | |
===================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ps -ef | grep mysql | grep -v grep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo service mysql status > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
# プロセスの停止など | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ grep -q '検索文字列' ファイル名 ]; then | |
# 検索文字列が含まれていれば、処理を行う(置換処理とか) | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo service mysql status > /dev/null 2>&1 | |
if [ $? -eq 3 ]; then | |
# プロセスの起動 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 例:dockerのmysqlが起動するまで待つ | |
while : | |
do | |
docker-compose ps | grep mysql | |
if [ $? -eq 0 ]; then | |
break # 起動したら抜ける | |
fi | |
echo mysqlのdockerコンテナが起動するまで待ちます(30秒) | |
sleep 30 | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while : | |
do | |
# インストールしたい何か | |
npm install -g gulp | |
if [ $? -eq 0 ]; then | |
break # 成功したらループを抜ける | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ ! grep -q '検索文字列' ファイル名 ]; then | |
# 検索文字列が含まれていなければ、処理を行う(追記処理とか) | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
コマンド名 -v > /dev/null 2>&1 # コマンド名 --versionなど | |
if [ $? -eq 127 ]; then | |
# コマンドをインストールする処理とか | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
コマンド名 -v > /dev/null 2>&1 # コマンド名 --versionなど | |
if [ $? -eq 0 ]; then | |
# コマンドをアンインストールする処理とか、依存ライブラリをインストールするとか・・・ | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ -e 'コマンドの場所' ]; then #sshコマンドであれば、/usr/bin/ssh | |
# コマンドをインストールする処理とか | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ which コマンド名 | |
# 例 | |
$ which ssh | |
=========== | |
/usr/bin/ssh | |
=========== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ ! -e 'コマンドの場所' ]; then | |
# コマンドをアンインストールする処理とか、依存ライブラリをインストールするとか・・・ | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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