shell 实练(5)if 判断
来源:互联网 发布:遥感大数据的定义 编辑:程序博客网 时间:2024/06/06 04:55
练习的小内容:
判断当前文件夹下是否存在a.txt文件,若存在则新建a-1.txt文件;否则创建t.txt文件;
if 的三种条件表达式
1、
if 条件一;then
command1
else
command2
fi
code:
1 #!/bin/bash
2
3 if ls|grep a.txt; then
4 touch a-1.txt
5 else
6 touch t.txt
7 fi
Loong:/home/yee/if# ls
if-1.sh if-2.sh
Loong:/home/yee/if# sh -x if-1.sh
+ ls
+ grep a.txt
+ touch t.txt
Loong:/home/yee/if# ls
if-1.sh if-2.sh t.txt
Loong:/home/yee/if# touch a.txt
Loong:/home/yee/if# ls
a.txt if-1.sh if-2.sh t.txt
Loong:/home/yee/if# sh -x if-1.sh
+ ls
+ grep a.txt
a.txt
+ touch a-1.txt
Loong:/home/yee/if#
2、
if [表达式];then if与【】间要留空格,否则报错;
command1
fi 分号 ;不能少,否则报错:syntax error: unexpected end of file ;不要分号则then 换行写;
更复杂的情况,则可以使用这个语法:
if [ 条件判断式一 ]; then
当条件判断式一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件判断式二成立时,可以进行的指令工作内容;
else
当条件判断式一与二均不成立时,可以进行的指令工作内容;
fi
文件表达式
条件表达式
if [ -f
if [ -d...
if [ -s file
if [ -r file ] 如果文件存在且可读
if [ -x file
if [ -e dir||file] 如果指定的文件或者目录存在返回真
[ -z STRING ] 如果STRING的长度为零则为真
[ -n STRING ] 如果STRING的长度非零则为真
[ STRING1 = STRING2 ] 如果两个字符串相同则为真
[ STRING1 != STRING2 ] 如果字符串不相同则为真
整数变量表达式
if [ int1 -eq int2]
if [ int1 -ge int2]
if [ int1 -gt int2]
if [ int1 -le int2]
if [ int1 -lt int2]
字符串变量表达式
If
if
if
if
if
if [ ! 表达式 ]
if [ ! -d $num] 如果不存在目录$num
if [ 表达式1
if [ 表达式1
-
表达式与前面的= != -d –f –x -ne -eq -lt等合用
-
逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列
code:
1 #!/bin/bash
2
3 filename=/home/yee/if/a.txt
4 if [ -f $filename ]; then
5 touch a-1.txt
6 fi
7 if [ ! -e $filename ]; then
8 touch t.txt
9 fi
Loong:/home/yee/if# ll
总计 12
-rw-r--r-- 1 root root 75 11-07 11:42 if-1.sh
-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
-rw-r--r-- 1 root root 38 11-07 14:21 if-3.sh
Loong:/home/yee/if# sh -x if-2.sh
+ filename=/home/yee/if/a.txt
+ '[' -f /home/yee/if/a.txt ']'
+ '[' '!' -e /home/yee/if/a.txt ']'
+ touch t.txt
Loong:/home/yee/if# ll
总计 12
-rw-r--r-- 1 root root 75 11-07 11:42 if-1.sh
-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
-rw-r--r-- 1 root root 38 11-07 14:21 if-3.sh
-rw-r--r-- 1 root root 0 11-07 16:11 t.txt
Loong:/home/yee/if#
3、
if test 表达式 ;then
command1
fi
例:
if test $num -eq0
1 #!/bin/bash 2 3 filename=/home/yee/if/a.txt 4 if test -f $filename 5 then 6 touch a-1.txt 7 else 8 touch t.txt 9 fi
Loong:/home/yee/if# ll总计 12-rw-r--r-- 1 root root 75 11-07 11:42 if-1.sh-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh-rw-r--r-- 1 root root 38 11-07 14:21 if-3.sh-rw-r--r-- 1 root root 0 11-07 16:11 t.txtLoong:/home/yee/if# sh -x if-3.sh + filename=/home/yee/if/a.txt+ test -f /home/yee/if/a.txt+ touch t.txtLoong:/home/yee/if# ll总计 12-rw-r--r-- 1 root root 75 11-07 11:42 if-1.sh-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh-rw-r--r-- 1 root root 103 11-07 16:20 if-3.sh-rw-r--r-- 1 root root 0 11-07 16:20 t.txtLoong:/home/yee/if# touch a.txtLoong:/home/yee/if# sh -x if-3.sh + filename=/home/yee/if/a.txt+ test -f /home/yee/if/a.txt+ touch a-1.txtLoong:/home/yee/if# ll总计 12-rw-r--r-- 1 root root 0 11-07 16:21 a-1.txt-rw-r--r-- 1 root root 0 11-07 16:21 a.txt-rw-r--r-- 1 root root 75 11-07 11:42 if-1.sh-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh-rw-r--r-- 1 root root 103 11-07 16:20 if-3.sh-rw-r--r-- 1 root root 0 11-07 16:20 t.txtLoong:/home/yee/if#
echo -e参数使输出中的反斜线(\)的说明起作用
echo -n参数使引号后的内容接着输出(不换行)
- shell 实练(5)if 判断
- Shell 编程5(条件判断语句if)
- shell 中的条件判断(if else)
- shell if 判断语句
- Shell IF判断语句
- shell if判断
- shell中的if判断
- shell中的if判断
- shell的if判断
- [shell] if判断条件
- Shell中if判断
- shell中的if判断
- SHELL----IF条件判断
- shell if 判断语句
- shell if判断
- Shell IF条件判断
- shell if =判断
- shell if 判断
- ASP.NET 三层架构技术 人力资源管理系统项目HR (深入WebServic
- android imageButton 点击按钮前中后,按钮颜色的变化
- 可怕的SCN!
- ExcelVBA:利用SUMIF和OFFSET+MATCH进行多条件求和
- servlet基础
- shell 实练(5)if 判断
- javascript in语法
- AJAX博客项目
- DotNet学习目录
- ASP.NET 权限管理 页面静态化 OA系统培训 三层架构
- Mac OS X快捷键
- 如何Enable/Disable菜单项
- 听《设计模式》培训课程有感
- c# 遍历枚举(转自博客园)