---
title: "我使用过的Linux命令之if - Bash中的条件判断语句"
author: "codingstandards"
date: "2010-10-09T20:53:32Z"
tags: ["Bash", "Blog", "Linux", "编程"]
categories: ["操作系统"]
slug: "linux-if-bash"
draft: false
lastmod: "2010-11-05T09:13:49Z"
id: 780156
url: "https://codingstandards.iteye.com/blog/780156"
---

# 我使用过的Linux命令之if - Bash中的条件判断语句

本文链接：[http://codingstandards.iteye.com/blog/780156](/blog/780156)    （转载请注明出处）

## 用途说明

Shell中的条件判断语句，与其他编程语言类似。

如果需要知道有哪些条件判断方式，通过man test就可以得到帮助。

## 常用格式

### 格式一

if 条件; then

    语句

fi

### 格式二

if 条件; then

    语句

else

    语句

fi

### 格式三

if 条件; then

    语句

elif 条件; then

    语句

fi

### 格式四

if 条件; then

    语句

elif 条件; then

    语句

else

    语句

fi

## 使用示例

### 示例一

```
if [ "foo" = "foo" ]; then
    echo expression evaluated as true
fi
```

 

\[root@jfht ~]# if \[ "foo" = "foo" ]; then  
&gt;     echo expression evaluated as true  
&gt; fi  
expression evaluated as true  
\[root@jfht ~]#

### 示例二

```
if [ "foo" = "foo" ]; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi
```

 

\[root@jfht ~]# if \[ "foo" = "foo" ]; then  
&gt;     echo expression evaluated as true  
&gt; else  
&gt;     echo expression evaluated as false  
&gt; fi  
expression evaluated as true  
\[root@jfht ~]#

### 示例三

```
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi
```

 

\[root@jfht ~]# T1="foo"  
\[root@jfht ~]# T2="bar"  
\[root@jfht ~]# if \[ "$T1" = "$T2" ]; then  
&gt;     echo expression evaluated as true  
&gt; else  
&gt;     echo expression evaluated as false  
&gt; fi  
expression evaluated as false  
\[root@jfht ~]#

### 示例四 判断命令行参数数量

文件 if\_4.sh

```
#!/bin/sh

if [ "$#" != "1" ]; then
    echo "usage: $0 <file>"
    exit 1
fi
```

 

\[root@smsgw root]# cat if\_4.sh  
#!/bin/sh

if \[ "$#" != "1" ]; then  
    echo "usage: $0 &lt;file&gt;"  
    exit 1  
fi

\[root@smsgw root]# chmod +x if\_4.sh  
\[root@smsgw root]# ./if\_4.sh  
usage: ./if\_4.sh &lt;file&gt;  
\[root@smsgw root]# ./if\_4.sh hello  
\[root@smsgw root]#

 

### 示例五 判断文件中是否包含某个字符串

```
if grep -q root /etc/passwd; then
    echo account root exists
else
    echo account root not exist
fi
```

 

\[root@jfht ~]# if grep -q root /etc/passwd; then  
&gt;     echo account root exists  
&gt; else  
&gt;     echo account root not exist  
&gt; fi  
account root exists  
\[root@jfht ~]#

 

### 示例六 判断文件是否存在

```
if [ -e myfile ]; then
    echo myfile exists
else
    touch myfile
    echo myfile created
fi
```

 

\[root@jfht ~]# if \[ -e myfile ]; then  
&gt;     echo myfile exists  
&gt; else  
&gt;     touch myfile  
&gt;     echo myfile created  
&gt; fi  
myfile created  
\[root@jfht ~]# if \[ -e myfile ]; then  
&gt;     echo myfile exists  
&gt; else  
&gt;     touch myfile  
&gt;     echo myfile created  
&gt; fi  
myfile exists  
\[root@jfht ~]# ls -l myfile  
\-rw-r--r-- 1 root root 0 10-09 20:44 myfile

 

### 示例七 判断两个文件是否相同

```
echo 1 >file1
echo 2 >file2
if ! diff -q file1 file2; then
    echo file1 file2 diff
else
    echo file1 file2 same
fi 
```

 

\[root@jfht ~]# echo 1 &gt;file1  
\[root@jfht ~]# echo 2 &gt;file2  
\[root@jfht ~]# if ! diff -q file1 file2; then  
&gt;     echo file1 file2 diff  
&gt; else  
&gt;     echo file1 file2 same  
&gt; fi  
Files file1 and file2 differ  
file1 file2 diff  
\[root@jfht ~]#

## 问题思考

1\. 怎么判断字符串非空？

2\. 怎么判断文件非空？

3\. 怎么判断文件可执行？

4\. 怎么判断目录？

5\. 怎么判断数值大小判断？

## 相关资料

【1】BASH Programming [6.1 Dry Theory](http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html#ss6.1)

【2】刘 泰山的博客 [bash if 条件判断](http://blog.myspace.cn/e/405974342.htm)

 

返回 [我使用过的Linux命令系列总目录](/blog/786653)