
正文
linux shell 小技能
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
环境:
[root@test ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@test ~]# uname -a
Linux test 2.6.-.el6.x86_64 # SMP Fri Nov :: UTC x86_64 x86_64 x86_64 GNU/Linux
一、shell 多行注释
[root@test tmp]# cat test.sh
#!/usr/bin/env bash
#echo
echo
echo
echo
echo
echo
echo
[root@test tmp]# sh test.sh [root@test tmp]# vim test.sh
[root@test tmp]# cat test.sh
#!/usr/bin/env bash
#echo
echo
:<<!
echo
echo
echo
echo
!
echo
[root@test tmp]# sh test.sh
提示:这里的叹号(!)可以换成其他任意成对的字符
二、内置的模糊匹配
注意:使用匹配的方式一定要是[[ ]]这种方式
1、正则方式匹配
[root@test ~]# [[ "$var" =~ "a|b" ]] && echo ok || echo fail
fail
[root@test ~]# [[ "$var" =~ a|b ]] && echo ok || echo fail
ok
[root@test ~]# var=
[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" =~ ^([-]{,}.){}[-]{,}$ ]] && 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=
[root@test ~]# [[ $a = ]] && echo ok || echo fail
fail
[root@test ~]# [[ $a = * ]] && echo ok || echo fail
ok
[root@test ~]# [[ $a = ** ]] && echo ok || echo fail
ok
[root@test ~]# [[ $a = [-] ]] && echo ok || echo fail
fail
[root@test ~]# [[ $a = [-][-][-] ]] && echo ok || echo fail
ok
[root@test ~]# [[ $a = [a-z][a-z][a-z] ]] && echo ok || echo fail
fail
[root@test ~]# a=
[root@test ~]# [[ $a = [a-z] ]] && echo ok || echo fail
fail
应用场景:可以用作对用户从命令行传递给脚本的参数做合法验证
三、case语句模糊匹配(通配符)
[root@test tmp]# cat test.sh
#!/usr/bin/env bash
#
test(){
case $ in
*abc*)
echo yes
;;
*)
echo no
;;
esac
}#test hkfase2abcljfp
test $[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 $ in
^.*abc.*$)
echo yes
;;
*)
echo no
;;
esac
}#test hkfase2abcljfp
test $[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
;;
n)
echo "program continue ..."
;;
*)
echo 'continue ...'
;;
esac
}trap "fun_exit" while true;do
echo
sleep
done
[root@test tmp]# sh test.sh ^C
The program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]: n
program continue ...^C
The program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]:
continue ...^C
The program receives an interrupt signal,Do you wish to really exit? Your choice [ y | n ]: y
[root@test tmp]#
提示:如果连续多次按ctrl+c 还是会中断





