linux SHELL之结构化命令
2015-12-28 09:34
489 查看
SHELL之使用结构化命令
使用if-then语句Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
if command
then
commands
fi
如果if后面的命令退出状态码=0,那么就执行then
另外一种形式
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
if command; then
commands
fi
if-then-else语句
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
if command
then
commands
else
commands
fi
如果if后面的命令退出状态码=0,那么就执行then,否则,执行else
嵌套if(else if)
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
if command1
then
commands
elif command2
then
commands
fi
test命令
test命令可以判断3类条件
1.数值比较
2.字符串比较
3.文件比较
格式:
testcondition
在if语句中,可以使用方括号[]
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
if test condition
then
commands
fi
或者
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
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 < str2 | 检查str1是否比str2小 |
str1 > 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旧 |
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
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 | 先减 |
! | 逻辑求反 |
~ | 位求反 |
** | 幂运算 |
<< | 左位移 |
>> | 右位移 |
& | 位布尔和 |
| | 位布尔或 |
&& | 逻辑和 |
|| | 逻辑或 |
[[ expression ]]
提供了模式匹配(pattern matching)特性,可以在其中使用正则表达式
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
if [ $USER == r* ]
case命令
命令格式:
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
bash中是可以在case中检查字符串的
从java到bash,语法转变还是很大的,不过思想是不会变的
下面是一个简单的示例:
Bash代码
![](http://su1216.iteye.com/images/icon_star.png)
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类型
【from:http://su1216.iteye.com/blog/1771836】
相关文章推荐
- ShellExecuteEx(&ShExecInfo) 和 WinExec
- Bash 字符串操作示例-长度,子串,查找和替换
- Linux shell 编程
- shell提取字符串指定的内容
- shell笔记(7):流程控制
- shell笔记(6):条件判断
- shell笔记(5):字符截取命令
- shell笔记(4):正则表达式
- shell笔记(3)数值计算
- shell笔记(2):变量介绍
- Shell的详细解释
- 学习shell脚本之前要掌握的知识
- centos提示bash: service: command not found
- lunix shell 基础经常使用整理
- Linux Shell 逐行读取文件 ( txt , sh , csv等)
- 把"Hello, World\n"转为机器码shellcode 来显示
- python使用os.system执行Linux shell命令
- [笔记]登录式Shell与非登录式Shell
- shell处理字符串的基本命令
- shell变量替换技术实践与生产实践