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语言操作文件时候的句柄,通过句柄就可以 ...
随机推荐
- java几个常见的基础错误
1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...
- C# 服务里面调用Python.exe 来执行python文件
问题描述:在WCF服务里面通过调用python.exe来执行py文件,像下面这样py文件路径+参数,用空格隔开.会出现调用结果为空的现象 System.Diagnostics.ProcessStart ...
- gitbook 入门教程之从零到壹发布自己的插件
什么是插件 Gitbook 插件是扩展 Gitbook 功能的最佳方式,如果 Gitbook 没有想要的功能或者说网络上也没有现成的解决方案时,那么只剩下自食其力这条道路,让我们一起来自力更生开发插件 ...
- css 实现图片灰度
先看效果鼠标移入图片中摁下向左移动 图片由灰度变为原图 向右移动原图变灰度 代码如下:尚未做优化 <style> *{ margin:0; padding:0; } #img{ w ...
- Docker 为非root用户授权
Docker 为非root用户授权: 当运行docker pull busybox时候,会提示sky用户无法调用docker. 那么应该把sky用户加入docker用户组,不过在添加的时候,又提示了如 ...
- ELK 学习笔记之 Logstash基本语法
Logstash基本语法: 处理输入的input 处理过滤的filter 处理输出的output 区域 数据类型 条件判断 字段引用 区域: Logstash中,是用{}来定义区域 区域内,可以定义插 ...
- Vue三步完成跨域请求
三步完成跨域请求 ①main.js中: Vue.prototype.HOME = '/api'; ② config/index.js中: module.exports = { dev: { // Pa ...
- 记一次共享内存/dev/shm 小于memory_target 引发的客户DB 宕机问题
1> 记一次共享内存/dev/shm 小于memory_target 引发的客户DB 宕机问题(处理心得)
- day 20
目录 一.继承初体验 二.寻找继承关系 三.继承背景下对象属性查找顺序 四.派生 五.子类派生出新的属性,并重复父类的属性 六.新式类与经典类(了解) 一.继承初体验 父类: class Parent ...
- MySQL基础(四)常用函数
转载自 http://blog.csdn.net/evankaka MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数. ...