设为首页 收藏本站
查看: 771|回复: 1

shell学习之条件测试(参考shell脚本编程诀窍)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-5-25 10:01:00 | 显示全部楼层 |阅读模式
1关于test测试,查看man文档知

表达式的判断
( EXPRESSION )                    #EXPRESSION is true
! EXPRESSION                      #EXPRESSION is false
EXPRESSION1 -a EXPRESSION2        #both are true,-o means or

字符串是否为空,相等
-n STRING  #the length of STRING is nonzero,-n can be removed
-z STRING  #the length of STRING is zero(nonexists,or null)
#when comes to string, we use =

STRING1 = STRING2       #the strings are equal
STRING1 != STRING2      #the strings are not equal

数字的比较
#when comes to number,we use different eq ne and so on
INTEGER1 -eq INTEGER2   #INTEGER1 is equal to INTEGER2
        -ge  greater or equal
        -gt  greater than
    -le   -lt   -ne   I
比较文件是否为链接文件     
FILE1 -ef FILE2         #FILE1 and FILE2 have the same device and inode numbers
                eg
1
2
3
4
5
6
                [iyunv@www shell]# test  txt  -ef txthardlink            
                [iyunv@www shell]# echo $?
                0
                [iyunv@www shell]# test  txt  -ef txtlink(soft)
                [iyunv@www shell]# echo $?
                0





                所以这个是测试是否是文件链接(包括软硬),大家可以自己试试

比较文件的修改时间
FILE1 -nt FILE2                        #FILE1 is newer/older (modification date) than FILE2,timestamp
FILE1 -ot FILE2                    文件有修改时间modify time (强调内容改变) 改变时间change time(本质即相关属性改变)  接触时间access time(被访问)

文件类型
-b FILE     #type:block special
-c FILE     #type:character special
-d FILE     # a directory
-e FILE     #FILE exists是否存在
-s FILE     #FILE exists and has a size greater than zero是否为空
-f FILE     #a regular file
-u FILE     #set user-ID   SUID
-g FILE     #FILE exists and is set-group-ID使用时暂时拥有文件所属组的权限SGID
-G FILE     #FILE exists and is owned by the effective group ID真的Group
-h FILE     #a symbolic link (same as -L)
-k FILE     #FILE exists and has its sticky bit set   sticky位其他用户只能添加不能删除
-L FILE     #FILE exists and is a symbolic link (same as -h)
-O FILE     #FILE exists and is owned by the effective user ID
-p FILE     #a named pipe
-S FILE     #FILE exists and is a socket
-r FILE     #FILE exists and read permission is granted
                        you can  use -w  ,write  ,-x  execute

-t FD       #file descriptor FD is opened on a terminal
很多,但有规律,常用的并不多

2 上面的测试还可用于类似[ -e $num ]结构中

3 if/then条件测试结构(还有case,while,等结构可能后面会涉及,不懂请自行baidu)
①if  [ condition ]
then
COMMAND
fi
        or
if  [ condition ];then
COMMAND
fi
②if  [ condition ];then
COMMAND1
else
COMMAND2
fi
③if  [ condition ];then
COMMAND1
elif  [ condition ];then
COMMAND2
else
COMMAND3
fi

big eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#if1.sh
#!/bin/bash
#
read -p "guss who am i?"  HELLO
test -z $HELLO&&echo "wrong argv please reinput!"&&exit 1
if [ "$HELLO" = "Jack" ];then
echo "oh,you are jack!"
elif [ "$HELLO" = "Kitty" ];then
echo "oh,you are Kitty!"
elif [ "$HELLO" = "Mike" ];then
echo "oh,you are Mike!"
else
echo "sorry i don't remember."
echo "ok,my name is $HELLO"
fi





通常if then可以改为[[ conditon ]]&&条件ok||条件不ok
3 [[ ]]与[]的区别
通常表现在以下几个方面
[ ]使用-a,-o连接多个逻辑表达式,而[[ ]]使用&&,||连接多个表达式
[ ]:通配符不起作用,[[ ]]会展开通配符
[ ]不能使用比较运算符,如>,<,而[[ ]]可以4 (())
测试数字表达式
结果为0返回1,结果为其他返回0

[iyunv@www ~]# ((1-1))
[iyunv@www ~]# echo $?
1
[iyunv@www ~]# ((1+1))
[iyunv@www ~]# echo $?
0
5 条件判断与测试的小程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#-N可以表示文件是否被修改
#所以有下面的一段监控文件是否被修改的shell script
#watchfile.sh
#!/bin/bash

TT=5 #5 seconds
NFILE=$1 #the watched file

#origin length of the file
len=`wc -l $NFILE |awk '{print $1 }'`

echo " $NFILE has  $len lines "

while :
do
  if [ -N $NFILE ]
  then
  echo "`date`:new entry in $NFILE"
  newlen=`wc -l $NFILE |awk '{print $1 }'`
  newlines=`expr $newlen - $len`
  tail -$newlines $NFILE
  len=$newlen
  fi
  sleep $TT
done





6 正则表达式的测试程序
1
2
3
4
5
6
7
8
9
10
11
12
13
#RE.sh
#!/bin/bash

for deb in /root/shell/*
do
pkgname=`basename $deb`
if [[ $pkgname =~ .*\.deb ]];then
        #提醒 if紧跟着的[有空格,$前面有一个空格,deb后面有个空格
  echo "FILE $pkgname is a debian package"
else
  echo "File $pkgname is not a debian package"
fi
done



另一个添加捕获的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#如何捕获括号中的字符串
#保存在BASH_REMATCH[]数组中
#RE1.sh
#!/bin/bash
# manage to know your name
read -p "please input your fullname:" NAME
if [[ $NAME =~  (.*)[[:space:]](.*) ]];then
echo "你姓 ${BASH_REMATCH[2]}"
echo "你叫 ${BASH_REMATCH[1]}"
echo "全称 ${BASH_REMATCH[0]}"
fi
执行结果
[iyunv@www shell]# ./RE1.sh
please input your fullname:liancao liu
你姓 liu
你叫 liancao
全称 liancao liu



7 一个猜数字的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#一个猜数字的sh
#numguess.sh
#!/bin/bash
#you may want to guess between 0-50
WORDS=51
GUESS=-1
TIME=0
BELOW=0
TOP=`expr $WORDS - 1 `
let ANSWER=($RANDOM % $WORDS)
let ANSWER+=-1

while [ "$GUESS" -ne "$ANSWER" ]
do
    echo "the number is between $BELOW and $TOP"
        read -p "your guess:" GUESS
    let TIME+=1   
    if [ "$GUESS" -lt "$ANSWER" ] ;then
            echo "please input larger"
        BELOW=`expr $GUESS + 1`
    elif [ "$GUESS" -gt "$ANSWER" ];then
        echo "please input smaller"
        TOP=`expr $GUESS - 1 `
    else
            echo "right! you guessed $TIME times"
        fi
done



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-70389-1-1.html 上篇帖子: shell自动化之端口检测脚本 下篇帖子: SHELL中的条件测试
累计签到:102 天
连续签到:1 天
发表于 2015-6-26 13:25:42 | 显示全部楼层
不错,学习了!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表