linux shell 小技能
环境:
[root@test ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@test ~]# uname -a Linux test 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
一、shell 多行注释
[root@test tmp]# cat test.sh #!/usr/bin/env bash # echo 1 echo 2 echo 3 echo 4 echo 5 echo 6 echo 7 [root@test tmp]# sh test.sh 1 2 3 4 5 6 7 [root@test tmp]# vim test.sh [root@test tmp]# cat test.sh #!/usr/bin/env bash # echo 1 echo 2 :<<! echo 3 echo 4 echo 5 echo 6 ! echo 7 [root@test tmp]# sh test.sh 1 2 7
提示:这里的叹号(!)可以换成其他任意成对的字符
二、内置的模糊匹配
注意:使用匹配的方式一定要是[[ ]]这种方式
1、正则方式匹配
[root@test ~]# [[ "$var" =~ "a|b" ]] && echo ok || echo fail fail [root@test ~]# [[ "$var" =~ a|b ]] && echo ok || echo fail ok [root@test ~]# var=1564 [root@test ~]# [[ "$var" =~ a|b ]] && echo ok || echo fail fail [root@test ~]# var=b [root@test ~]# [[ "$var" =~ a|b ]] && echo ok || echo fail ok [root@test ~]# ip=172.16.100.5 [root@test ~]# [[ "$ip" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]] && echo ok || echo fail ok [root@test ~]# [[ "$ip" =~ "^([0-9]{1,3}.){3}[0-9]{1,3}$" ]] && echo ok || echo fail fail [root@test ~]# reg='^([0-9]{1,3}.){3}[0-9]{1,3}$' [root@test ~]# [[ "$ip" =~ $reg ]] && echo ok || echo fail ok [root@test ~]# [[ "$ip" =~ "$reg" ]] && echo ok || echo fail fail
[root@test ~]# a=uuoipwsdf23423rf5
[root@test ~]# [[ $a =~ .*df.* ]] && echo ok || echo fail
ok
[root@test ~]# [[ $a =~ .*hh.* ]] && echo ok || echo fail
fail
小结:通过上面的示例,可以看出被匹配的对象不能加双引号,就算是变量也不能加。
2、使用通配模式匹配
[root@test ~]# a=bbccddee [root@test ~]# [[ $a = *e ]] && echo ok || echo fail ok [root@test ~]# [[ $a = *f ]] && echo ok || echo fail fail [root@test ~]# [[ $a = bb*ee ]] && echo ok || echo fail ok [root@test ~]# [[ $a = cc*ee ]] && echo ok || echo fail fail [root@test ~]# [[ $a = *cc*ee ]] && echo ok || echo fail ok [root@test ~]# [[ $a = *dd ]] && echo ok || echo fail fail [root@test ~]# [[ $a = *dd* ]] && echo ok || echo fail ok [root@test ~]# a=123 [root@test ~]# [[ $a = 1 ]] && echo ok || echo fail fail [root@test ~]# [[ $a = 1* ]] && echo ok || echo fail ok [root@test ~]# [[ $a = *1* ]] && echo ok || echo fail ok [root@test ~]# [[ $a = [0-9] ]] && echo ok || echo fail fail [root@test ~]# [[ $a = [0-9][0-9][0-9] ]] && echo ok || echo fail ok [root@test ~]# [[ $a = [a-z][a-z][a-z] ]] && echo ok || echo fail fail [root@test ~]# a=9 [root@test ~]# [[ $a = [a-z] ]] && echo ok || echo fail fail
应用场景:可以用作对用户从命令行传递给脚本的参数做合法验证
三、case语句模糊匹配(通配符)
[root@test tmp]# cat test.sh #!/usr/bin/env bash # test(){ case $1 in *abc*) echo yes ;; *) echo no ;; esac } #test hkfase2abcljfp test $1 [root@test tmp]# sh test.sh fasdfasdf no [root@test tmp]# sh test.sh qewfsdabchwerf yes 尝试用正则模式匹配 [root@test tmp]# cat test.sh #!/usr/bin/env bash # test(){ case $1 in ^.*abc.*$) echo yes ;; *) echo no ;; esac } #test hkfase2abcljfp test $1 [root@test tmp]# sh test.sh qewfsdabchwerf no
四、trap型号捕捉
[root@test tmp]# cat test.sh #!/usr/bin/env bash # fun_exit(){ echo -ne "\nThe program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]: " read answer case $answer in y) exit 1 ;; n) echo "program continue ..." ;; *) echo 'continue ...' ;; esac } trap "fun_exit" 1 2 3 15 while true;do echo 1 sleep 2 done [root@test tmp]# sh test.sh 1 1 1 ^C The program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]: n program continue ... 1 1 1 ^C The program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]: continue ... 1 1 ^C The program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]: y [root@test tmp]#
提示:如果连续多次按ctrl+c 还是会中断
***** 不要假装努力,结果不会陪你演戏! *****