条件分岐でよく使われるif
についてまとめます。
基本的な文法
if [ 条件式 ]; then 処理 fi
の形式で記述します。
注意しなければいけないのは、
[
の前後にスペースを入れること]
の前にスペースを入れること- 条件式の各引数はスペースで区切ること
]
の後ろに;
を入れること
です。
例:
1 2 3 4 5 6 7 |
#!/bin/bash if [ "$1" = yes ]; then echo Yes else echo No fi |
1 2 |
$ bash if_yes.sh yes Yes |
色々な判定方法
文字列の判定
文字列の条件判定式のリストはこちらです。
演算子 | 内容 |
str1 == str2 | str1とstr2が同じ |
str1 != str2 | str1とstr2が同じではない |
-n str1 | str1が空文字ではない |
-z str2 | str2が空文字である |
str1 < str2 | str1がstr2よりも辞書的に前である |
str1 > str2 | str1がstr2よりも辞書的に後である |
if_str_eq.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ "$1" == "$2" ]; then echo "$1 equal $2"; else echo "$1 not equal $2"; fi |
1 2 |
$ bash if_str_eq.sh aaa aaa aaa equal aaa |
if_str_empty.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ -z "$1" ]; then echo "arg1 is empty" else echo "arg1 is not empty" fi |
1 2 |
$ bash if_str_empty.sh arg1 is empty |
if_str_before_after.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ "$1" '>' "$2" ]; then echo "$1 after $2" else echo "$1 before $2" fi |
>
や<
は'(シングルクオート)
で囲まなければならないので注意です。
1 2 |
bash if_str_befor_after.sh aaaa bbb aaaa before bbb |
数値の判定
演算子 | 内容 |
int1 -eq int2 | int1とint2が等しい |
int1 -lt int2 | int1がint2よりも小さい(less than) |
int1 -le int2 | int1がint2以下(less equal) |
int1 -gt int2 | int1がint2よりも大きい(greater than) |
int1 -ge int2 | int1がint2異常(greater equal) |
if_int_eq.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ "$1" -eq "$2" ]; then echo "$1 equal $2"; else echo "$1 not equal $2"; fi |
1 2 |
$ bash if_int_eq.sh 100 100 100 equal 100 |
if_int_lt.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ "$1" -lt "$2" ]; then echo "$1 less than $2" else echo "$1 greater equal $2" fi |
1 2 |
$ bash if_int_lt.sh 50 100 50 less than 100 |
if_int_gt.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ "$1" -gt "$2" ]; then echo "$1 greater than $2" else echo "$1 less equal $2" fi |
1 2 |
$ bash if_int_gt.sh 100 50 100 greater than 50 |
ファイルやディレクトリの判定
演算子 | 内容 |
-f file | fileが存在し、通常のファイルである |
-d file | fileが存在し、ディレクトリである |
file1 -nt file2 | file1がfile2よりも新しい |
file1 -ot file2 | file1がfile2よりも古い |
if_file_exist.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ -f "$1" ]; then echo "$1 is file" else touch "$1" fi |
1 2 3 |
$ bash if_file_exist.sh file1 $ bash if_file_exist.sh file1 file1 is file |
if_dir_exist.sh
1 2 3 4 5 6 7 |
#!/bin/bash if [ -d "$1" ]; then echo "$1 is directory" else mkdir "$1" fi |
1 2 3 |
$ bash if_dir_exist.sh dir1 $ bash if_dir_exist.sh dir1 dir1 is directory |
if_file_new.sh
1 2 3 4 5 |
#!/bin/bash if [ "$1" -nt "$2" ]; then echo "$1 newer than $2" fi |
1 2 3 4 5 |
$ ls -l | grep -E 'file[0-9]' -rw-r--r-- 1 root root 0 Feb 11 11:21 file1 -rw-r--r-- 1 root root 0 Feb 11 11:33 file2 $ bash if_file_new.sh file2 file1 file2 newer than file1 |
if_file_old.sh
1 2 3 4 5 |
#!/bin/bash if [ "$1" -ot "$2" ]; then echo "$1 older then $2" fi |
1 2 3 4 5 |
$ ls -l | grep -E 'file[0-9]' -rw-r--r-- 1 root root 0 Feb 11 11:21 file1 -rw-r--r-- 1 root root 0 Feb 11 11:33 file2 $ bash if_file_old.sh file1 file2 file1 older then file2 |
NOT, AND, OR条件
演算子 | 内容 |
条件式1 -a 条件式2 | 条件式1と条件式2の両方が真の場合に真(AND) |
条件式1 -o 条件式2 | 条件式1と条件式2のどちらかが真の場合に真(OR) |
! 条件式 | 条件式の真偽を逆にする(NOT) |
if_and.sh
1 2 3 4 5 |
#!/bin/bash if [ "$1" -lt "$2" -a "$2" -lt "$3" ];then echo "$1 < $2 < $3" fi |
1 2 |
$ bash if_and.sh 10 20 30 10 < 20 < 30 |
if_or.sh
1 2 3 4 5 |
#!/bin/bash if [ "$1" == "Yes" -o "$1" == "YES" ];then echo "Yes" fi |
1 2 3 |
$ bash if_or.sh yes $ bash if_or.sh Yes Yes |
if_not.sh
1 2 3 4 5 |
#!/bin/bash if [ ! "$1" == "Yes" ];then echo "Not Yes" fi |
1 2 |
bash if_not.sh No Not Yes |
まとめ
- ifの条件式の文法は間にスペースを入れることに注意する
- 文字列の一致には
==
を使い、数値の一致には-eq
を使う - ファイルが存在するか確かめる時は
-f
演算子を、ディレクトリが存在するか確かめるときは-d
演算子を使う - ANDは
条件式1 -a 条件式2
、ORは条件式1 -o 条件式2
、NOTは! 条件式
で行う