Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Last active July 9, 2016 02:46
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 tomoyamkung/d17053e8bf57258e0a00bab064db8926 to your computer and use it in GitHub Desktop.
Save tomoyamkung/d17053e8bf57258e0a00bab064db8926 to your computer and use it in GitHub Desktop.
コマンドがインストールされているかを確認するスクリプト。 `which` を使って指定されたコマンドがシステムに存在するかを確認するスクリプトと、そのユニットテストを作成。
function installed_command() {
which $1 > /dev/null 2>&1
}
#!/bin/bash
# dotfiles/etc/test 直下に移動する
cd `dirname $0`
# テスト対象のスクリプトを読み込む
source ../lib/installed_command.sh
# `ls` コマンドは存在するので `$? = 0` になるはず
installed_command ls
if [ ! $? -eq 0 ]; then
echo "ls コマンドは存在します"
fi
# `dummy_command` コマンドは存在しないので `$? != 0` になるはず
installed_command dummy_command
if [ $? -eq 0 ]; then
echo "dummy_command コマンドは存在しません"
fi
# 別解
installed_command ls || echo "ls はインストールされているはずです"
installed_command dummy_command && echo "dummy_command はインストールされていないはずです"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment