---
title: "《Linux命令行与shell脚本编程大全》 第十一章"
author: "su1216"
date: "2013-01-17T23:22:29Z"
tags: ["elif", "if"]
categories: ["操作系统"]
slug: "linux-shell"
draft: false
lastmod: "2014-01-01T13:52:12Z"
id: 1771836
url: "https://su1216.iteye.com/blog/1771836"
---

第一部分：Linux命令行  
[《Linux命令行与shell脚本编程大全》 第一章：初识Linux shell](/blog/1733601)  
[《Linux命令行与shell脚本编程大全》 第二章：走进shell](/blog/1733601)  
[《Linux命令行与shell脚本编程大全》 第三章：基本的bash shell命令](/blog/1735297)  
[《Linux命令行与shell脚本编程大全》 第四章：更多的bash shell命令](/blog/1739876)  
[《Linux命令行与shell脚本编程大全》 第五章：使用Linux环境变量](/blog/1745521)  
[《Linux命令行与shell脚本编程大全》 第六章：理解Linux文件权限](/blog/1748756)  
[《Linux命令行与shell脚本编程大全》 第七章：管理文件系统](/blog/1757290)  
[《Linux命令行与shell脚本编程大全》 第八章：安装软件程序](/blog/1757505)  
[《Linux命令行与shell脚本编程大全》 第九章：使用编辑器](/blog/1758137)

第二部分：shell脚本编程基础  
[《Linux命令行与shell脚本编程大全》 第十章：构建基本脚本](/blog/1766729)  
[《Linux命令行与shell脚本编程大全》 第十一章：使用结构化命令](/blog/1771836)  
[《Linux命令行与shell脚本编程大全》 第十二章：更多的结构化命令](/blog/1797844)  
[《Linux命令行与shell脚本编程大全》 第十三章：处理用户输入](/blog/1926382)  
[《Linux命令行与shell脚本编程大全》 第十四章：呈现数据](/blog/1928663)  
[《Linux命令行与shell脚本编程大全》 第十五章：控制脚本](/blog/1933910)

第三部分：高级shell编程  
[《Linux命令行与shell脚本编程大全》 第十六章：创建函数](/blog/1935450)  
[《Linux命令行与shell脚本编程大全》 第十七章：图形化桌面上的脚本编程](/blog/1937078)  
[《Linux命令行与shell脚本编程大全》 第十八章：初识sed和gawk](/blog/1940011)  
[《Linux命令行与shell脚本编程大全》 第十九章：正则表达式](/blog/1940429)  
[《Linux命令行与shell脚本编程大全》 第二十章：sed进阶](/blog/1946721)  
[《Linux命令行与shell脚本编程大全》 第二十一章：gawk进阶](/blog/1958425)  
[《Linux命令行与shell脚本编程大全》 第二十二章：使用其他shell](/blog/1966679)

第四部分：高级shell脚本编程主题  
[《Linux命令行与shell脚本编程大全》 第二十三章：使用数据库](/blog/1967736)  
[《Linux命令行与shell脚本编程大全》 第二十四章：使用Web](/blog/1975505)  
[《Linux命令行与shell脚本编程大全》 第二十五章：使用E-mail](/blog/1986145)  
[《Linux命令行与shell脚本编程大全》 第二十六章：编写脚本实用工具](/blog/1988177)  
[《Linux命令行与shell脚本编程大全》 第二十七章：shell脚本编程进阶](/blog/1988409)

 

**第十一章：使用结构化命令**

使用if-then语句

```
if command
then
    commands
fi
```

**如果if后面的命令退出状态码=0，那么就执行then**

另外一种形式

```
if command; then
    commands
fi
```

 

if-then-else语句

```
if command
then
    commands
else
    commands
fi
```

**如果if后面的命令退出状态码=0，那么就执行then,否则，执行else**

 

**嵌套if（else if）**

```
if command1
then
    commands
elif command2
then
    commands
fi
```

 

**test命令**

**test命令可以判断3类条件**

**1.数值比较**

**2.字符串比较**

**3.文件比较**

**格式：**

**test** *condition*

在if语句中，可以使用方括号\[]

```
if test condition
then
    commands
fi
```

或者

```
if [ command ]
then
    commands
fi
```

**注意：方括号两边必须加入一个空格，不然会报错**

 

**数值比较**

**比较** **描述** n1 -eq n2

检查n1是否与n2相等

n1 -ge n2 检查n1是否大于或等于n2 n1 -gt n2 检查n1是否大于n2 n1 -le n2 检查n1是否下雨或等于n2 n1 -lt n2 检查n1是否小于n2 n1 -ne n2 检查n1是否不等于n2

**可以用在变量上**

**注意：不能使用浮点数**

 

**字符串比较**

比较 描述 str1 = str2 检查str1是否和str2相同 str1 != str2 检查str1是否和str2不同 str1 &lt; str2 检查str1是否比str2小 str1 &gt; str2 检查str1是否比str2大 -n str1 检查str1的长度是否非0 -z str1 检查str1的长度是否为0

 

**顺序比较需要注意：**

**1.大于小于符号要转义，不然会被识别为重定向符号**                                                                                              

**2.test命令根据ASCII顺序排序，与sort命令不同**

 

 **字符串长度**

**当变量为空字符串或者没有初始化时，变成长度是0**

**空字符串或没有初始化的变量可能会对程序有灾难性影响，使用前最好先使用test命令中的-n和-z确认**

 

**文件比较**

**比较** 描述 -d file 检查file是否存在并且是否是一个目录 -e file 检查file是否存在 -f file 检查file是否存在并且是否是一个文件 -r file 检查file是否存在并可读 -s 检查file是否存在并非空 -w 检查file是否存在并可写 -x 检查file是否存在并可执行 -O 检查file是否存在并属于当前用户所有 -G 检查file是否存在并且默认组与当前用户相同

file1 -nt file2

检查file1是否比file2新 file1 -ot file2 检查file1是否比file2旧

**以检查目录为例：**

```
if [ -d $HOME ]
then
    cd $HOME
else
    echo "$HOME is not a directory"
fi
```

 

**复合条件测试**

**\[ condition1 ] || \[ condition2 ]**

**\[ condition1 ] && \[ condition2 ]**

 

**if-then的高级特性**

**1.用于数学表达式的双圆括号**

**2.用于高级字符串处理的双方括号**

**双圆括号允许将高级数学表达式放入比较中**

**格式：**

**(( expression ))**

**命令符号**

**符号** **描述** val++ 后增 val-- 后减 ++val 先增 --val 先减 ! 逻辑求反 ~ 位求反 \** 幂运算 &lt;&lt; 左位移 &gt;&gt; 右位移 & 位布尔和 | 位布尔或 && 逻辑和 || 逻辑或

 

**双方括号格式**

**\[\[ expression ]]**

**提供了模式匹配（pattern matching）特性，可以在其中使用正则表达式**

```
if [ $USER == r* ]
```

 

**case命令**

命令格式：

```
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
```

**bash中是可以在case中检查字符串的**

从java到bash，语法转变还是很大的，不过思想是不会变的

下面是一个简单的示例：

```
case $test in
pork | beef | lamb )
    echo "$test is meat!"
    echo "I like it!";;
potato | tomato | eggplant ）
    echo "$test is a vegetable"
    echo "who likes it?";;
*)
    echo "what's this?";;
esac
```

在java7之前，这么写是不可以的，switch case中不支持string类型

 

 

**转贴请保留以下链接**

本人blog地址

[http://su1216.iteye.com/](/)

[http://blog.csdn.net/su1216/](http://blog.csdn.net/su1216/)