LoginSignup
4
3

More than 3 years have passed since last update.

良く忘れるtestコマンドの演算子について

Last updated at Posted at 2019-09-24

自身の備忘を兼ねて記載を行っています。
「とりあえず動いた」程度のソースなどもございますので参考程度にブラシアップ頂けると幸いです。
また、誤りやもっとよいコーディングやきれいな書き方があるなどご指摘頂けるととてもうれしいです。

今回のお題

良く忘れるtestコマンドの演算子を思い出しがてらにとりまとめ

演算子一覧表

数値の比較

意味 不等号 testコマンド if文で使う例
n以上 n >= y test n -ge y if [ n -ge y ]; then
n以下 n <= y test n -le y if [ n -le y ]; then
nより大きい n > y test n -gt y if [ n -gt y ]; then
nより小さい n < y test n -lt y if [ n -lt y ]; then
nと同じ n = y test n -eq y if [ n -eq y ]; then
nと異なる n <> y (n != y) test n -ne y if [ n -ne y ]; then

文字列の比較

意味 不等号 testコマンド if文で使う例
nと同じ n = y test "n" = "y" if [ "n" = "y" ]; then
nと異なる n <> y (n != y) test "n" != "y" if [ "n" != "y" ]; then

こっちは覚えやすいです。

番外編

testコマンドは、右辺と左辺の比較に対し便利に使えるコマンドですが、まだまだ他にも便利に使えます。

ディレクトリ/ファイルのチェック

ディレクトリやファイルの状態をチェックすることが出来ます。

意味 testコマンド if文で使う例
Aファイルの方が新しいか test fileA -nt fileB if [ fileA -nt fileB ]; then
Aファイルの方が古いか test fileA -ot fileB if [ fileA -ot fileB ]; then
パスがファイルか test -f path if [ -f path ]; then
パスがディレクトリか test -d path if [ -d path ]; then
Aファイルが存在するか test -e fileA if [ -e fileA ]; then
Aファイルが空じゃないか test -s fileA if [ -s fileA ]; then
Aファイルが読取可能か test -r fileA if [ -r fileA ]; then
Aファイルが書込可能か test -w fileA if [ -w fileA ]; then
Aファイルが実行可能か test -x fileA if [ -x fileA ]; then

文字列のチェック

文字列の状態をチェックすることが出来ます。

意味 testコマンド if文で使う例
文字列が空か test -z string if [ -z string ]; then
文字列が空ではないか test -n string if [ -n string ]; then

複数条件

testコマンドではAND条件やOR条件もちゃんとある

意味 testコマンド if文で使う例
条件Aかつ条件B test [条件A] -a [条件B] if test [ ... ] -a [ ... ]; then
条件Aまたは条件B test [条件A] -o [条件B] if test [ ... ] -o [ ... ]; then

※ ifで使う際、if [ [ -z AAA ] -a [ -z bbb ] ];というように略式で書くことはできなかった。
  (方法をご存知な方がいらっしゃいましたら、ぜひご教授ください)

もちろん結合演算子を使い、下記のような書き方も可能

意味 testコマンド
条件Aかつ条件B test [条件A] && test [条件B]
条件Aまたは条件B test [条件A] || test [条件B]

こちらのほうがわかりやすいかも...?

sample03.sh
#! /bin/bash

strA=あいうえお
strB=ABCDE
strC=""
strD=

#-------------------------------------------------------------------------------
# 単一条件
#-------------------------------------------------------------------------------
if [ -z $strA ]; then
    echo "strAは空です。"
else
    echo "strAは空ではありません。"
fi
## 結果:strAは空ではありません。

if test -z $strB ; then
    echo "strBは空です。"
else
    echo "strBは空ではありません。"
fi
## 結果:strBは空ではありません。

if [ -z $strC ]; then
    echo "strCは空です。"
else
    echo "strCは空ではありません。"
fi
## 結果:strCは空です。

if test -z $strD ; then
    echo "strDは空です。"
else
    echo "strDは空ではありません。"
fi
## 結果:strDは空です。

#-------------------------------------------------------------------------------
# 複数条件
#-------------------------------------------------------------------------------
# AND条件 ======================================================================
if test -z "${strA}" -a -z "${strB}" ; then
    echo "strAとstrBは両方が空です。"
else
    echo "strAとstrBは両方が空ではありません。"
fi
## 結果:strAとstrBは両方が空ではありません。

if test -z "${strA}" -a -z "${strC}" ; then
    echo "strAとstrCは両方が空です。"
else
    echo "strAとstrCは両方が空ではありません。"
fi
## 結果:strAとstrCは両方が空ではありません。

if test -z "${strC}" -a -z "${strD}" ; then
    echo "strCとstrDは両方が空です。"
else
    echo "strCとstrDは両方が空ではありません。"
fi
## 結果:strCとstrDは両方が空です。

if test -z "${strA}" && test -z "${strC}" ; then
    echo "strAとstrCは両方が空です。"
else
    echo "strAとstrCは両方が空ではありません。"
fi
## 結果:strAとstrCは両方が空ではありません。

if test -z "${strC}" && test -z "${strD}" ; then
    echo "strCとstrDは両方が空です。"
else
    echo "strCとstrDは両方が空ではありません。"
fi
## 結果:strCとstrDは両方が空です。

# OR条件 =======================================================================
if test -z "${strA}" -o -z "${strB}" ; then
    echo "strAまたはstrBは空です。"
else
    echo "strAもstrBも空ではありません。"
fi
## 結果:strAもstrBも空ではありません。

if test -z "${strA}" -o -z "${strC}" ; then
    echo "strAまたはstrCは空です。"
else
    echo "strAもstrCも空ではありません。"
fi
## 結果:strAまたはstrCは空です。

if test -z "${strC}" -o -z "${strD}" ; then
    echo "strCまたはstrDは空です。"
else
    echo "strCもstrDも空ではありません。"
fi
## 結果:strCまたはstrDは空です。

if test -z "${strA}" || test -z "${strC}" ; then
    echo "strAまたはstrCは空です。"
else
    echo "strAもstrCも空ではありません。"
fi
## 結果:strAまたはstrCは空です。

if test -z "${strC}" || test -z "${strD}" ; then
    echo "strCまたはstrDは空です。"
else
    echo "strCもstrDも空ではありません。"
fi
## 結果:strCまたはstrDは空です。
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3