From 6234f76c102d63e50a22c7b7b023b3c2b9538c46 Mon Sep 17 00:00:00 2001 From: diandian Date: Tue, 11 Apr 2023 00:13:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'Shell-MD'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shell-MD/shell三剑客.md | 106 ++++++++++++++++++++++++++++++++++ Shell-MD/shell脚本案例.md | 50 ++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 Shell-MD/shell脚本案例.md diff --git a/Shell-MD/shell三剑客.md b/Shell-MD/shell三剑客.md index 9f214d9..208f735 100644 --- a/Shell-MD/shell三剑客.md +++ b/Shell-MD/shell三剑客.md @@ -509,7 +509,113 @@ root ​ egrep: 扩展的egrep,支持更多的正则表达式元字符 +​ fgrep: 固定grep(fixed grep),有时也被称作快速(fast grep),它按字面解释所有的字符(了解) +#### 2.语法格式 + +```shell +grep [选项] PATTERN filename filename ... +``` + +案例: + +```shell +[root@xingdiancloud ~]# grep 'Tom' /etc/passwd +[root@xingdiancloud ~]# grep 'bash shell' /etc/test +``` + +注意: + +​ 找到: grep返回的退出状态为0 +​ 没找到: grep返回的退出状态为1 +​ 找不到指定文件: grep返回的退出状态为2 + +来自标准输入或管道: + +```shell +[root@xingdiancloud ~]# ps aux |grep 'sshd' +[root@xingdiancloud ~]# ll |grep '^d' +[root@xingdiancloud ~]# grep 'alice' /etc/passwd /etc/shadow /etc/group +``` + +#### 2.使用元字符 + +```shell +grep: 使用基本元字符集 ^, $, ., *, [], [^], \< \>,\(\),\{\}, \+, \| +egrep(或grep -E): 使用扩展元字符集 ?, +, { }, |, ( ) +注:grep也可以使用扩展集中的元字符,仅需要对这些元字符前置一个反斜线 + +\w 所有字母与数字,称为字符[a-zA-Z0-9] 'l[a-zA-Z0-9]*ve' 'l\w*ve' +\W 所有字母与数字之外的字符,称为非字符 'love[^a-zA-Z0-9]+' 'love\W+' +\b 词边界 '\' '\blove\b' +``` + +#### 3.grep示例 + +```shell +[root@xingdiancloud ~]# egrep 'N\W' datafile +[root@xingdiancloud ~]# egrep '^n' datafile +[root@xingdiancloud ~]# egrep '4$' datafile +[root@xingdiancloud ~]# egrep '5\..' datafile +[root@xingdiancloud ~]# egrep '\.5' datafile +[root@xingdiancloud ~]# egrep '^[we]' datafile +[root@xingdiancloud ~]# egrep '[^0-9]' datafile +[root@xingdiancloud ~]# egrep '[A-Z][A-Z] [A-Z]' datafile +[root@xingdiancloud ~]# egrep 'ss* ' datafile +[root@xingdiancloud ~]# egrep '[a-z]{9}' datafile +[root@xingdiancloud ~]# egrep '\' datafile +[root@xingdiancloud ~]# egrep '\<[a-r].*n\>' datafile +[root@xingdiancloud ~]# egrep '^n\w*\W' datafile +[root@xingdiancloud ~]# egrep '\bnorth\b' datafile +[root@xingdiancloud ~]# egrep '3+' datafile +[root@xingdiancloud ~]# egrep '2\.?[0-9]' datafile +[root@xingdiancloud ~]# egrep '(no)+' datafile +[root@xingdiancloud ~]# egrep 'S(h|u)' datafile +``` + +#### 4.grep参数 + +```shell +-i, --ignore-case 忽略大小写 +-l, --files-with-matches 只列出匹配行所在的文件名 +-n, --line-number 在每一行前面加上它在文件中的相对行号 +-c, --count 显示成功匹配的行数 +-s, --no-messages 禁止显示文件不存在或文件不可读的错误信息 +-q, --quiet, --silent 静默--quiet, --silent +-v, --invert-match 反向查找,只显示不匹配的行 +-R, -r, --recursive 递归针对目录 +--color 颜色 +-o, --only-matching 只显示匹配的内容 +``` + +示例: + +```shell +[root@xingdian ~]# grep -R 'ifcfg' /etc 目录 + +[root@xingdian ~]# egrep 'root' /etc/passwd /etc/shadow /etc/hosts +/etc/passwd:root:x:0:0:root:/root:/bin/bash +/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin +/etc/shadow:root:$6$gcO6Vp4t$OX9LmVgpjtur67UQdUYfw7vJW.78.uRXCLIxw4mBk82Z99:7::: + +[root@xingdian ~]# egrep -l 'root' /etc/passwd /etc/shadow /etc/hosts +/etc/passwd +/etc/shadow + +[root@xingdian ~]# egrep -n 'root' /etc/passwd /etc/shadow /etc/hosts +/etc/passwd:1:root:x:0:0:root:/root:/bin/bash +/etc/passwd:11:operator:x:11:0:operator:/root:/sbin/nologin +/etc/shadow:1:root:$6$gcO6Vp4t$OX9LmVgpjtur67UQdUy8.M78.uRXCLIxw4mBk82ZrNlxyf54 + + +[root@xingdian ~]# egrep -R '54:04:A6:CE:C2:1F' /etc/sysconfig/ + +[root@xingdian ~]# egrep '^IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 |egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' +192.168.2.254 +[root@xingdian ~]# egrep '^IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' +192.168.2.254 +``` diff --git a/Shell-MD/shell脚本案例.md b/Shell-MD/shell脚本案例.md new file mode 100644 index 0000000..d3bf12d --- /dev/null +++ b/Shell-MD/shell脚本案例.md @@ -0,0 +1,50 @@ +

shell脚本案例

+ +作者:行癫(盗版必究) + +------ + +## 一:脚本案例 + +#### 1.配置静态IP案例 + +```shell +#!/bin/bash +# This script configures a static IP address on CentOS 7 + +# Define variables for the IP address, netmask, gateway, and DNS servers +IP_ADDRESS=192.168.1.100 +NETMASK=255.255.255.0 +GATEWAY=192.168.1.1 +DNS_SERVERS="8.8.8.8 114.114.114.114" + +# Backup the original network configuration file +cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak + +# Modify the network configuration file with the static IP address, netmask, gateway, and DNS servers +cat << EOF > /etc/sysconfig/network-scripts/ifcfg-ens33 +TYPE=Ethernet +BOOTPROTO=none +NAME=ens33 +DEVICE=ens33 +ONBOOT=yes +IPADDR=$IP_ADDRESS +NETMASK=$NETMASK +GATEWAY=$GATEWAY +DNS1=${DNS_SERVERS%% *} +DNS2=${DNS_SERVERS##* } +EOF + +# Restart the network service to apply the changes +systemctl restart network + +# Display the new network configuration +ip addr show ens33 +``` + +centos stream 9 + +``` + +``` +