shell命令if判断条件简单记载
2016-10-20 16:48
369 查看
将下面脚本拷贝到.sh中即可运行
注意变量
1.运用-n一元运算符的时候变量要用双引号引起来
2.[]中用大于、小于号要用转移符号[[]]和(())中可以直接使用
!
:<<!
str1 = str2 当两个串有相同内容、长度时为真
str1 != str2 当串str1和str2不等时为真
-n str1 当串的长度大于0时为真(串非空)
-z str1 当串的长度为0时为真(空串)
str1 当串str1为非空时为真
!
echo ————–字符串比较=———————
a=’sd’
b=’sd’
if [ “$a” != “$b” ];then
echo “比较两值是否相等[1]$a=$b[2]$a == $b[3]\”$a\”=\”$b\””
else
echo “不相等”
fi
:<<!
-n -z字符串为”[可有任意空格]”或不存在为真【只适用于字符串】
!
if [ $a ];then
echo “z长度为零为[ -z “$c” ],n字符串非空[ -z “$a” ],判断字符是否为null[ $a ]”
else
echo -e ‘变量有值’
fi
echo ————–字符串比较=———————
:<<!
数值的比较:
int1 -eq int2 两数相等为真
int1 -ne int2 两数不等为真
int1 -gt int2 int1大于int2为真
int1 -ge int2 int1大于等于int2为真
int1 -lt int2 int1小于int2为真
int1 -le int2 int1小于等于int2为真
!
echo -e “\n\n————–数值比较-eq———————”
ia=”3”
ib=”3”
if [ “$ia” -eq “$ib” ];then
echo “比较数值是否相等[1]$ia -eq $ib[2]\”$ia\” -eq \”$ib\”[3]\”$ia\” -eq \”3\”[4]$ia -eq 3”
else
echo “不相等”
fi
echo “————–数值比较=———————”
echo -e “\n\n————–比较符>———————”
:<<!
比较符的使用
!
gta=3
gtb=2
if [[ $gta > $gtb ]];then
echo “a大于b的比较【1】[$gta -gt $gtb]【2】[$gta \> $gtb] 【3】(($gta > $gtb))【4】[[$gta > $gtb]]”
else
echo “错误信息”
fi
echo -e “————–比较符>———————”
echo -e “\n\n————–或且非———————”
:<<!
或与切的运用:
a 与
-o 或
! 非
!
a=”a”
b=”b”
c=”c”
if [[ $a < $b && $b < $c ]];then
echo “【1】[$a lt $b -a $b lt $c ] 【2】[$a lt; $b ] && [ $b lt $c ];【3】[[$a < $b && $b < $c ]]”
else
echo “错误信息”
fi
echo -e “————–或且非———————”
echo -e “\n\n————–包含比配=~———————”
:<<!
包含匹配
!
ea=”a.txt”
eb=”txt”
if [[ $ea =~ $eb ]];then
echo “=~”
else
echo “错误的信息”
fi
echo -e “————–包含比配=~———————”
echo -e “\n\n————-文件相关判断———————”
:<<!
-r file 用户可读为真
-w file 用户可写为真
-x file 用户可执行为真
-f file 文件为正规文件为真
-d file 文件为目录为真
-c file 文件为字符特殊文件为真
-b file 文件为块特殊文件为真
-s file 文件大小非0时为真
-t file 当文件描述符(默认为1)指定的设备为终端时为真
!
if [ -d $PWD ];then
echo $PWD
else
echo “错误信息”
fi
echo -e “————-文件相关判断———————”
!/bin/bash
:<<!注意变量
1.运用-n一元运算符的时候变量要用双引号引起来
2.[]中用大于、小于号要用转移符号[[]]和(())中可以直接使用
!
:<<!
str1 = str2 当两个串有相同内容、长度时为真
str1 != str2 当串str1和str2不等时为真
-n str1 当串的长度大于0时为真(串非空)
-z str1 当串的长度为0时为真(空串)
str1 当串str1为非空时为真
!
echo ————–字符串比较=———————
a=’sd’
b=’sd’
if [ “$a” != “$b” ];then
echo “比较两值是否相等[1]$a=$b[2]$a == $b[3]\”$a\”=\”$b\””
else
echo “不相等”
fi
:<<!
-n -z字符串为”[可有任意空格]”或不存在为真【只适用于字符串】
!
if [ $a ];then
echo “z长度为零为[ -z “$c” ],n字符串非空[ -z “$a” ],判断字符是否为null[ $a ]”
else
echo -e ‘变量有值’
fi
echo ————–字符串比较=———————
:<<!
数值的比较:
int1 -eq int2 两数相等为真
int1 -ne int2 两数不等为真
int1 -gt int2 int1大于int2为真
int1 -ge int2 int1大于等于int2为真
int1 -lt int2 int1小于int2为真
int1 -le int2 int1小于等于int2为真
!
echo -e “\n\n————–数值比较-eq———————”
ia=”3”
ib=”3”
if [ “$ia” -eq “$ib” ];then
echo “比较数值是否相等[1]$ia -eq $ib[2]\”$ia\” -eq \”$ib\”[3]\”$ia\” -eq \”3\”[4]$ia -eq 3”
else
echo “不相等”
fi
echo “————–数值比较=———————”
echo -e “\n\n————–比较符>———————”
:<<!
比较符的使用
!
gta=3
gtb=2
if [[ $gta > $gtb ]];then
echo “a大于b的比较【1】[$gta -gt $gtb]【2】[$gta \> $gtb] 【3】(($gta > $gtb))【4】[[$gta > $gtb]]”
else
echo “错误信息”
fi
echo -e “————–比较符>———————”
echo -e “\n\n————–或且非———————”
:<<!
或与切的运用:
a 与
-o 或
! 非
!
a=”a”
b=”b”
c=”c”
if [[ $a < $b && $b < $c ]];then
echo “【1】[$a lt $b -a $b lt $c ] 【2】[$a lt; $b ] && [ $b lt $c ];【3】[[$a < $b && $b < $c ]]”
else
echo “错误信息”
fi
echo -e “————–或且非———————”
echo -e “\n\n————–包含比配=~———————”
:<<!
包含匹配
!
ea=”a.txt”
eb=”txt”
if [[ $ea =~ $eb ]];then
echo “=~”
else
echo “错误的信息”
fi
echo -e “————–包含比配=~———————”
echo -e “\n\n————-文件相关判断———————”
:<<!
-r file 用户可读为真
-w file 用户可写为真
-x file 用户可执行为真
-f file 文件为正规文件为真
-d file 文件为目录为真
-c file 文件为字符特殊文件为真
-b file 文件为块特殊文件为真
-s file 文件大小非0时为真
-t file 当文件描述符(默认为1)指定的设备为终端时为真
!
if [ -d $PWD ];then
echo $PWD
else
echo “错误信息”
fi
echo -e “————-文件相关判断———————”
相关文章推荐
- shell if 条件判断的命令
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
- shell基础之二:条件判断语句if、case及read命令,
- SHELL学习笔记----IF条件判断,判断条件
- 入门命令4-条件判断:if
- SHELL学习笔记----IF条件判断,判断条件
- shell中条件判断if中的-z到-d的意思
- shell中条件判断if中的-z到-d的意思
- SHELL学习笔记----IF条件判断,判断条件
- Shell 编程5(条件判断语句if)
- shell中条件判断if中的-z到-d的意思
- shell中条件判断if
- shell脚本--if判断(数字条件、字符串条件)
- shell中条件判断if中的-z到-d的意思
- 刚开始学习shell的条件判断if [] ;then 时的小小体会
- SHELL学习笔记----IF条件判断,判断条件
- shell脚本中的if判断条件 详解
- 关于shell命令中嵌入if判断
- shell中条件判断if中的-z到-d的意思
- SHELL学习笔记----IF条件判断,判断条件