本周主要讲了三个知识点:
一.文本处理工具 二.shell脚本编程基础 三.文本查找和压缩
扩展正则表达式 . 一个任意字符 * 例:a(字符)* a字符重复无限(任意)次;表示单词出现无数次 (wang)* 分组,扩展正则表达式不用+/
【】例:【abcd】其中任何一个 若表示范围【a-z 0-9】任意一个
【^123】排除 【^a-z.】除了a-z和. 其他的字符 表示一个字符
【【:alnum:】】表示一个数字和字母
a{10} a重复10次 (wang){10} 10个wang {10,20} 表示范围
a? a可能有一个(包括n个)或者没有 a+ = a{1,} 一个以上(至少一个)
位置锚定 ^行首 $行尾 \< \> 词首词尾 \b 既可代表行首,也可代表行尾
(xyz)(abc)(123) 分组 搜索替代 s/(xyz)(abc)(123)/\2\3/ 把分组1删了
grep vims sed awk nginx 等都支持表达式(包括windows)findstr dir=ls
vim 刚进 命令模式 主要功能:查看文件内容 也可删除dd(整行)dw(一个单词)、复制(yy 一行)粘贴(p)等
插入模式 改文件内容 切换方式:Aa、Oo、iI、c(删除整行并切换) 按esc回命令模式
扩展的执行模式 ex模式 切换方式: wq、q、q!(不存盘退出)、r后跟file 把文件内容读入、w file2 另存 !cmd 执行命令 r!cmd 把标准输出读入当前文件
命令模式下 u 撤销 u/U功能不同 ctrl+r 重做撤销 .重复一次 n.重复n次
“ayy 放在寄存器a里 "ap 粘贴 26个命名寄存器 直接yy 未指定寄存器 10个数字寄存器
echo -e "\e[31mred\e[0m" 打印红色文本 加深:在31前+1;
%s /UUID/xxx/ 搜索替代 大小写敏感 ignore case 忽略大小写
auto indent 自动缩进
cat /etc/shells #! /bin/bash shell
脚本的用途有:1.自动化常用命令2.执行系统管理和故障排除3.创建简单的应用程序4.处理文本或文件
运行脚本:1.bash hello.sh2. source hello.sh 或 . hello.sh
可以加上执行权限 chmod a+x hello.sh hello.sh直接执行不可以 路径不在path变量里 mkdir /root/bin cp hello.sh /root/bin/ 然后可以执行 hello.sh 或者直接写绝对路径:/data/scripts/hello.sh 相对路径:./hello.sh
例:vim systeminfo.sh 显示当前系统信息
echo "OS version is:`cat /etc/centos-release`" 操作系统版本
echo "kernel version is `uname -r`" 内核版本
echo "CPU type:`lscpu | grep "Model name"|tr -s " "|cut -d: -f2`" cpu型号
cat /proc/meminfo | head -n1 内存大小
echo "Disk:`lsblk | grep '^sd' | tr -s " " | cut -d " " -f1,4`"硬盘大小
ifconfig eth0 | grep -w 'inet' | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -n1
ifconfig eth0 | grep -Eo 'inet([0-9]{1,3}\.){3}[0-9]{1,3}' | cut -d "" -f2
echo "My username is $USER" 用户名
echo "My hostname is `hostname`" 主机名
保存 chmod+x systeminfo.sh 查看pwd,直接执行
加颜色is:^[[1;31m`cat...`^[[0m
!# /bin/bash
#date 2018-08-01
groupadd -g 80 apache
useradd -u 80 -g apache -s /sbin/nologin -d /var/www apache
echo "User apache is created"
mkdir /var/www
chown apache:apache /var/www
echo"/var/www is ready!"
chomd +x * 后执行apache.sh id apache ll -d ... userdel -r apache
echo 'alias name=hostname' >> /root/.bashrc^c 重定向
PS1 /etc/profile.d/env.sh 攒一个逐渐丰富的脚本 reset.sh 新系统配环境
name=`hostname` echo "my name is $name"
name=`who` echo "my name is $name" 多行 echo $name 单行
pstree -p 进程树 echo $$ 当前进程进程编号 父进程;子进程
变量有效范围:当前进程 不能传给子进程,也不能传给父进程 局部(普通)变量
环境变量 export title='ceo' echo $title bash echo $title 可传