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 还是会中断
linux shell 小技能的更多相关文章
- Linux通用小技能
Linux通用小技能 前言 无论你用ubuntu还是centos,通通没问题,运维这东西,踩坑写文档就是了. 小技能 新磁盘挂载 不管是阿里云还是腾讯云,还是自己的机器,请记住这条命令. mkfs.e ...
- Linux Shell 小脚本经典收藏
原文:http://www.cnblogs.com/Javame/p/3867686.html 1.在两个文件中找出相同的号码 diff -y xx.txt oo.txt | egrep -v &qu ...
- 普及一个Linux的小技能~Ctrl+Z切换到后台运行
逆天Linux一直是自己摸索的,几年下来也小有心得,前不久PC也换成Ubuntu了,但毕竟不是专门搞运维的,有些知识还是有死角 这不,今天发现了个小技巧,来和大家分享一下: 比如运行一个交互式的程序: ...
- Linux Shell 小知识
${} ——变量替换 通常 $var 与 ${var} 没有区别,但是用 ${} 会比较精确的界定变量名称的范围. name='Ace' echo "result1: my name is ...
- [转]Linux shell中的那些小把戏
我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧.是的,我能记住一些命令,但是肯定不会只在特定的任务上使用一次,所以我就开始在我的Dropbox账号里用文本 ...
- [转帖]拿小本本记下的Linux Shell常用技巧(一)
拿小本本记下的Linux Shell常用技巧(一) https://zhuanlan.zhihu.com/p/73361101 一. 特殊文件: /dev/null和/dev/tty Linux系统提 ...
- 机器取代人类成为现实,Linux shell才可被取代?
机器取代人类成为现实,Linux shell才可被取代? 新睿云 新睿云 新睿云-让云服务触手可及 本次笔者用通俗易懂的语言介绍一下Linux shell,由于笔者能力有限,如有有描述不准确的地方还请 ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- Linux Shell 重定向与管道【转帖】
by 程默 在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以 ...
随机推荐
- 遇到不支持的 Oracle 数据类型 USERDEFINED
以前都是sql查询mdb空间数据没有什么问题,今天在用sql方式查询Oracle中的空间数据时候,出现错误.它不支持geometry.空间数据都带有shape属性.只要不查询shape字段就没问题.但 ...
- 使用.Htaccess文件实现301重定向常用的七种方法
使用.Htaccess文件实现301重定向常用的七种方法 301重定向对广大站长来说并不陌生,从网站建设到目录优化,避免不了对网站目录进行更改,在这种情况下用户的收藏夹里面和搜索引擎里面可能保存的 ...
- 夯实Java基础系列22:一文读懂Java序列化和反序列化
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- ELK 学习笔记之 Kibana安装
Kibana安装: 安装地址: https://www.elastic.co/downloads/kibana 安装: tar -zxvf kibana-5.6.1-linux-x86_64.tar. ...
- MongoDB 学习笔记之 入门安装和配置
下载MongoDB: 下载解压即可使用. 为了启动方便和统一管理, 在Mongo根目录下建立/data, /logs, /conf文件夹. 在conf文件夹下建立mongodb.conf 文件,基本配 ...
- Butter Knife
Butter Knife,专门为Android View设计的绑定注解,专业解决各种findViewById. 简介 对一个成员变量使用@BindView注解,并传入一个View ID, Butter ...
- pycharm导入自己写的包的时候,不能识别模块的解决办法
今天用写selenium脚本的时候导入自己统计目录下的模块时,出错,明明存在但是报错说模块不存在,找了半天终于找到解决方案,顺便记录一下吧 pycharm不会将当前文件目录自动加入自己的sourse_ ...
- 浅析MVC Pattern
一.前言 最近做CAD插件相关的工作,用到了一些模式,解决对应场景的问题. 比如插件的运行实例上使用Singleton.实例内部使用了MVC(Strategy and Observer ). 针对CA ...
- CyclicBarrier 是如何做到等待多线程到达一起执行的?
我们有些场景,是需要使用 多线各一起执行某些操作的,比如进行并发测试,比如进行多线程数据汇总. 自然,我们可以使用 CountDownLatch, CyclicBarrier, 以及多个 Thread ...
- BZOJ 4597: [Shoi2016]随机序列
4597: [Shoi2016]随机序列 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 255 Solved: 174[Submit][Status ...