├── Makefile ├── README.md ├── build ├── doctrees │ ├── environment.pickle │ ├── index.doctree │ ├── myshell-note.doctree │ ├── shell-01-基础知识.doctree │ ├── shell-02-字符串与数组.doctree │ ├── shell-03-运算符.doctree │ ├── shell-04-流程控制.doctree │ ├── shell-05-函数.doctree │ ├── shell-06-正则表达式.doctree │ ├── shell-07-三剑客-grep.doctree │ ├── shell-08-三剑客-sed.doctree │ ├── shell-09-三剑客-awk.doctree │ └── shell-10-杂项.doctree └── html │ ├── .buildinfo │ ├── _sources │ ├── index.rst.txt │ ├── myshell-note.rst.txt │ ├── shell-01-基础知识.rst.txt │ ├── shell-02-字符串与数组.rst.txt │ ├── shell-03-运算符.rst.txt │ ├── shell-04-流程控制.rst.txt │ ├── shell-05-函数.rst.txt │ ├── shell-06-正则表达式.rst.txt │ ├── shell-07-三剑客-grep.rst.txt │ ├── shell-08-三剑客-sed.rst.txt │ ├── shell-09-三剑客-awk.rst.txt │ └── shell-10-杂项.rst.txt │ ├── _static │ ├── ajax-loader.gif │ ├── basic.css │ ├── comment-bright.png │ ├── comment-close.png │ ├── comment.png │ ├── css │ │ ├── badge_only.css │ │ └── theme.css │ ├── doctools.js │ ├── documentation_options.js │ ├── down-pressed.png │ ├── down.png │ ├── file.png │ ├── fonts │ │ ├── Inconsolata-Bold.ttf │ │ ├── Inconsolata-Regular.ttf │ │ ├── Inconsolata.ttf │ │ ├── Lato-Bold.ttf │ │ ├── Lato-Regular.ttf │ │ ├── Lato │ │ │ ├── lato-bold.eot │ │ │ ├── lato-bold.ttf │ │ │ ├── lato-bold.woff │ │ │ ├── lato-bold.woff2 │ │ │ ├── lato-bolditalic.eot │ │ │ ├── lato-bolditalic.ttf │ │ │ ├── lato-bolditalic.woff │ │ │ ├── lato-bolditalic.woff2 │ │ │ ├── lato-italic.eot │ │ │ ├── lato-italic.ttf │ │ │ ├── lato-italic.woff │ │ │ ├── lato-italic.woff2 │ │ │ ├── lato-regular.eot │ │ │ ├── lato-regular.ttf │ │ │ ├── lato-regular.woff │ │ │ └── lato-regular.woff2 │ │ ├── RobotoSlab-Bold.ttf │ │ ├── RobotoSlab-Regular.ttf │ │ ├── RobotoSlab │ │ │ ├── roboto-slab-v7-bold.eot │ │ │ ├── roboto-slab-v7-bold.ttf │ │ │ ├── roboto-slab-v7-bold.woff │ │ │ ├── roboto-slab-v7-bold.woff2 │ │ │ ├── roboto-slab-v7-regular.eot │ │ │ ├── roboto-slab-v7-regular.ttf │ │ │ ├── roboto-slab-v7-regular.woff │ │ │ └── roboto-slab-v7-regular.woff2 │ │ ├── fontawesome-webfont.eot │ │ ├── fontawesome-webfont.svg │ │ ├── fontawesome-webfont.ttf │ │ ├── fontawesome-webfont.woff │ │ └── fontawesome-webfont.woff2 │ ├── jquery-3.2.1.js │ ├── jquery.js │ ├── js │ │ ├── modernizr.min.js │ │ └── theme.js │ ├── minus.png │ ├── plus.png │ ├── pygments.css │ ├── searchtools.js │ ├── underscore-1.3.1.js │ ├── underscore.js │ ├── up-pressed.png │ ├── up.png │ └── websupport.js │ ├── genindex.html │ ├── index.html │ ├── myshell-note.html │ ├── objects.inv │ ├── search.html │ ├── searchindex.js │ ├── shell-01-基础知识.html │ ├── shell-02-字符串与数组.html │ ├── shell-03-运算符.html │ ├── shell-04-流程控制.html │ ├── shell-05-函数.html │ ├── shell-06-正则表达式.html │ ├── shell-07-三剑客-grep.html │ ├── shell-08-三剑客-sed.html │ ├── shell-09-三剑客-awk.html │ └── shell-10-杂项.html ├── exts ├── chinese_search.py ├── chinese_search.pyc ├── main.dic ├── smallseg.py ├── smallseg.pyc ├── suffix.dic ├── zh.py └── zh.pyc ├── make.bat ├── requirements.txt └── source ├── conf.py ├── index.rst ├── myshell-note.md ├── myshell-note.rst ├── shell-01-基础知识.md ├── shell-01-基础知识.rst ├── shell-02-字符串与数组.md ├── shell-02-字符串与数组.rst ├── shell-03-运算符.md ├── shell-03-运算符.rst ├── shell-04-流程控制.md ├── shell-04-流程控制.rst ├── shell-05-函数.md ├── shell-05-函数.rst ├── shell-06-正则表达式.md ├── shell-06-正则表达式.rst ├── shell-07-三剑客-grep.md ├── shell-07-三剑客-grep.rst ├── shell-08-三剑客-sed.md ├── shell-08-三剑客-sed.rst ├── shell-09-三剑客-awk.md ├── shell-09-三剑客-awk.rst ├── shell-10-杂项.md ├── shell-10-杂项.rst └── shell-demo.png /Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SOURCEDIR = source 8 | BUILDDIR = build 9 | 10 | # Put it first so that "make" without argument is like "make help". 11 | help: 12 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 13 | 14 | .PHONY: help Makefile 15 | 16 | # Catch-all target: route all unknown targets to Sphinx using the new 17 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 18 | %: Makefile 19 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 说明 2 | 3 | 为方便更多系统运维人员或开发人员更系统性的学习shell,利用`sphinx`将笔记整理构建程在线文档,方便学习交流,这份笔记是自学`Shell`时的资料总结,其中包含shell基础、正则表达式、AWK、SED、GREP、函数等,希望对其他人有用 4 | 5 | 本文 [over-shell](https://myshell-note.readthedocs.io/en/latest/) 6 | 7 | 个人信息: 8 | - [GitHub](https://github.com/redhatxl) 9 | - [掘金](https://juejin.im/user/5c36033fe51d456e4138b473/posts) 10 | - [慕课网](https://www.imooc.com/u/1260704) 11 | 12 | > over-shell:[在线阅读](https://myshell-note.readthedocs.io/en/latest/) 13 | 14 | > 新整理-Shell教程:[Wiki](http://www.imooc.com/wiki/shelllesson) 15 | 16 | 17 | ## demo 18 | 19 | ![](https://raw.githubusercontent.com/overnote/over-shell/master/source/shell-demo.png) 20 | 21 | ## 目录 22 | 23 | 24 | * #### [一 Shell基础](https://myshell-note.readthedocs.io/en/latest/shell-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.html) 25 | * #### [二 字符串与数组](https://myshell-note.readthedocs.io/en/latest/shell-02-%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E6%95%B0%E7%BB%84.html) 26 | * #### [三 运算符](https://myshell-note.readthedocs.io/en/latest/shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6.html) 27 | * #### [四 流程控制](https://myshell-note.readthedocs.io/en/latest/shell-04-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6.html) 28 | * #### [五 函数](https://myshell-note.readthedocs.io/en/latest/shell-05-%E5%87%BD%E6%95%B0.html) 29 | * #### [六 正则表达式](https://myshell-note.readthedocs.io/en/latest/shell-06-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.html) 30 | * #### [七 三剑客之grep](https://myshell-note.readthedocs.io/en/latest/shell-07-%E4%B8%89%E5%89%91%E5%AE%A2-grep.html) 31 | * #### [八 三剑客之sed](https://myshell-note.readthedocs.io/en/latest/shell-08-%E4%B8%89%E5%89%91%E5%AE%A2-sed.html) 32 | * #### [九 三剑客之awk](https://myshell-note.readthedocs.io/en/latest/shell-09-%E4%B8%89%E5%89%91%E5%AE%A2-awk.html) 33 | * #### [十 shell杂项](https://myshell-note.readthedocs.io/en/latest/shell-10-%E6%9D%82%E9%A1%B9.html) 34 | 35 | 36 | 37 | 38 | 39 | 如果此笔记对您有任何帮助,更多文章,欢迎关注博客一块学习交流🎉 40 | -------------------------------------------------------------------------------- /build/doctrees/environment.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/environment.pickle -------------------------------------------------------------------------------- /build/doctrees/index.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/index.doctree -------------------------------------------------------------------------------- /build/doctrees/myshell-note.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/myshell-note.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-01-基础知识.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-01-基础知识.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-02-字符串与数组.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-02-字符串与数组.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-03-运算符.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-03-运算符.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-04-流程控制.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-04-流程控制.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-05-函数.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-05-函数.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-06-正则表达式.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-06-正则表达式.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-07-三剑客-grep.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-07-三剑客-grep.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-08-三剑客-sed.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-08-三剑客-sed.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-09-三剑客-awk.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-09-三剑客-awk.doctree -------------------------------------------------------------------------------- /build/doctrees/shell-10-杂项.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/doctrees/shell-10-杂项.doctree -------------------------------------------------------------------------------- /build/html/.buildinfo: -------------------------------------------------------------------------------- 1 | # Sphinx build info version 1 2 | # This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. 3 | config: 90a29c80b262efcfdf091be52793f2af 4 | tags: 645f666f9bcd5a90fca523b33c5a78b7 5 | -------------------------------------------------------------------------------- /build/html/_sources/index.rst.txt: -------------------------------------------------------------------------------- 1 | .. MING's BLOG documentation master file, created by 2 | sphinx-quickstart on Sat Jun 23 16:57:48 2018. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to MING's BLOG's documentation! 7 | ======================================= 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | :caption: Contents: 12 | 13 | shell-01-基础知识 14 | shell-02-字符串与数组 15 | shell-03-运算符 16 | shell-04-流程控制 17 | shell-05-函数 18 | shell-06-正则表达式 19 | shell-07-三剑客-grep 20 | shell-08-三剑客-sed 21 | shell-09-三剑客-awk 22 | shell-10-杂项 23 | -------------------------------------------------------------------------------- /build/html/_sources/shell-01-基础知识.rst.txt: -------------------------------------------------------------------------------- 1 | .. contents:: 2 | :depth: 3 3 | .. 4 | 5 | 一、基础知识 6 | ============ 7 | 8 | 一、Shell简介 9 | ------------- 10 | 11 | Shell 是一个 C 语言编写的脚本语言,它是用户与 Linux 12 | 的桥梁,用户输入命令交给 Shell 处理, Shell 13 | 将相应的操作传递给内核(Kernel),内核把处理的结果输出给用户。 14 | 15 | 程序=指令+数据 16 | 17 | 二、Shell分类 18 | ------------- 19 | 20 | 2.1 图形界面 Shell(GUI Shell) 21 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 22 | 23 | GUI 为 Unix 或者类 Unix 24 | 操作系统构造一个功能完善、操作简单以及界面友好的桌面环境。主流桌面环境有 25 | KDE,Gnome 等。 26 | 27 | 2.2 命令行界面 Shell(CLI Shell) 28 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 29 | 30 | CLI 31 | 是在用户提示符下键入可执行指令的界面,用户通过键盘输入指令,完成一系列操作。 32 | 33 | 在 Linux 系统上主流的 CLI 实现是 Bash,是许多 Linux 发行版默认的 34 | Shell。还有许多 Unix 上Shell。 35 | 36 | :: 37 | 38 | [root@10-234-2-128 pyworkspace]# cat /etc/shells 39 | /bin/sh 40 | /bin/bash 41 | /sbin/nologin 42 | /usr/bin/sh 43 | /usr/bin/bash 44 | /usr/sbin/nologin 45 | 46 | **Shell的分类:** 47 | 48 | :: 49 | 50 | * Bourne Shell(/usr/bin/sh或/bin/sh) 51 | * Bourne Again Shell(/bin/bash) 52 | * C Shell(/usr/bin/csh) 53 | * K Shell(/usr/bin/ksh) 54 | * Shell for Root(/sbin/sh) 55 | 56 | **脚本命名:** 57 | 58 | 注意:见名知意,后缀规范为\ ``.sh`` 59 | 60 | 三、第一个Shell 61 | --------------- 62 | 63 | :: 64 | 65 | #!/bin/bash 66 | echo "this is my first shell script" 67 | 68 | ``#!`` 告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序 69 | ``/bin/bash`` 指定使用的是那种shell ``echo``\ 在终端打印出内容 70 | 71 | 四、执行Shell的三种方法 72 | ----------------------- 73 | 74 | 4.1 直接bash执行 75 | ~~~~~~~~~~~~~~~~ 76 | 77 | :: 78 | 79 | [root@shell workspace]# ll 80 | total 4 81 | -rw-r--r-- 1 root root 44 Sep 3 14:16 01-scripts.sh 82 | [root@shell workspace]# cat 01-scripts.sh 83 | #!/bin/bash 84 | 85 | echo "this is my first script" 86 | [root@shell workspace]# bash 01-scripts.sh 87 | this is my first script 88 | 89 | 4.2 ./执行 90 | ~~~~~~~~~~ 91 | 92 | :: 93 | 94 | [root@shell workspace]# ./01-scripts.sh 95 | -bash: ./01-scripts.sh: Permission denied 96 | [root@shell workspace]# chmod +x 01-scripts.sh 97 | [root@shell workspace]# ll 98 | total 4 99 | -rwxr-xr-x 1 root root 44 Sep 3 14:16 01-scripts.sh 100 | [root@shell workspace]# ./01-scripts.sh 101 | this is my first script 102 | 103 | 这种方式默认根据脚本第一行指定的解释器处理,如果没写以当前默认 Shell 104 | 解释器执行。 105 | 106 | 4.3 source执行 107 | ~~~~~~~~~~~~~~ 108 | 109 | :: 110 | 111 | [root@shell workspace]# source 01-scripts.sh 112 | this is my first script 113 | 114 | 五、Shell变量 115 | ------------- 116 | 117 | 变量名+内存空间 118 | 119 | 变量赋值:\ ``name=value`` 120 | 121 | 弱类型变量,所有变量类型视为字符串类型,对于数值相加自动转换为数组类型,无需实现声明 122 | 123 | 5.1 变量命名规则: 124 | ~~~~~~~~~~~~~~~~~~ 125 | 126 | - 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。 127 | - 中间不能有空格,可以使用下划线(_)。 128 | - 不能使用标点符号。 129 | - 不能使用bash里的关键字(可用help命令查看保留关键字) 130 | - 做到见名知意 131 | 132 | 环境变量作用范围:当前shell进程及其子进程 133 | 134 | 本地变量作用范围:当前shell 135 | 136 | 局部变量作用范围:代码片段 137 | 138 | 利用export将本地变量导入到环境,扩大作用范围 139 | 140 | 5.2 系统内置变量 141 | ~~~~~~~~~~~~~~~~ 142 | 143 | 在命令行提示符直接执行\ ``env、set``\ 查看系统或环境变量。\ ``env`` 144 | 显示用户环境变量,\ ``set`` 显示 Shell 145 | 预先定义好的变量以及用户变量。可以通过 ``export`` 导出成用户变量。 146 | 147 | 还可通过\ ``printevn/declare -x`` 148 | 149 | :: 150 | 151 | $SHELL 默认 Shell 152 | 153 | $HOME 当前用户家目录 154 | 155 | $IFS 内部字段分隔符 156 | 157 | $LANG 默认语言 158 | 159 | $PATH 默认可执行程序路径 160 | 161 | $PWD 当前目录 162 | 163 | $UID 当前用户 ID 164 | 165 | $USER 当前用户 166 | 167 | $HISTSIZE 历史命令大小,可通过 HISTTIMEFORMAT 变量设置命令执行时间 168 | 169 | $RANDOM 随机生成一个 0 至 32767 的整数 170 | 171 | $HOSTNAME 主机名 172 | 173 | **特殊变量** 174 | 175 | :: 176 | 177 | ${1..n} 指定第n个输入的变量名称 178 | $0 脚本自身名字 179 | 180 | $? 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败 181 | 182 | $# 位置参数总数 183 | 184 | $* 所有的位置参数被看做一个字符串 185 | 186 | $@ 每个位置参数被看做独立的字符串 187 | 188 | $$ 当前进程 PID 189 | 190 | $! 上一条运行后台进程的 PID 191 | 192 | **相同点**\ :都是引用所有参数。 193 | 194 | **不同点**\ :只有在双引号中体现出来。假设在脚本运行时写了三个参数 195 | 1、2、3,,则 " \* " 等价于 “1 2 3”(传递了一个参数),而 “@” 等价于 “1” 196 | “2” “3”(传递了三个参数)。 197 | 198 | profile 类型: \* 定义全局变量 \* 运行命令或脚本 199 | 200 | bashrc 类型: \* 定义本地变量 \* 定义命令别名 201 | 202 | 交互式登录shell: 加载顺序:/etc/profile -> /etc/profile.d/\* -> 203 | ~/.bash_profile -> ~/.bashrc -> /etc/bashrc 204 | 205 | 非交互式登录shell: 加载顺序:~/.bashrc -> /etc/bashrc -> 206 | /etc/profile.d/\* 207 | 208 | 5.3 用户自定义变量 209 | ~~~~~~~~~~~~~~~~~~ 210 | 211 | - 普通变量 212 | 213 | :: 214 | 215 | [root@shell workspace]# var=normal 216 | [root@shell workspace]# echo $var 217 | normal 218 | 219 | - 临时环境变量 220 | 221 | 在当前shell下定义的变量,只对当前shell有效,新的bash已经其子bash无法使用当前定义的shell,如果在本shell存在的情况下,使用\ ``export``\ 来导入到系统变量中,如果当前shell终端终端,那么导入的变量将全部失效,永久生效需要写入linux配置文件中。 222 | 223 | - 只读变量 224 | 225 | :: 226 | 227 | [root@shell ~]# var='test' 228 | [root@shell ~]# echo $var 229 | test 230 | [root@shell ~]# readonly var 231 | [root@shell ~]# var='bbb' 232 | -bash: var: readonly variable 233 | 234 | - 删除变量 235 | 236 | ``unset variable_name`` 237 | 238 | 变量被删除后不能再次使用。unset 命令不能删除只读变量。 239 | 240 | 5.4 变量引用 241 | ~~~~~~~~~~~~ 242 | 243 | - = 变量赋值 244 | - += 变量相加 245 | 246 | :: 247 | 248 | [root@shell data]# var=123 249 | [root@shell data]# var+=234 250 | [root@shell data]# echo $var 251 | 123234 252 | 253 | 为避免特殊字符及变量与字符连接使用,建议引用变量添加大括号 254 | 255 | 六、引号 256 | -------- 257 | 258 | 单引号是告诉 Shell 259 | 忽略特殊字符,而双引号则解释特殊符号原有的意义,比如$、!。 260 | 261 | :: 262 | 263 | [root@xuel-tmp-shell www]# var1="aaa" 264 | [root@xuel-tmp-shell www]# echo '$var1' 265 | $var1 266 | [root@xuel-tmp-shell www]# echo "$var1" 267 | aaa 268 | 269 | :: 270 | 271 | [root@xuel-tmp-shell www]# var2="aa" 272 | [root@xuel-tmp-shell www]# var3='bb $var2' 273 | [root@xuel-tmp-shell www]# echo $var3 274 | bb $var2 275 | [root@xuel-tmp-shell www]# var4="bb $var2" 276 | [root@xuel-tmp-shell www]# echo $var4 277 | bb aa 278 | 279 | 七、注释 280 | -------- 281 | 282 | - 单行注释使用\ ``#`` 283 | - 多行注释固定函数格式 284 | 285 | :: 286 | 287 | :</dev/null; then 142 | 143 | echo "$ip OK." 144 | 145 | else 146 | 147 | echo "$ip NO!" 148 | 149 | fi 150 | 151 | done 152 | 153 | 读取文件,判断url可用性 154 | 155 | :: 156 | 157 | #!/bin/bash 158 | #function:check url 159 | filename=urllist.txt 160 | for url in $(cat $filename) 161 | do 162 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 163 | if [ $status == "200" ];then 164 | echo "Url:$url is ok!status is $status" 165 | else 166 | echo "Url:$url is error!status is $status" 167 | fi 168 | done 169 | 170 | 三、while语句 171 | ------------- 172 | 173 | 格式: 174 | 175 | :: 176 | 177 | while 条件表达式:do 178 | command 179 | done 180 | 181 | eg1: 182 | 183 | :: 184 | 185 | #!/bin/bash 186 | N=0 187 | while [ $N -lt 5 ]; do 188 | let N++ 189 | echo $N 190 | done 191 | 192 | 条件表达式为 true,将会产生死循环,利用此可以将脚本一直放在后台进行执行 193 | eg2: 194 | 195 | :: 196 | 197 | #!/bin/bash 198 | IP=10.75.128.8 199 | dir="/DATA/oracle/netdir/" 200 | if [ ! -d ${dir} ];then 201 | mkdir -p ${dir} 202 | fi 203 | echo 1 > ${dir}ping.lock 204 | while true 205 | do 206 | Time=`date +%F` 207 | TIME="${Time} 23:59" 208 | if [ "${data}" == "${TIME}" ];then 209 | mkdir ${dir}${Time} && mv ${dir}ping2.log ${dir}${Time}-ping2.log 210 | mv ${dir}${Time}-ping2.log ${dir}${Time} 211 | fi 212 | find ${dir} -mtime +7 -name "*-ping2.log" -exec rm -rf {} \; 213 | find ${dir} -mtime +7 -type d -exec rm -rf {} \; 214 | 215 | data=`date +%F' '%H:%M` 216 | data1=`date +%F' '%H:%M:%S` 217 | echo "------------${data1}---------------">>${dir}ping2.log 218 | ping -c 10 ${IP} >>${dir}ping2.log 219 | if [ $? -eq 1 ];then 220 | STAT=`cat ${dir}ping.lock` 221 | if [ ${STAT} -eq 1 ];then 222 | /usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1 223 | 50) ping 金融云(10.75.128.8)中断,请检查深信服VPN! \n TIME:${data1}" echo 0 > ${dir}ping.lock 224 | else 225 | continue 226 | fi 227 | else 228 | STAT=`cat ${dir}ping.lock` 229 | if [ ${STAT} -eq 0 ];then 230 | /usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1 231 | 50) ping 金融云(10.75.128.8)恢复! \n TIME:${data1}" echo 1 > ${dir}ping.lock 232 | else 233 | continue 234 | fi 235 | fi 236 | 237 | 238 | done 239 | 240 | 文件处理 241 | 242 | eg3: 243 | 244 | :: 245 | 246 | #!/bin/bash 247 | #function:check url 248 | filename=urllist.txt 249 | cat $filename | while read url;do 250 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 251 | if [ $status == "200" ];then 252 | echo "Url:$url is ok!status is $status" 253 | else 254 | echo "Url:$url is error!status is $status" 255 | fi 256 | done 257 | 258 | 或 259 | 260 | :: 261 | 262 | #!/bin/bash 263 | #function:check url 264 | filename=urllist.txt 265 | while read url; 266 | do 267 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 268 | if [ $status == "200" ];then 269 | echo "Url:$url is ok!status is ${status}" 270 | else 271 | echo "Url:$url is error!status is ${status}" 272 | fi 273 | done <$filename 274 | 275 | 四、break 和 continue 语句 276 | -------------------------- 277 | 278 | break跳出循环 279 | 280 | :: 281 | 282 | #!/bin/bash 283 | 284 | N=0 285 | while true; do 286 | let N++ 287 | if [ $N -eq 5 ]; then 288 | break 289 | fi 290 | echo $N 291 | done 292 | 293 | continue 294 | 295 | :: 296 | 297 | #!/bin/bash 298 | N=0 299 | while [ $N -lt 5 ]; do 300 | let N++ 301 | if [ $N -eq 3 ]; then 302 | continue 303 | fi 304 | echo $N 305 | done 306 | 307 | 五、case语句 308 | ------------ 309 | 310 | 语句 311 | 312 | :: 313 | 314 | case 模式名 in 315 | 模式 1) 316 | 命令 317 | ;; 318 | 模式 2) 319 | 命令 320 | ;; 321 | *) 322 | 不符合以上模式执行的命令 323 | esac 324 | 325 | eg 326 | 327 | :: 328 | 329 | #!/bin/bash 330 | case $1 in 331 | start) 332 | echo "start." 333 | ;; 334 | stop) 335 | echo "stop." 336 | ;; 337 | restart) 338 | echo "restart." 339 | ;; 340 | *) 341 | echo "Usage: $0 {start|stop|restart}" 342 | esac 343 | -------------------------------------------------------------------------------- /build/html/_sources/shell-05-函数.rst.txt: -------------------------------------------------------------------------------- 1 | .. contents:: 2 | :depth: 3 3 | .. 4 | 5 | 五、函数 6 | ======== 7 | 8 | 一、概念 9 | -------- 10 | 11 | linux shell 12 | 可以用户定义函数,然后在shell脚本中可以随便调用,以此来重复调用公共函数,减少代码量。 13 | 14 | 二、格式 15 | -------- 16 | 17 | :: 18 | 19 | [ function ] funname() 20 | { 21 | action; 22 | [return int;] 23 | } 24 | 25 | 说明: 26 | 27 | - function 关键字可写,也可不写。 28 | - 参数返回,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。 29 | return后跟数值n(0-255),hell 30 | 函数返回值只能是整形数值,一般是用来表示函数执行成功与否的,0表示成功,其他值表示失败。因而用函数返回值来返回函数执行结果是不合适的。如果要硬生生地return某个计算结果,比如一个字符串,往往会得到错误提示:“numeric 31 | argument required”。 32 | 如果一定要让函数返回一个或多个值,可以定义全局变量,函数将计算结果赋给全局变量,然后脚本中其他地方通过访问全局变量,就可以获得那个函数“返回”的一个或多个执行结果了。 33 | 34 | :: 35 | 36 | #!/bin/bash 37 | function output_data() { 38 | DATA=$((1+1)) 39 | return $DATA 40 | } 41 | output_data 42 | echo $? 43 | 44 | :: 45 | 46 | #!/bin/bash 47 | # function:add number 48 | function add_num() { 49 | echo "请输入第一个数:" 50 | read number01 51 | echo "请输入第二个数字" 52 | read number02 53 | if [[ "$number01" =~ ^[0-9]+$ ]] && [[ "$number02" =~ ^[0-9]+$ ]];then 54 | sum=$(($number01+$number02)) 55 | echo "$number01 + $number02 = $sum" 56 | else 57 | echo "input must be number" 58 | fi 59 | } 60 | add_num 61 | 62 | 三、函数参数 63 | ------------ 64 | 65 | 将函数写成无状态的,将数据当做参数进行传入 66 | 67 | :: 68 | 69 | #!/bin/bash 70 | funWithParam(){ 71 | echo "第一个参数为 $1 !" 72 | echo "第二个参数为 $2 !" 73 | echo "第十个参数为 $10 !" 74 | echo "第十个参数为 ${10} !" 75 | echo "第十一个参数为 ${11} !" 76 | echo "参数总数有 $# 个!" 77 | echo "作为一个字符串输出所有参数 $* !" 78 | echo "作为一个字符串输出所有参数 $@ !" 79 | 80 | } 81 | funWithParam `seq 1 20` 82 | 83 | :: 84 | 85 | ${1..n} 指定第n个输入的变量名称 86 | $0 脚本自身名字 87 | 88 | $? 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败 89 | 90 | $# 位置参数总数 91 | 92 | $* 所有的位置参数被看做一个字符串 93 | 94 | $@ 每个位置参数被看做独立的字符串 95 | 96 | $$ 当前进程 PID 97 | 98 | $! 上一条运行后台进程的 PID 99 | 100 | eg:函数炸弹 101 | 102 | :: 103 | 104 | :(){ :|:& };: 105 | 106 | :|: 表示每次调用函数“:”的时候就会生成两份拷贝。 107 | 108 | & 放到后台 109 | 110 | 递归调用自身,直至系统崩溃 111 | -------------------------------------------------------------------------------- /build/html/_sources/shell-06-正则表达式.rst.txt: -------------------------------------------------------------------------------- 1 | .. role:: raw-latex(raw) 2 | :format: latex 3 | .. 4 | 5 | .. contents:: 6 | :depth: 3 7 | .. 8 | 9 | 六、正则表达式 10 | ============== 11 | 12 | 一、基本正则表达式 13 | ------------------ 14 | 15 | 1.1 字符匹配 16 | ~~~~~~~~~~~~ 17 | 18 | - .:匹配任意单个字符 19 | - 20 | - [^]:匹配指定范围外的任意单个字符 21 | - [:digit:]匹配元字符 22 | 23 | :: 24 | 25 | posix字符 26 | [:alnum:] 字母数字[a-z A-Z 0-9] 27 | [:alpha:]字母[a-z A-Z] 28 | [:blank:]空格或制表键 29 | [:cntrl:] 任何控制字符 30 | [:digit:] 数字 [0-9] 31 | [:graph:] 任何可视字符(无空格) 32 | [:lower:] 小写 [a-z] 33 | [:print:] 非控制字符 34 | [:punct:] 标点字符 35 | [:space:] 空格 36 | [:upper:] 大写 [A-Z] 37 | [:xdigit:] 十六进制数字 [0-9 a-f A-F] 38 | 39 | :: 40 | 41 | 特殊字符 42 | \w 匹配任意数字和字母,等效[a-zA-Z0-9_] 43 | \W 和\w相反,等效[^a-zA-Z0-9_] 44 | \b 匹配字符串开始或结束,等效\<和\> 45 | \s 匹配任意的空白字符 46 | \S 匹配非空白字符 47 | 48 | 1.2 次数匹配 49 | ~~~~~~~~~~~~ 50 | 51 | 用在制定的字符后面,表示制定前面的字符出现多少次 \* 52 | \*:匹配前面的字符任意次(0次获无数次) \* ?:匹配前面的字符0次或1次 \* 53 | +:匹配前面的字符至少1次 \* 54 | {m,}:匹配前面的字符至少m次(默认工作在贪婪模式下,?取消贪婪模式) \* 55 | {m,n}:匹配前面的字符至少m次,至多n次 eg: 56 | 57 | :: 58 | 59 | .*:匹配任意字符任意次数 60 | 61 | 1.3 位置锚定 62 | ~~~~~~~~~~~~ 63 | 64 | - ^:行首锚定,用于模式最左边 65 | - $:行尾锚定,用于模式最右边 66 | - \\<或:raw-latex:`\b`:锚定词首,用于单词模式左侧 67 | - \\>或:raw-latex:`\b`:锚定词尾,用于单词模式右侧 68 | 69 | eg: 70 | 71 | :: 72 | 73 | ^$:锚定空行 74 | 75 | 1.4 分组引用 76 | ~~~~~~~~~~~~ 77 | 78 | 分组 \* ():将一个或多个字符当成一个整体来进行后续处理 79 | 80 | 引用 \* 81 | 1:从左侧起,引用第一个左括号以及与之匹配右括号之间的模式所匹配到的字符,后向引用 82 | 83 | exercises: 84 | 85 | :: 86 | 87 | 1.显示/etc/init.d/functions文件中以大小s开头的行(使用两种方式) 88 | grep '^[Pp]' /etc/init.d/functions 89 | grep -i "^p" /etc/init.d/functions 90 | 91 | 2.显示/etc/passwd文件中不以/bin/bash结尾的行 92 | grep -v "/bin/bash$" /etc/passwd 93 | 94 | 3.显示/etc/passwd文件中ID号最大用户的用户名 95 | sort -t: -k3 -n /etc/passwd |tail -1 |cut -d: -f1 96 | 97 | 4.如果root用户存在,显示其默认的shell程序 98 | id root && grep '^\' /etc/passwd |awk -F: '{print $NF}' 99 | 100 | 5.找出/etc/passwd中的两位或三位数 101 | grep -o -E "[0-9]{2,3}" /etc/passwd 102 | grep -o "[0-9]\{2,3\}" /etc/passwd 103 | 104 | 6.显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存非空白字符的行: 105 | grep '^[[:space:]]\+[^[:space:]]' /etc/rc.d/rc.sysinit 106 | 107 | 108 | 7.找出"netstat -tan"命令的结果以"LISTEN"后跟0,1或多个空白字符结尾的行 109 | netstat -tan|grep 'LISTEN[[:space:]]*$' 110 | 111 | 8.如果root用户登录了系统,就显示root用户在线,否则说明未登录 112 | w |grep '^\'>/dev/null && echo "root在线"|| echo "root未登录" 113 | 114 | 9.找出/etc/rc.d/init.d/functions文件中某单词后面跟一对小括号的行 115 | grep '[[:alpha:]]*()' /etc/rc.d/init.d/functions 116 | 117 | 10.使用echo输出一个路径,使用egrep取出基名 118 | echo /tmp/tmp1/vmstat.8.gz |grep -E -o '[^/]+/?$'|cut -d/ -f1 119 | echo /tmp/tmp1/vmstat.8.gz |awk -F'/' '{print $NF}' 120 | 121 | 11.匹配PPID开头,行中又再次出现PPID的内容。/etc/init.d/functions 122 | grep -E "(PPID).*\1" /etc/init.d/functions 123 | 124 | 12.利用awk找出/etc/ssh/sshd_config内出过空行与以#开头的行 125 | awk '!/^#/ && !/^$/{print}' /etc/ssh/sshd_config 126 | grep -v -E '^#|^$' /etc/ssh/sshd_config 127 | -------------------------------------------------------------------------------- /build/html/_sources/shell-07-三剑客-grep.rst.txt: -------------------------------------------------------------------------------- 1 | .. contents:: 2 | :depth: 3 3 | .. 4 | 5 | 七、三剑客之grep 6 | ================ 7 | 8 | 一、概念 9 | -------- 10 | 11 | grep (global search regular expression(RE) and print out the 12 | line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。egrep是grep的扩展,支持更多的re元字符, 13 | fgrep就是fixed grep或fast 14 | grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。 15 | 16 | 二、语法格式 17 | ------------ 18 | 19 | grep [OPTION]… PATTERN [FILE] 20 | 21 | 2.1 选项 22 | ~~~~~~~~ 23 | 24 | - -i:忽略大小写 25 | - -c:统计匹配到字符串的次数 26 | - -n:顺便输出行号 27 | - -v:反向选择,显示没有匹配到的内容 28 | - -o:只显示匹配到的串 29 | - -A:显示匹配到的字符后面的n行 30 | - -B:显示匹配到的字符前面的n行 31 | - -C:显示前后各n行 32 | - .. rubric:: 2.2 模式 33 | :name: 模式 34 | 35 | - 基本正则表达式元字符: 36 | 37 | :: 38 | 39 | . :匹配任意单个字符 40 | [] :匹配指定范围内的字符 41 | [^]:匹配指定范围外的任意字符 42 | 43 | - 次数匹配(贪婪模式) 44 | 45 | :: 46 | 47 | *:匹配其前的字符0,1或者多次 48 | ?:匹配其前的字符0或1次 49 | \{m,n\}: 50 | \(m,\):至少m次 51 | \{0,n\}:至多n次 52 | \{m\}:m次 53 | 54 | - 锚定符: 55 | 56 | :: 57 | 58 | r..t :root chroot 59 | * 单词锚定: 60 | \<:锚定词首:\:锚定词尾:root\> 62 | * 行首行末锚定: 63 | ^: ^root, 行首 64 | $: root$ 行尾 65 | .*: 任意长度的任意字符 66 | 67 | - 分组: () (abc) 68 | - 引用: 69 | 70 | :: 71 | 72 | \1 :后向引用,引用前面的第一个左括号与与之对应的右括号中的模式所匹配到的内容 73 | 74 | eg: 75 | 76 | :: 77 | 78 | dmesg |grep -n eth0 79 | grep -E '/.{2,3}' /etc/passwd 80 | 81 | 取掉空行 82 | 83 | :: 84 | 85 | grep -E -v "^$|^#" /etc/httpd/conf/httpd.conf 86 | 87 | 查看mysql中的库 88 | 89 | :: 90 | 91 | $(mysql -uroot -p'passwd' -e "show databases;"|egrep -v 'Database|^test|mysql|performance_schema|information_schema') 92 | -------------------------------------------------------------------------------- /build/html/_sources/shell-08-三剑客-sed.rst.txt: -------------------------------------------------------------------------------- 1 | .. role:: raw-latex(raw) 2 | :format: latex 3 | .. 4 | 5 | .. contents:: 6 | :depth: 3 7 | .. 8 | 9 | 八、三剑客之sed 10 | =============== 11 | 12 | 一、概念: 13 | ---------- 14 | 15 | sed是一种流编辑的文本处理工具, \* 16 | 工作模式:将当前处理的行存储在临时缓冲区(模式空间),对缓冲区中的内容利用制定的动作进行处理,完成后输出到屏幕,接着反复重复执行此操作完成整改文件的处理。 17 | \* ## 二、适用场景 18 | 19 | - 大文件 20 | - 有规律的文本 21 | 22 | 三、语法 23 | -------- 24 | 25 | sed [option] ‘Addresscommand’ [file …] 26 | 27 | - 选项 28 | 29 | - -n:安静模式,仅显示script处理后的结果,不再默认显示模式空间中的内容 30 | - -e: 31 | 32 | .. raw:: html 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 | 43 | 44 | 103 | 104 |
105 | 106 | 107 | 113 | 114 | 115 |
116 | 117 |
118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 |
136 | 137 |
    138 | 139 |
  • Docs »
  • 140 | 141 |
  • Index
  • 142 | 143 | 144 |
  • 145 | 146 | 147 | 148 |
  • 149 | 150 |
151 | 152 | 153 |
154 |
155 |
156 |
157 | 158 | 159 |

Index

160 | 161 |
162 | 163 |
164 | 165 | 166 |
167 | 168 |
169 |
170 | 171 | 172 |
173 | 174 |
175 |

176 | © Copyright KaliArch 177 | 178 |

179 |
180 | Built with Sphinx using a theme provided by Read the Docs. 181 | 182 |
183 | 184 |
185 |
186 | 187 |
188 | 189 |
190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 213 | 214 | 215 | -------------------------------------------------------------------------------- /build/html/objects.inv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/build/html/objects.inv -------------------------------------------------------------------------------- /build/html/search.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Search — KaliArch SHELL BLOG 1.0.0 documentation 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
41 | 42 | 43 | 102 | 103 |
104 | 105 | 106 | 112 | 113 | 114 |
115 | 116 |
117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 |
135 | 136 |
    137 | 138 |
  • Docs »
  • 139 | 140 |
  • Search
  • 141 | 142 | 143 |
  • 144 | 145 | 146 | 147 |
  • 148 | 149 |
150 | 151 | 152 |
153 |
154 |
155 |
156 | 157 | 165 | 166 | 167 |
168 | 169 |
170 | 171 |
172 | 173 |
174 |
175 | 176 | 177 |
178 | 179 |
180 |

181 | © Copyright KaliArch 182 | 183 |

184 |
185 | Built with Sphinx using a theme provided by Read the Docs. 186 | 187 |
188 | 189 |
190 |
191 | 192 |
193 | 194 |
195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 219 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | -------------------------------------------------------------------------------- /build/html/shell-08-三剑客-sed.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 八、三剑客之sed — KaliArch SHELL BLOG 1.0.0 documentation 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
43 | 44 | 45 | 108 | 109 |
110 | 111 | 112 | 118 | 119 | 120 |
121 | 122 |
123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 |
141 | 142 |
    143 | 144 |
  • Docs »
  • 145 | 146 |
  • 八、三剑客之sed
  • 147 | 148 | 149 |
  • 150 | 151 | 152 | View page source 153 | 154 | 155 |
  • 156 | 157 |
158 | 159 | 160 |
161 |
162 |
163 |
164 | 165 |
166 |

Contents

167 | 174 |
175 |
176 |

八、三剑客之sed

177 |
178 |

一、概念:

179 |

sed是一种流编辑的文本处理工具, * 180 | 工作模式:将当前处理的行存储在临时缓冲区(模式空间),对缓冲区中的内容利用制定的动作进行处理,完成后输出到屏幕,接着反复重复执行此操作完成整改文件的处理。 181 | * ## 二、适用场景

182 |
    183 |
  • 大文件
  • 184 |
  • 有规律的文本
  • 185 |
186 |
187 |
188 |

三、语法

189 |

sed [option] ‘Addresscommand’ [file …]

190 |
    191 |
  • 选项

    192 |
      193 |
    • -n:安静模式,仅显示script处理后的结果,不再默认显示模式空间中的内容

      194 |
    • 195 |
    • -e:

      196 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 329 | 330 | 331 | -------------------------------------------------------------------------------- /exts/chinese_search.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | def setup(app): 4 | import sphinx.search as search 5 | import zh 6 | search.languages["zh_CN"] = zh.SearchChinese -------------------------------------------------------------------------------- /exts/chinese_search.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/exts/chinese_search.pyc -------------------------------------------------------------------------------- /exts/smallseg.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function 3 | import re 4 | import os 5 | import sys 6 | 7 | try: 8 | xrange # Python 2 9 | except NameError: 10 | xrange = range # Python 3 11 | 12 | class SEG(object): 13 | def __init__(self): 14 | _localDir=os.path.dirname(__file__) 15 | _curpath=os.path.normpath(os.path.join(os.getcwd(),_localDir)) 16 | curpath=_curpath 17 | self.d = {} 18 | print("loading dict...", file=sys.stderr) 19 | with open(os.path.join(curpath, "main.dic")) as in_file: 20 | self.set([x.rstrip() for x in in_file]) 21 | with open(os.path.join(curpath,"suffix.dic")) as in_file: 22 | self.specialwords= set([x.rstrip().decode('utf-8') for x in in_file]) 23 | print('dict ok.', file=sys.stderr) 24 | #set dictionary(a list) 25 | def set(self,keywords): 26 | p = self.d 27 | q = {} 28 | k = '' 29 | for word in keywords: 30 | word = (chr(11)+word).decode('utf-8') 31 | if len(word)>5: 32 | continue 33 | p = self.d 34 | ln = len(word) 35 | for i in xrange(ln-1,-1,-1): 36 | char = word[i].lower() 37 | if p=='': 38 | q[k] = {} 39 | p = q[k] 40 | if not (char in p): 41 | p[char] = '' 42 | q = p 43 | k = char 44 | p = p[char] 45 | 46 | def _binary_seg(self,s): 47 | ln = len(s) 48 | if ln==1: 49 | return [s] 50 | R = [] 51 | for i in xrange(ln,1,-1): 52 | tmp = s[i-2:i] 53 | R.append(tmp) 54 | return R 55 | 56 | def _pro_unreg(self,piece): 57 | #print(piece) 58 | R = [] 59 | tmp = re.sub(u"。|,|,|!|…|!|《|》|<|>|\"|'|:|:|?|\?|、|\||“|”|‘|’|;|—|(|)|·|\(|\)| "," ",piece).split() 60 | ln1 = len(tmp) 61 | for i in xrange(len(tmp)-1,-1,-1): 62 | mc = re.split(r"([0-9A-Za-z\-\+#@_\.]+)",tmp[i]) 63 | for j in xrange(len(mc)-1,-1,-1): 64 | r = mc[j] 65 | if re.search(r"([0-9A-Za-z\-\+#@_\.]+)",r)!=None: 66 | R.append(r) 67 | else: 68 | R.extend(self._binary_seg(r)) 69 | return R 70 | 71 | 72 | def cut(self,text): 73 | """ 74 | """ 75 | text = text.decode('utf-8','ignore') 76 | p = self.d 77 | ln = len(text) 78 | i = ln 79 | j = 0 80 | z = ln 81 | q = 0 82 | recognised = [] 83 | mem = None 84 | mem2 = None 85 | while i-j>0: 86 | t = text[i-j-1].lower() 87 | #print(i,j,t,mem) 88 | if not (t in p): 89 | if (mem!=None) or (mem2!=None): 90 | if mem!=None: 91 | i,j,z = mem 92 | mem = None 93 | elif mem2!=None: 94 | delta = mem2[0]-i 95 | if delta>=1: 96 | if (delta<5) and (re.search(u"[\w\u2E80-\u9FFF]",t)!=None): 97 | pre = text[i-j] 98 | #print(pre) 99 | if not (pre in self.specialwords): 100 | i,j,z,q = mem2 101 | del recognised[q:] 102 | mem2 = None 103 | 104 | p = self.d 105 | if((i1))): 125 | #print(text[i-1]) 126 | mem = None 127 | mem2 = i,j,z,len(recognised) 128 | p = self.d 129 | i -= 1 130 | j = 0 131 | continue 132 | #print(mem) 133 | p = self.d 134 | #print(i,j,z,text[i:z]) 135 | if((i 1 17 | -------------------------------------------------------------------------------- /exts/zh.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/overnote/over-shell/006ca3aace761da5042db3c11230308835143d38/exts/zh.pyc -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | 13 | if "%1" == "" goto help 14 | 15 | %SPHINXBUILD% >NUL 2>NUL 16 | if errorlevel 9009 ( 17 | echo. 18 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 19 | echo.installed, then set the SPHINXBUILD environment variable to point 20 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 21 | echo.may add the Sphinx directory to PATH. 22 | echo. 23 | echo.If you don't have Sphinx installed, grab it from 24 | echo.http://sphinx-doc.org/ 25 | exit /b 1 26 | ) 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | greenlet==0.4.5 2 | oauthlib==0.7.2 3 | paho-mqtt==1.0 4 | tzlocal==1.1.2 5 | redis==2.10.3 6 | requests==2.20.0 7 | requests-oauthlib==0.4.2 8 | whitenoise==1.0.3 9 | openpyxl==2.1.5 -------------------------------------------------------------------------------- /source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # python3-cookbook documentation build configuration file, created by 4 | # sphinx-quickstart on Tue Aug 19 03:21:45 2014. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | 18 | # If extensions (or modules to document with autodoc) are in another directory, 19 | # add these directories to sys.path here. If the directory is relative to the 20 | # documentation root, use os.path.abspath to make it absolute, like shown here. 21 | #sys.path.insert(0, os.path.abspath('.')) 22 | 23 | # -- General configuration ------------------------------------------------ 24 | 25 | # If your documentation needs a minimal Sphinx version, state it here. 26 | #needs_sphinx = '1.0' 27 | 28 | # Add any Sphinx extension module names here, as strings. They can be 29 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 30 | # ones. 31 | extensions = ['chinese_search'] 32 | 33 | # Add any paths that contain templates here, relative to this directory. 34 | templates_path = ['_templates'] 35 | 36 | # The suffix of source filenames. 37 | source_suffix = ['.rst','.md'] 38 | 39 | # The encoding of source files. 40 | #source_encoding = 'utf-8-sig' 41 | 42 | # The master toctree document. 43 | master_doc = 'index' 44 | 45 | # General information about the project. 46 | project = u'KaliArch SHELL BLOG' 47 | copyright = u'KaliArch' 48 | 49 | # The version info for the project you're documenting, acts as replacement for 50 | # |version| and |release|, also used in various other places throughout the 51 | # built documents. 52 | # 53 | # The short X.Y version. 54 | version = '1.0' 55 | # The full version, including alpha/beta/rc tags. 56 | release = '1.0.0' 57 | 58 | exclude_patterns = [] 59 | 60 | html_theme = 'default' 61 | 62 | html_static_path = ['_static'] 63 | 64 | # Output file base name for HTML help builder. 65 | htmlhelp_basename = 'myshell note' 66 | 67 | 68 | # -- Options for LaTeX output --------------------------------------------- 69 | 70 | # 注:在生成html的时候这句话要注释 71 | # latex_engine = 'xelatex' 72 | 73 | latex_elements={# The paper size ('letterpaper' or 'a4paper'). 74 | 'papersize':'a4paper',# The font size ('10pt', '11pt' or '12pt'). 75 | 'pointsize':'12pt','classoptions':',oneside','babel':'',#必须 76 | 'inputenc':'',#必须 77 | 'utf8extra':'',#必须 78 | # Additional stuff for the LaTeX preamble. 79 | 'preamble': r""" 80 | \usepackage{xeCJK} 81 | \usepackage{indentfirst} 82 | \setlength{\parindent}{2em} 83 | \setCJKmainfont{WenQuanYi Micro Hei} 84 | \setCJKmonofont[Scale=0.9]{WenQuanYi Micro Hei Mono} 85 | \setCJKfamilyfont{song}{WenQuanYi Micro Hei} 86 | \setCJKfamilyfont{sf}{WenQuanYi Micro Hei} 87 | \XeTeXlinebreaklocale "zh" 88 | \XeTeXlinebreakskip = 0pt plus 1pt 89 | """} 90 | 91 | # Grouping the document tree into LaTeX files. List of tuples 92 | # (source start file, target name, title, 93 | # author, documentclass [howto, manual, or own class]). 94 | latex_documents = [ 95 | ('index', 'python3-cookbook.tex', u'myshell note', 96 | u'KaliArch', 'howto'), 97 | ] 98 | 99 | # -- Options for manual page output --------------------------------------- 100 | 101 | # One entry per manual page. List of tuples 102 | # (source start file, name, description, authors, manual section). 103 | man_pages = [ 104 | ('index', 'myshell note', u'myshell note', 105 | [u'KaliArch'], 1) 106 | ] 107 | 108 | # If true, show URL addresses after external links. 109 | #man_show_urls = False 110 | 111 | 112 | # -- Options for Texinfo output ------------------------------------------- 113 | 114 | # Grouping the document tree into Texinfo files. List of tuples 115 | # (source start file, target name, title, author, 116 | # dir menu entry, description, category) 117 | texinfo_documents = [ 118 | ('index', 'myshell-note', u'myshell note', 119 | u'KaliArch', 'myshell-note', 'myshell note', 120 | 'Miscellaneous'), 121 | ] 122 | 123 | 124 | #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 125 | # on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org 126 | on_rtd = os.environ.get('READTHEDOCS', None) == 'True' 127 | 128 | if not on_rtd: # only import and set the theme if we're building docs locally 129 | import sphinx_rtd_theme 130 | html_theme = 'sphinx_rtd_theme' 131 | html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] 132 | # otherwise, readthedocs.org uses their theme by default, so no need to specify it 133 | #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 134 | 135 | _exts = "../exts" 136 | sys.path.append(os.path.abspath(_exts)) 137 | -------------------------------------------------------------------------------- /source/index.rst: -------------------------------------------------------------------------------- 1 | .. KaliArch's BLOG documentation master file, created by 2 | You can adapt this file completely to your liking, but it should at least 3 | contain the root `toctree` directive. 4 | 5 | Welcome to KaliArch's BLOG's documentation! 6 | ======================================= 7 | 8 | .. toctree:: 9 | :maxdepth: 2 10 | :caption: Contents: 11 | 12 | shell-01-基础知识 13 | shell-02-字符串与数组 14 | shell-03-运算符 15 | shell-04-流程控制 16 | shell-05-函数 17 | shell-06-正则表达式 18 | shell-07-三剑客-grep 19 | shell-08-三剑客-sed 20 | shell-09-三剑客-awk 21 | shell-10-杂项 22 | -------------------------------------------------------------------------------- /source/shell-01-基础知识.md: -------------------------------------------------------------------------------- 1 | # 一、基础知识 2 | 3 | ## 一、Shell简介 4 | 5 | Shell 是一个 C 语言编写的脚本语言,它是用户与 Linux 的桥梁,用户输入命令交给 Shell 处理, Shell 将相应的操作传递给内核(Kernel),内核把处理的结果输出给用户。 6 | 7 | 程序=指令+数据 8 | 9 | ## 二、Shell分类 10 | 11 | #### 2.1 图形界面 Shell(GUI Shell) 12 | 13 | GUI 为 Unix 或者类 Unix 操作系统构造一个功能完善、操作简单以及界面友好的桌面环境。主流桌面环境有 KDE,Gnome 等。 14 | 15 | #### 2.2 命令行界面 Shell(CLI Shell) 16 | 17 | CLI 是在用户提示符下键入可执行指令的界面,用户通过键盘输入指令,完成一系列操作。 18 | 19 | 在 Linux 系统上主流的 CLI 实现是 Bash,是许多 Linux 发行版默认的 Shell。还有许多 Unix 上Shell。 20 | 21 | ``` 22 | [root@10-234-2-128 pyworkspace]# cat /etc/shells 23 | /bin/sh 24 | /bin/bash 25 | /sbin/nologin 26 | /usr/bin/sh 27 | /usr/bin/bash 28 | /usr/sbin/nologin 29 | ``` 30 | 31 | **Shell的分类:** 32 | 33 | ``` 34 | * Bourne Shell(/usr/bin/sh或/bin/sh) 35 | * Bourne Again Shell(/bin/bash) 36 | * C Shell(/usr/bin/csh) 37 | * K Shell(/usr/bin/ksh) 38 | * Shell for Root(/sbin/sh) 39 | ``` 40 | 41 | **脚本命名:** 42 | 43 | 注意:见名知意,后缀规范为`.sh` 44 | 45 | 46 | ## 三、第一个Shell 47 | 48 | ``` 49 | #!/bin/bash 50 | echo "this is my first shell script" 51 | ``` 52 | 53 | `#!` 告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序 54 | `/bin/bash` 指定使用的是那种shell 55 | `echo `在终端打印出内容 56 | 57 | ## 四、执行Shell的三种方法 58 | 59 | #### 4.1 直接bash执行 60 | 61 | ``` 62 | [root@shell workspace]# ll 63 | total 4 64 | -rw-r--r-- 1 root root 44 Sep 3 14:16 01-scripts.sh 65 | [root@shell workspace]# cat 01-scripts.sh 66 | #!/bin/bash 67 | 68 | echo "this is my first script" 69 | [root@shell workspace]# bash 01-scripts.sh 70 | this is my first script 71 | ``` 72 | 73 | #### 4.2 ./执行 74 | 75 | ``` 76 | [root@shell workspace]# ./01-scripts.sh 77 | -bash: ./01-scripts.sh: Permission denied 78 | [root@shell workspace]# chmod +x 01-scripts.sh 79 | [root@shell workspace]# ll 80 | total 4 81 | -rwxr-xr-x 1 root root 44 Sep 3 14:16 01-scripts.sh 82 | [root@shell workspace]# ./01-scripts.sh 83 | this is my first script 84 | ``` 85 | 86 | 这种方式默认根据脚本第一行指定的解释器处理,如果没写以当前默认 Shell 解释器执行。 87 | 88 | ### 4.3 source执行 89 | 90 | ``` 91 | [root@shell workspace]# source 01-scripts.sh 92 | this is my first script 93 | ``` 94 | 95 | ## 五、Shell变量 96 | 97 | 变量名+内存空间 98 | 99 | 变量赋值:`name=value` 100 | 101 | 弱类型变量,所有变量类型视为字符串类型,对于数值相加自动转换为数组类型,无需实现声明 102 | 103 | #### 5.1 变量命名规则: 104 | 105 | * 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。 106 | * 中间不能有空格,可以使用下划线(_)。 107 | * 不能使用标点符号。 108 | * 不能使用bash里的关键字(可用help命令查看保留关键字) 109 | * 做到见名知意 110 | 111 | 环境变量作用范围:当前shell进程及其子进程 112 | 113 | 本地变量作用范围:当前shell 114 | 115 | 局部变量作用范围:代码片段 116 | 117 | 利用export将本地变量导入到环境,扩大作用范围 118 | 119 | #### 5.2 系统内置变量 120 | 121 | 在命令行提示符直接执行` env、set `查看系统或环境变量。`env` 显示用户环境变量,`set` 显示 Shell 预先定义好的变量以及用户变量。可以通过 `export` 导出成用户变量。 122 | 123 | 还可通过`printevn/declare -x` 124 | 125 | ``` 126 | $SHELL 默认 Shell 127 | 128 | $HOME 当前用户家目录 129 | 130 | $IFS 内部字段分隔符 131 | 132 | $LANG 默认语言 133 | 134 | $PATH 默认可执行程序路径 135 | 136 | $PWD 当前目录 137 | 138 | $UID 当前用户 ID 139 | 140 | $USER 当前用户 141 | 142 | $HISTSIZE 历史命令大小,可通过 HISTTIMEFORMAT 变量设置命令执行时间 143 | 144 | $RANDOM 随机生成一个 0 至 32767 的整数 145 | 146 | $HOSTNAME 主机名 147 | ``` 148 | 149 | **特殊变量** 150 | 151 | ``` 152 | ${1..n} 指定第n个输入的变量名称 153 | $0 脚本自身名字 154 | 155 | $? 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败 156 | 157 | $# 位置参数总数 158 | 159 | $* 所有的位置参数被看做一个字符串 160 | 161 | $@ 每个位置参数被看做独立的字符串 162 | 163 | $$ 当前进程 PID 164 | 165 | $! 上一条运行后台进程的 PID 166 | ``` 167 | 168 | **相同点**:都是引用所有参数。 169 | 170 | **不同点**:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)。 171 | 172 | 173 | profile 类型: 174 | * 定义全局变量 175 | * 运行命令或脚本 176 | 177 | bashrc 类型: 178 | * 定义本地变量 179 | * 定义命令别名 180 | 181 | 182 | 交互式登录shell: 183 | 加载顺序:/etc/profile -> /etc/profile.d/* -> ~/.bash_profile -> ~/.bashrc -> /etc/bashrc 184 | 185 | 186 | 非交互式登录shell: 187 | 加载顺序:~/.bashrc -> /etc/bashrc -> /etc/profile.d/* 188 | 189 | #### 5.3 用户自定义变量 190 | 191 | * 普通变量 192 | 193 | ``` 194 | [root@shell workspace]# var=normal 195 | [root@shell workspace]# echo $var 196 | normal 197 | ``` 198 | 199 | * 临时环境变量 200 | 201 | 在当前shell下定义的变量,只对当前shell有效,新的bash已经其子bash无法使用当前定义的shell,如果在本shell存在的情况下,使用`export`来导入到系统变量中,如果当前shell终端终端,那么导入的变量将全部失效,永久生效需要写入linux配置文件中。 202 | 203 | * 只读变量 204 | 205 | ``` 206 | [root@shell ~]# var='test' 207 | [root@shell ~]# echo $var 208 | test 209 | [root@shell ~]# readonly var 210 | [root@shell ~]# var='bbb' 211 | -bash: var: readonly variable 212 | ``` 213 | 214 | * 删除变量 215 | 216 | ```unset variable_name``` 217 | 218 | 变量被删除后不能再次使用。unset 命令不能删除只读变量。 219 | 220 | #### 5.4 变量引用 221 | 222 | * = 变量赋值 223 | * += 变量相加 224 | 225 | ``` 226 | [root@shell data]# var=123 227 | [root@shell data]# var+=234 228 | [root@shell data]# echo $var 229 | 123234 230 | ``` 231 | 232 | 为避免特殊字符及变量与字符连接使用,建议引用变量添加大括号 233 | 234 | ## 六、引号 235 | 236 | 单引号是告诉 Shell 忽略特殊字符,而双引号则解释特殊符号原有的意义,比如$、!。 237 | 238 | ``` 239 | [root@xuel-tmp-shell www]# var1="aaa" 240 | [root@xuel-tmp-shell www]# echo '$var1' 241 | $var1 242 | [root@xuel-tmp-shell www]# echo "$var1" 243 | aaa 244 | ``` 245 | 246 | ``` 247 | [root@xuel-tmp-shell www]# var2="aa" 248 | [root@xuel-tmp-shell www]# var3='bb $var2' 249 | [root@xuel-tmp-shell www]# echo $var3 250 | bb $var2 251 | [root@xuel-tmp-shell www]# var4="bb $var2" 252 | [root@xuel-tmp-shell www]# echo $var4 253 | bb aa 254 | ``` 255 | 256 | 257 | ## 七、注释 258 | 259 | * 单行注释使用`#` 260 | * 多行注释固定函数格式 261 | 262 | ``` 263 | :< /etc/profile.d/\* -> 203 | ~/.bash_profile -> ~/.bashrc -> /etc/bashrc 204 | 205 | 非交互式登录shell: 加载顺序:~/.bashrc -> /etc/bashrc -> 206 | /etc/profile.d/\* 207 | 208 | 5.3 用户自定义变量 209 | ~~~~~~~~~~~~~~~~~~ 210 | 211 | - 普通变量 212 | 213 | :: 214 | 215 | [root@shell workspace]# var=normal 216 | [root@shell workspace]# echo $var 217 | normal 218 | 219 | - 临时环境变量 220 | 221 | 在当前shell下定义的变量,只对当前shell有效,新的bash已经其子bash无法使用当前定义的shell,如果在本shell存在的情况下,使用\ ``export``\ 来导入到系统变量中,如果当前shell终端终端,那么导入的变量将全部失效,永久生效需要写入linux配置文件中。 222 | 223 | - 只读变量 224 | 225 | :: 226 | 227 | [root@shell ~]# var='test' 228 | [root@shell ~]# echo $var 229 | test 230 | [root@shell ~]# readonly var 231 | [root@shell ~]# var='bbb' 232 | -bash: var: readonly variable 233 | 234 | - 删除变量 235 | 236 | ``unset variable_name`` 237 | 238 | 变量被删除后不能再次使用。unset 命令不能删除只读变量。 239 | 240 | 5.4 变量引用 241 | ~~~~~~~~~~~~ 242 | 243 | - = 变量赋值 244 | - += 变量相加 245 | 246 | :: 247 | 248 | [root@shell data]# var=123 249 | [root@shell data]# var+=234 250 | [root@shell data]# echo $var 251 | 123234 252 | 253 | 为避免特殊字符及变量与字符连接使用,建议引用变量添加大括号 254 | 255 | 六、引号 256 | -------- 257 | 258 | 单引号是告诉 Shell 259 | 忽略特殊字符,而双引号则解释特殊符号原有的意义,比如$、!。 260 | 261 | :: 262 | 263 | [root@xuel-tmp-shell www]# var1="aaa" 264 | [root@xuel-tmp-shell www]# echo '$var1' 265 | $var1 266 | [root@xuel-tmp-shell www]# echo "$var1" 267 | aaa 268 | 269 | :: 270 | 271 | [root@xuel-tmp-shell www]# var2="aa" 272 | [root@xuel-tmp-shell www]# var3='bb $var2' 273 | [root@xuel-tmp-shell www]# echo $var3 274 | bb $var2 275 | [root@xuel-tmp-shell www]# var4="bb $var2" 276 | [root@xuel-tmp-shell www]# echo $var4 277 | bb aa 278 | 279 | 七、注释 280 | -------- 281 | 282 | - 单行注释使用\ ``#`` 283 | - 多行注释固定函数格式 284 | 285 | :: 286 | 287 | :</dev/null; then 132 | 133 | echo "$ip OK." 134 | 135 | else 136 | 137 | echo "$ip NO!" 138 | 139 | fi 140 | 141 | done 142 | ``` 143 | 144 | 读取文件,判断url可用性 145 | 146 | ``` 147 | #!/bin/bash 148 | #function:check url 149 | filename=urllist.txt 150 | for url in $(cat $filename) 151 | do 152 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 153 | if [ $status == "200" ];then 154 | echo "Url:$url is ok!status is $status" 155 | else 156 | echo "Url:$url is error!status is $status" 157 | fi 158 | done 159 | 160 | ``` 161 | 162 | ## 三、while语句 163 | 164 | 格式: 165 | 166 | ``` 167 | while 条件表达式:do 168 | command 169 | done 170 | ``` 171 | 172 | eg1: 173 | 174 | ``` 175 | #!/bin/bash 176 | N=0 177 | while [ $N -lt 5 ]; do 178 | let N++ 179 | echo $N 180 | done 181 | ``` 182 | 183 | 条件表达式为 true,将会产生死循环,利用此可以将脚本一直放在后台进行执行 184 | eg2: 185 | 186 | ``` 187 | #!/bin/bash 188 | IP=10.75.128.8 189 | dir="/DATA/oracle/netdir/" 190 | if [ ! -d ${dir} ];then 191 | mkdir -p ${dir} 192 | fi 193 | echo 1 > ${dir}ping.lock 194 | while true 195 | do 196 | Time=`date +%F` 197 | TIME="${Time} 23:59" 198 | if [ "${data}" == "${TIME}" ];then 199 | mkdir ${dir}${Time} && mv ${dir}ping2.log ${dir}${Time}-ping2.log 200 | mv ${dir}${Time}-ping2.log ${dir}${Time} 201 | fi 202 | find ${dir} -mtime +7 -name "*-ping2.log" -exec rm -rf {} \; 203 | find ${dir} -mtime +7 -type d -exec rm -rf {} \; 204 | 205 | data=`date +%F' '%H:%M` 206 | data1=`date +%F' '%H:%M:%S` 207 | echo "------------${data1}---------------">>${dir}ping2.log 208 | ping -c 10 ${IP} >>${dir}ping2.log 209 | if [ $? -eq 1 ];then 210 | STAT=`cat ${dir}ping.lock` 211 | if [ ${STAT} -eq 1 ];then 212 | /usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1 213 | 50) ping 金融云(10.75.128.8)中断,请检查深信服VPN! \n TIME:${data1}" echo 0 > ${dir}ping.lock 214 | else 215 | continue 216 | fi 217 | else 218 | STAT=`cat ${dir}ping.lock` 219 | if [ ${STAT} -eq 0 ];then 220 | /usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1 221 | 50) ping 金融云(10.75.128.8)恢复! \n TIME:${data1}" echo 1 > ${dir}ping.lock 222 | else 223 | continue 224 | fi 225 | fi 226 | 227 | 228 | done 229 | 230 | ``` 231 | 232 | 文件处理 233 | 234 | eg3: 235 | 236 | ``` 237 | #!/bin/bash 238 | #function:check url 239 | filename=urllist.txt 240 | cat $filename | while read url;do 241 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 242 | if [ $status == "200" ];then 243 | echo "Url:$url is ok!status is $status" 244 | else 245 | echo "Url:$url is error!status is $status" 246 | fi 247 | done 248 | ``` 249 | 250 | 或 251 | 252 | ``` 253 | #!/bin/bash 254 | #function:check url 255 | filename=urllist.txt 256 | while read url; 257 | do 258 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 259 | if [ $status == "200" ];then 260 | echo "Url:$url is ok!status is ${status}" 261 | else 262 | echo "Url:$url is error!status is ${status}" 263 | fi 264 | done <$filename 265 | ``` 266 | 267 | ## 四、break 和 continue 语句 268 | 269 | break跳出循环 270 | 271 | ``` 272 | #!/bin/bash 273 | 274 | N=0 275 | while true; do 276 | let N++ 277 | if [ $N -eq 5 ]; then 278 | break 279 | fi 280 | echo $N 281 | done 282 | ``` 283 | 284 | continue 285 | 286 | ``` 287 | #!/bin/bash 288 | N=0 289 | while [ $N -lt 5 ]; do 290 | let N++ 291 | if [ $N -eq 3 ]; then 292 | continue 293 | fi 294 | echo $N 295 | done 296 | ``` 297 | 298 | ## 五、case语句 299 | 300 | 语句 301 | 302 | ``` 303 | case 模式名 in 304 | 模式 1) 305 | 命令 306 | ;; 307 | 模式 2) 308 | 命令 309 | ;; 310 | *) 311 | 不符合以上模式执行的命令 312 | esac 313 | ``` 314 | 315 | eg 316 | 317 | ``` 318 | #!/bin/bash 319 | case $1 in 320 | start) 321 | echo "start." 322 | ;; 323 | stop) 324 | echo "stop." 325 | ;; 326 | restart) 327 | echo "restart." 328 | ;; 329 | *) 330 | echo "Usage: $0 {start|stop|restart}" 331 | esac 332 | ``` 333 | -------------------------------------------------------------------------------- /source/shell-04-流程控制.rst: -------------------------------------------------------------------------------- 1 | .. contents:: 2 | :depth: 3 3 | .. 4 | 5 | 四、流程控制 6 | ============ 7 | 8 | 一、if语句 9 | ---------- 10 | 11 | 1.1 单分支 12 | ~~~~~~~~~~ 13 | 14 | :: 15 | 16 | if condition 17 | then 18 | command1 19 | command2 20 | ... 21 | commandN 22 | fi 23 | 24 | eg: 25 | 26 | :: 27 | 28 | if [ `ps -ef |grep /usr/sbin/sshd|grep -v grep|wc -l` -eq 1 ];then echo "sshd server exist";fi 29 | 30 | 1.2 双分支 31 | ~~~~~~~~~~ 32 | 33 | :: 34 | 35 | if condition 36 | then 37 | command1 38 | command2 39 | ... 40 | commandN 41 | else 42 | command 43 | fi 44 | 45 | eg: 46 | 47 | :: 48 | 49 | if [ `ps -ef |grep /usr/sbin/sshd|grep -v grep|wc -l` -eq 0 ];then echo "sshd server exist";else echo "sshd server not exist";fi 50 | 51 | 1.3 多分支 52 | ~~~~~~~~~~ 53 | 54 | :: 55 | 56 | if condition1 57 | then 58 | command1 59 | elif condition2 60 | then 61 | command2 62 | else 63 | commandN 64 | fi 65 | 66 | eg: 67 | 68 | :: 69 | 70 | #! /bin/bash 71 | 72 | cmd=`rpm -q centos-release|cut -d- -f3` 73 | 74 | if [ $cmd -eq 6 ];then 75 | echo "sysversion is $cmd" 76 | elif [ $cmd -eq 7 ];then 77 | echo "sysversion is $cmd" 78 | else 79 | echo "sysversion is `rpm -q centos-release`" 80 | fi 81 | 82 | 二、for循环 83 | ----------- 84 | 85 | :: 86 | 87 | for var in item1 item2 ... itemN 88 | do 89 | command1 90 | command2 91 | ... 92 | commandN 93 | done 94 | 95 | eg1: 96 | 97 | :: 98 | 99 | for i in /*; 100 | do 101 | echo -e " \c"; 102 | find $i |wc -l|sort -nr; 103 | done 104 | 105 | eg2: 106 | 107 | :: 108 | 109 | #!/bin/bash 110 | for i in {1..3}; 111 | do 112 | echo $i 113 | done 114 | 115 | eg3: 116 | 117 | :: 118 | 119 | #!/bin/bash 120 | for i in "$@"; { # $@是将位置参数作为单个来处理 121 | echo $i 122 | } 123 | 124 | 默认 for 循环的取值列表是以空白符分隔,也就是第一章讲系统变量里的$IFS: 125 | 126 | :: 127 | 128 | #!/bin/bash 129 | OLD_IFS=$IFS 130 | IFS=":" 131 | for i in $(head -1 /etc/passwd); do 132 | echo $i 133 | done 134 | 135 | :: 136 | 137 | #!/bin/bash 138 | 139 | for ip in 192.168.1.{1..254}; do 140 | 141 | if ping -c 1 $ip >/dev/null; then 142 | 143 | echo "$ip OK." 144 | 145 | else 146 | 147 | echo "$ip NO!" 148 | 149 | fi 150 | 151 | done 152 | 153 | 读取文件,判断url可用性 154 | 155 | :: 156 | 157 | #!/bin/bash 158 | #function:check url 159 | filename=urllist.txt 160 | for url in $(cat $filename) 161 | do 162 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 163 | if [ $status == "200" ];then 164 | echo "Url:$url is ok!status is $status" 165 | else 166 | echo "Url:$url is error!status is $status" 167 | fi 168 | done 169 | 170 | 三、while语句 171 | ------------- 172 | 173 | 格式: 174 | 175 | :: 176 | 177 | while 条件表达式:do 178 | command 179 | done 180 | 181 | eg1: 182 | 183 | :: 184 | 185 | #!/bin/bash 186 | N=0 187 | while [ $N -lt 5 ]; do 188 | let N++ 189 | echo $N 190 | done 191 | 192 | 条件表达式为 true,将会产生死循环,利用此可以将脚本一直放在后台进行执行 193 | eg2: 194 | 195 | :: 196 | 197 | #!/bin/bash 198 | IP=10.75.128.8 199 | dir="/DATA/oracle/netdir/" 200 | if [ ! -d ${dir} ];then 201 | mkdir -p ${dir} 202 | fi 203 | echo 1 > ${dir}ping.lock 204 | while true 205 | do 206 | Time=`date +%F` 207 | TIME="${Time} 23:59" 208 | if [ "${data}" == "${TIME}" ];then 209 | mkdir ${dir}${Time} && mv ${dir}ping2.log ${dir}${Time}-ping2.log 210 | mv ${dir}${Time}-ping2.log ${dir}${Time} 211 | fi 212 | find ${dir} -mtime +7 -name "*-ping2.log" -exec rm -rf {} \; 213 | find ${dir} -mtime +7 -type d -exec rm -rf {} \; 214 | 215 | data=`date +%F' '%H:%M` 216 | data1=`date +%F' '%H:%M:%S` 217 | echo "------------${data1}---------------">>${dir}ping2.log 218 | ping -c 10 ${IP} >>${dir}ping2.log 219 | if [ $? -eq 1 ];then 220 | STAT=`cat ${dir}ping.lock` 221 | if [ ${STAT} -eq 1 ];then 222 | /usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1 223 | 50) ping 金融云(10.75.128.8)中断,请检查深信服VPN! \n TIME:${data1}" echo 0 > ${dir}ping.lock 224 | else 225 | continue 226 | fi 227 | else 228 | STAT=`cat ${dir}ping.lock` 229 | if [ ${STAT} -eq 0 ];then 230 | /usr/bin/python /DATA/oracle/netdir/GFweixin.py xuel GLP-VPN "GLP from PDC(172.16.6.1 231 | 50) ping 金融云(10.75.128.8)恢复! \n TIME:${data1}" echo 1 > ${dir}ping.lock 232 | else 233 | continue 234 | fi 235 | fi 236 | 237 | 238 | done 239 | 240 | 文件处理 241 | 242 | eg3: 243 | 244 | :: 245 | 246 | #!/bin/bash 247 | #function:check url 248 | filename=urllist.txt 249 | cat $filename | while read url;do 250 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 251 | if [ $status == "200" ];then 252 | echo "Url:$url is ok!status is $status" 253 | else 254 | echo "Url:$url is error!status is $status" 255 | fi 256 | done 257 | 258 | 或 259 | 260 | :: 261 | 262 | #!/bin/bash 263 | #function:check url 264 | filename=urllist.txt 265 | while read url; 266 | do 267 | status=`curl -I $url -s|awk '/HTTP/{print $2}'` 268 | if [ $status == "200" ];then 269 | echo "Url:$url is ok!status is ${status}" 270 | else 271 | echo "Url:$url is error!status is ${status}" 272 | fi 273 | done <$filename 274 | 275 | 四、break 和 continue 语句 276 | -------------------------- 277 | 278 | break跳出循环 279 | 280 | :: 281 | 282 | #!/bin/bash 283 | 284 | N=0 285 | while true; do 286 | let N++ 287 | if [ $N -eq 5 ]; then 288 | break 289 | fi 290 | echo $N 291 | done 292 | 293 | continue 294 | 295 | :: 296 | 297 | #!/bin/bash 298 | N=0 299 | while [ $N -lt 5 ]; do 300 | let N++ 301 | if [ $N -eq 3 ]; then 302 | continue 303 | fi 304 | echo $N 305 | done 306 | 307 | 五、case语句 308 | ------------ 309 | 310 | 语句 311 | 312 | :: 313 | 314 | case 模式名 in 315 | 模式 1) 316 | 命令 317 | ;; 318 | 模式 2) 319 | 命令 320 | ;; 321 | *) 322 | 不符合以上模式执行的命令 323 | esac 324 | 325 | eg 326 | 327 | :: 328 | 329 | #!/bin/bash 330 | case $1 in 331 | start) 332 | echo "start." 333 | ;; 334 | stop) 335 | echo "stop." 336 | ;; 337 | restart) 338 | echo "restart." 339 | ;; 340 | *) 341 | echo "Usage: $0 {start|stop|restart}" 342 | esac 343 | -------------------------------------------------------------------------------- /source/shell-05-函数.md: -------------------------------------------------------------------------------- 1 | # 五、函数 2 | 3 | ## 一、概念 4 | 5 | linux shell 可以用户定义函数,然后在shell脚本中可以随便调用,以此来重复调用公共函数,减少代码量。 6 | 7 | ## 二、格式 8 | 9 | ``` 10 | [ function ] funname() 11 | { 12 | action; 13 | [return int;] 14 | } 15 | ``` 16 | 17 | 说明: 18 | 19 | * function 关键字可写,也可不写。 20 | * 参数返回,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255),hell 函数返回值只能是整形数值,一般是用来表示函数执行成功与否的,0表示成功,其他值表示失败。因而用函数返回值来返回函数执行结果是不合适的。如果要硬生生地return某个计算结果,比如一个字符串,往往会得到错误提示:“numeric 21 | argument required”。 22 | 如果一定要让函数返回一个或多个值,可以定义全局变量,函数将计算结果赋给全局变量,然后脚本中其他地方通过访问全局变量,就可以获得那个函数“返回”的一个或多个执行结果了。 23 | 24 | ``` 25 | #!/bin/bash 26 | function output_data() { 27 | DATA=$((1+1)) 28 | return $DATA 29 | } 30 | output_data 31 | echo $? 32 | ``` 33 | 34 | ``` 35 | #!/bin/bash 36 | # function:add number 37 | function add_num() { 38 | echo "请输入第一个数:" 39 | read number01 40 | echo "请输入第二个数字" 41 | read number02 42 | if [[ "$number01" =~ ^[0-9]+$ ]] && [[ "$number02" =~ ^[0-9]+$ ]];then 43 | sum=$(($number01+$number02)) 44 | echo "$number01 + $number02 = $sum" 45 | else 46 | echo "input must be number" 47 | fi 48 | } 49 | add_num 50 | ``` 51 | 52 | ## 三、函数参数 53 | 54 | 将函数写成无状态的,将数据当做参数进行传入 55 | 56 | ``` 57 | #!/bin/bash 58 | funWithParam(){ 59 | echo "第一个参数为 $1 !" 60 | echo "第二个参数为 $2 !" 61 | echo "第十个参数为 $10 !" 62 | echo "第十个参数为 ${10} !" 63 | echo "第十一个参数为 ${11} !" 64 | echo "参数总数有 $# 个!" 65 | echo "作为一个字符串输出所有参数 $* !" 66 | echo "作为一个字符串输出所有参数 $@ !" 67 | 68 | } 69 | funWithParam `seq 1 20` 70 | 71 | ``` 72 | 73 | ``` 74 | ${1..n} 指定第n个输入的变量名称 75 | $0 脚本自身名字 76 | 77 | $? 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败 78 | 79 | $# 位置参数总数 80 | 81 | $* 所有的位置参数被看做一个字符串 82 | 83 | $@ 每个位置参数被看做独立的字符串 84 | 85 | $$ 当前进程 PID 86 | 87 | $! 上一条运行后台进程的 PID 88 | ``` 89 | 90 | eg:函数炸弹 91 | 92 | ``` 93 | :(){ :|:& };: 94 | ``` 95 | 96 | :|: 表示每次调用函数":"的时候就会生成两份拷贝。 97 | 98 | & 放到后台 99 | 100 | 递归调用自身,直至系统崩溃 101 | -------------------------------------------------------------------------------- /source/shell-05-函数.rst: -------------------------------------------------------------------------------- 1 | .. contents:: 2 | :depth: 3 3 | .. 4 | 5 | 五、函数 6 | ======== 7 | 8 | 一、概念 9 | -------- 10 | 11 | linux shell 12 | 可以用户定义函数,然后在shell脚本中可以随便调用,以此来重复调用公共函数,减少代码量。 13 | 14 | 二、格式 15 | -------- 16 | 17 | :: 18 | 19 | [ function ] funname() 20 | { 21 | action; 22 | [return int;] 23 | } 24 | 25 | 说明: 26 | 27 | - function 关键字可写,也可不写。 28 | - 参数返回,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。 29 | return后跟数值n(0-255),hell 30 | 函数返回值只能是整形数值,一般是用来表示函数执行成功与否的,0表示成功,其他值表示失败。因而用函数返回值来返回函数执行结果是不合适的。如果要硬生生地return某个计算结果,比如一个字符串,往往会得到错误提示:“numeric 31 | argument required”。 32 | 如果一定要让函数返回一个或多个值,可以定义全局变量,函数将计算结果赋给全局变量,然后脚本中其他地方通过访问全局变量,就可以获得那个函数“返回”的一个或多个执行结果了。 33 | 34 | :: 35 | 36 | #!/bin/bash 37 | function output_data() { 38 | DATA=$((1+1)) 39 | return $DATA 40 | } 41 | output_data 42 | echo $? 43 | 44 | :: 45 | 46 | #!/bin/bash 47 | # function:add number 48 | function add_num() { 49 | echo "请输入第一个数:" 50 | read number01 51 | echo "请输入第二个数字" 52 | read number02 53 | if [[ "$number01" =~ ^[0-9]+$ ]] && [[ "$number02" =~ ^[0-9]+$ ]];then 54 | sum=$(($number01+$number02)) 55 | echo "$number01 + $number02 = $sum" 56 | else 57 | echo "input must be number" 58 | fi 59 | } 60 | add_num 61 | 62 | 三、函数参数 63 | ------------ 64 | 65 | 将函数写成无状态的,将数据当做参数进行传入 66 | 67 | :: 68 | 69 | #!/bin/bash 70 | funWithParam(){ 71 | echo "第一个参数为 $1 !" 72 | echo "第二个参数为 $2 !" 73 | echo "第十个参数为 $10 !" 74 | echo "第十个参数为 ${10} !" 75 | echo "第十一个参数为 ${11} !" 76 | echo "参数总数有 $# 个!" 77 | echo "作为一个字符串输出所有参数 $* !" 78 | echo "作为一个字符串输出所有参数 $@ !" 79 | 80 | } 81 | funWithParam `seq 1 20` 82 | 83 | :: 84 | 85 | ${1..n} 指定第n个输入的变量名称 86 | $0 脚本自身名字 87 | 88 | $? 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败 89 | 90 | $# 位置参数总数 91 | 92 | $* 所有的位置参数被看做一个字符串 93 | 94 | $@ 每个位置参数被看做独立的字符串 95 | 96 | $$ 当前进程 PID 97 | 98 | $! 上一条运行后台进程的 PID 99 | 100 | eg:函数炸弹 101 | 102 | :: 103 | 104 | :(){ :|:& };: 105 | 106 | :|: 表示每次调用函数“:”的时候就会生成两份拷贝。 107 | 108 | & 放到后台 109 | 110 | 递归调用自身,直至系统崩溃 111 | -------------------------------------------------------------------------------- /source/shell-06-正则表达式.md: -------------------------------------------------------------------------------- 1 | 2 | # 六、正则表达式 3 | 4 | ## 一、基本正则表达式 5 | 6 | #### 1.1 字符匹配 7 | 8 | * .:匹配任意单个字符 9 | * []:匹配指定范围内的任意单个字符 10 | * \[^]:匹配指定范围外的任意单个字符 11 | * [:digit:]匹配元字符 12 | 13 | ``` 14 | posix字符 15 | [:alnum:] 字母数字[a-z A-Z 0-9] 16 | [:alpha:]字母[a-z A-Z] 17 | [:blank:]空格或制表键 18 | [:cntrl:] 任何控制字符 19 | [:digit:] 数字 [0-9] 20 | [:graph:] 任何可视字符(无空格) 21 | [:lower:] 小写 [a-z] 22 | [:print:] 非控制字符 23 | [:punct:] 标点字符 24 | [:space:] 空格 25 | [:upper:] 大写 [A-Z] 26 | [:xdigit:] 十六进制数字 [0-9 a-f A-F] 27 | ``` 28 | ``` 29 | 特殊字符 30 | \w 匹配任意数字和字母,等效[a-zA-Z0-9_] 31 | \W 和\w相反,等效[^a-zA-Z0-9_] 32 | \b 匹配字符串开始或结束,等效\<和\> 33 | \s 匹配任意的空白字符 34 | \S 匹配非空白字符 35 | ``` 36 | 37 | 38 | #### 1.2 次数匹配 39 | 40 | 用在制定的字符后面,表示制定前面的字符出现多少次 41 | * \*:匹配前面的字符任意次(0次获无数次) 42 | * \?:匹配前面的字符0次或1次 43 | * \+:匹配前面的字符至少1次 44 | * {m\,}:匹配前面的字符至少m次(默认工作在贪婪模式下,?取消贪婪模式) 45 | * {m,n}:匹配前面的字符至少m次,至多n次 46 | eg: 47 | 48 | ``` 49 | .*:匹配任意字符任意次数 50 | ``` 51 | 52 | #### 1.3 位置锚定 53 | 54 | * ^:行首锚定,用于模式最左边 55 | * $:行尾锚定,用于模式最右边 56 | * \\<或\b:锚定词首,用于单词模式左侧 57 | * \\>或\b:锚定词尾,用于单词模式右侧 58 | 59 | 60 | 61 | eg: 62 | 63 | ``` 64 | ^$:锚定空行 65 | ``` 66 | 67 | #### 1.4 分组引用 68 | 69 | 分组 70 | * \(\):将一个或多个字符当成一个整体来进行后续处理 71 | 72 | 引用 73 | * 1:从左侧起,引用第一个左括号以及与之匹配右括号之间的模式所匹配到的字符,后向引用 74 | 75 | 76 | exercises: 77 | 78 | ``` 79 | 1.显示/etc/init.d/functions文件中以大小s开头的行(使用两种方式) 80 | grep '^[Pp]' /etc/init.d/functions 81 | grep -i "^p" /etc/init.d/functions 82 | 83 | 2.显示/etc/passwd文件中不以/bin/bash结尾的行 84 | grep -v "/bin/bash$" /etc/passwd 85 | 86 | 3.显示/etc/passwd文件中ID号最大用户的用户名 87 | sort -t: -k3 -n /etc/passwd |tail -1 |cut -d: -f1 88 | 89 | 4.如果root用户存在,显示其默认的shell程序 90 | id root && grep '^\' /etc/passwd |awk -F: '{print $NF}' 91 | 92 | 5.找出/etc/passwd中的两位或三位数 93 | grep -o -E "[0-9]{2,3}" /etc/passwd 94 | grep -o "[0-9]\{2,3\}" /etc/passwd 95 | 96 | 6.显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存非空白字符的行: 97 | grep '^[[:space:]]\+[^[:space:]]' /etc/rc.d/rc.sysinit 98 | 99 | 100 | 7.找出"netstat -tan"命令的结果以"LISTEN"后跟0,1或多个空白字符结尾的行 101 | netstat -tan|grep 'LISTEN[[:space:]]*$' 102 | 103 | 8.如果root用户登录了系统,就显示root用户在线,否则说明未登录 104 | w |grep '^\'>/dev/null && echo "root在线"|| echo "root未登录" 105 | 106 | 9.找出/etc/rc.d/init.d/functions文件中某单词后面跟一对小括号的行 107 | grep '[[:alpha:]]*()' /etc/rc.d/init.d/functions 108 | 109 | 10.使用echo输出一个路径,使用egrep取出基名 110 | echo /tmp/tmp1/vmstat.8.gz |grep -E -o '[^/]+/?$'|cut -d/ -f1 111 | echo /tmp/tmp1/vmstat.8.gz |awk -F'/' '{print $NF}' 112 | 113 | 11.匹配PPID开头,行中又再次出现PPID的内容。/etc/init.d/functions 114 | grep -E "(PPID).*\1" /etc/init.d/functions 115 | 116 | 12.利用awk找出/etc/ssh/sshd_config内出过空行与以#开头的行 117 | awk '!/^#/ && !/^$/{print}' /etc/ssh/sshd_config 118 | grep -v -E '^#|^$' /etc/ssh/sshd_config 119 | 120 | 121 | ``` -------------------------------------------------------------------------------- /source/shell-06-正则表达式.rst: -------------------------------------------------------------------------------- 1 | .. role:: raw-latex(raw) 2 | :format: latex 3 | .. 4 | 5 | .. contents:: 6 | :depth: 3 7 | .. 8 | 9 | 六、正则表达式 10 | ============== 11 | 12 | 一、基本正则表达式 13 | ------------------ 14 | 15 | 1.1 字符匹配 16 | ~~~~~~~~~~~~ 17 | 18 | - .:匹配任意单个字符 19 | - 20 | - [^]:匹配指定范围外的任意单个字符 21 | - [:digit:]匹配元字符 22 | 23 | :: 24 | 25 | posix字符 26 | [:alnum:] 字母数字[a-z A-Z 0-9] 27 | [:alpha:]字母[a-z A-Z] 28 | [:blank:]空格或制表键 29 | [:cntrl:] 任何控制字符 30 | [:digit:] 数字 [0-9] 31 | [:graph:] 任何可视字符(无空格) 32 | [:lower:] 小写 [a-z] 33 | [:print:] 非控制字符 34 | [:punct:] 标点字符 35 | [:space:] 空格 36 | [:upper:] 大写 [A-Z] 37 | [:xdigit:] 十六进制数字 [0-9 a-f A-F] 38 | 39 | :: 40 | 41 | 特殊字符 42 | \w 匹配任意数字和字母,等效[a-zA-Z0-9_] 43 | \W 和\w相反,等效[^a-zA-Z0-9_] 44 | \b 匹配字符串开始或结束,等效\<和\> 45 | \s 匹配任意的空白字符 46 | \S 匹配非空白字符 47 | 48 | 1.2 次数匹配 49 | ~~~~~~~~~~~~ 50 | 51 | 用在制定的字符后面,表示制定前面的字符出现多少次 \* 52 | \*:匹配前面的字符任意次(0次获无数次) \* ?:匹配前面的字符0次或1次 \* 53 | +:匹配前面的字符至少1次 \* 54 | {m,}:匹配前面的字符至少m次(默认工作在贪婪模式下,?取消贪婪模式) \* 55 | {m,n}:匹配前面的字符至少m次,至多n次 eg: 56 | 57 | :: 58 | 59 | .*:匹配任意字符任意次数 60 | 61 | 1.3 位置锚定 62 | ~~~~~~~~~~~~ 63 | 64 | - ^:行首锚定,用于模式最左边 65 | - $:行尾锚定,用于模式最右边 66 | - \\<或:raw-latex:`\b`:锚定词首,用于单词模式左侧 67 | - \\>或:raw-latex:`\b`:锚定词尾,用于单词模式右侧 68 | 69 | eg: 70 | 71 | :: 72 | 73 | ^$:锚定空行 74 | 75 | 1.4 分组引用 76 | ~~~~~~~~~~~~ 77 | 78 | 分组 \* ():将一个或多个字符当成一个整体来进行后续处理 79 | 80 | 引用 \* 81 | 1:从左侧起,引用第一个左括号以及与之匹配右括号之间的模式所匹配到的字符,后向引用 82 | 83 | exercises: 84 | 85 | :: 86 | 87 | 1.显示/etc/init.d/functions文件中以大小s开头的行(使用两种方式) 88 | grep '^[Pp]' /etc/init.d/functions 89 | grep -i "^p" /etc/init.d/functions 90 | 91 | 2.显示/etc/passwd文件中不以/bin/bash结尾的行 92 | grep -v "/bin/bash$" /etc/passwd 93 | 94 | 3.显示/etc/passwd文件中ID号最大用户的用户名 95 | sort -t: -k3 -n /etc/passwd |tail -1 |cut -d: -f1 96 | 97 | 4.如果root用户存在,显示其默认的shell程序 98 | id root && grep '^\' /etc/passwd |awk -F: '{print $NF}' 99 | 100 | 5.找出/etc/passwd中的两位或三位数 101 | grep -o -E "[0-9]{2,3}" /etc/passwd 102 | grep -o "[0-9]\{2,3\}" /etc/passwd 103 | 104 | 6.显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存非空白字符的行: 105 | grep '^[[:space:]]\+[^[:space:]]' /etc/rc.d/rc.sysinit 106 | 107 | 108 | 7.找出"netstat -tan"命令的结果以"LISTEN"后跟0,1或多个空白字符结尾的行 109 | netstat -tan|grep 'LISTEN[[:space:]]*$' 110 | 111 | 8.如果root用户登录了系统,就显示root用户在线,否则说明未登录 112 | w |grep '^\'>/dev/null && echo "root在线"|| echo "root未登录" 113 | 114 | 9.找出/etc/rc.d/init.d/functions文件中某单词后面跟一对小括号的行 115 | grep '[[:alpha:]]*()' /etc/rc.d/init.d/functions 116 | 117 | 10.使用echo输出一个路径,使用egrep取出基名 118 | echo /tmp/tmp1/vmstat.8.gz |grep -E -o '[^/]+/?$'|cut -d/ -f1 119 | echo /tmp/tmp1/vmstat.8.gz |awk -F'/' '{print $NF}' 120 | 121 | 11.匹配PPID开头,行中又再次出现PPID的内容。/etc/init.d/functions 122 | grep -E "(PPID).*\1" /etc/init.d/functions 123 | 124 | 12.利用awk找出/etc/ssh/sshd_config内出过空行与以#开头的行 125 | awk '!/^#/ && !/^$/{print}' /etc/ssh/sshd_config 126 | grep -v -E '^#|^$' /etc/ssh/sshd_config 127 | -------------------------------------------------------------------------------- /source/shell-07-三剑客-grep.md: -------------------------------------------------------------------------------- 1 | # 七、三剑客之grep 2 | 3 | ## 一、概念 4 | 5 | grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。 6 | 7 | ## 二、语法格式 8 | 9 | grep [OPTION]... PATTERN [FILE] 10 | 11 | #### 2.1 选项 12 | 13 | * -i:忽略大小写 14 | * -c:统计匹配到字符串的次数 15 | * -n:顺便输出行号 16 | * -v:反向选择,显示没有匹配到的内容 17 | * -o:只显示匹配到的串 18 | * -A:显示匹配到的字符后面的n行 19 | * -B:显示匹配到的字符前面的n行 20 | * -C:显示前后各n行 21 | * 22 | #### 2.2 模式 23 | 24 | * 基本正则表达式元字符: 25 | 26 | ``` 27 | . :匹配任意单个字符 28 | [] :匹配指定范围内的字符 29 | [^]:匹配指定范围外的任意字符 30 | ``` 31 | 32 | * 次数匹配(贪婪模式) 33 | 34 | ``` 35 | *:匹配其前的字符0,1或者多次 36 | ?:匹配其前的字符0或1次 37 | \{m,n\}: 38 | \(m,\):至少m次 39 | \{0,n\}:至多n次 40 | \{m\}:m次 41 | ``` 42 | 43 | * 锚定符: 44 | 45 | ``` 46 | r..t :root chroot 47 | * 单词锚定: 48 | \<:锚定词首:\:锚定词尾:root\> 50 | * 行首行末锚定: 51 | ^: ^root, 行首 52 | $: root$ 行尾 53 | .*: 任意长度的任意字符 54 | ``` 55 | 56 | * 分组: 57 | \(\) 58 | \(abc\) 59 | * 引用: 60 | 61 | ``` 62 | \1 :后向引用,引用前面的第一个左括号与与之对应的右括号中的模式所匹配到的内容 63 | 64 | ``` 65 | 66 | eg: 67 | 68 | ``` 69 | dmesg |grep -n eth0 70 | grep -E '/.{2,3}' /etc/passwd 71 | ``` 72 | 73 | 取掉空行 74 | 75 | ``` 76 | grep -E -v "^$|^#" /etc/httpd/conf/httpd.conf 77 | ``` 78 | 79 | 查看mysql中的库 80 | 81 | ``` 82 | $(mysql -uroot -p'passwd' -e "show databases;"|egrep -v 'Database|^test|mysql|performance_schema|information_schema') 83 | ``` -------------------------------------------------------------------------------- /source/shell-07-三剑客-grep.rst: -------------------------------------------------------------------------------- 1 | .. contents:: 2 | :depth: 3 3 | .. 4 | 5 | 七、三剑客之grep 6 | ================ 7 | 8 | 一、概念 9 | -------- 10 | 11 | grep (global search regular expression(RE) and print out the 12 | line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。egrep是grep的扩展,支持更多的re元字符, 13 | fgrep就是fixed grep或fast 14 | grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。 15 | 16 | 二、语法格式 17 | ------------ 18 | 19 | grep [OPTION]… PATTERN [FILE] 20 | 21 | 2.1 选项 22 | ~~~~~~~~ 23 | 24 | - -i:忽略大小写 25 | - -c:统计匹配到字符串的次数 26 | - -n:顺便输出行号 27 | - -v:反向选择,显示没有匹配到的内容 28 | - -o:只显示匹配到的串 29 | - -A:显示匹配到的字符后面的n行 30 | - -B:显示匹配到的字符前面的n行 31 | - -C:显示前后各n行 32 | - .. rubric:: 2.2 模式 33 | :name: 模式 34 | 35 | - 基本正则表达式元字符: 36 | 37 | :: 38 | 39 | . :匹配任意单个字符 40 | [] :匹配指定范围内的字符 41 | [^]:匹配指定范围外的任意字符 42 | 43 | - 次数匹配(贪婪模式) 44 | 45 | :: 46 | 47 | *:匹配其前的字符0,1或者多次 48 | ?:匹配其前的字符0或1次 49 | \{m,n\}: 50 | \(m,\):至少m次 51 | \{0,n\}:至多n次 52 | \{m\}:m次 53 | 54 | - 锚定符: 55 | 56 | :: 57 | 58 | r..t :root chroot 59 | * 单词锚定: 60 | \<:锚定词首:\:锚定词尾:root\> 62 | * 行首行末锚定: 63 | ^: ^root, 行首 64 | $: root$ 行尾 65 | .*: 任意长度的任意字符 66 | 67 | - 分组: () (abc) 68 | - 引用: 69 | 70 | :: 71 | 72 | \1 :后向引用,引用前面的第一个左括号与与之对应的右括号中的模式所匹配到的内容 73 | 74 | eg: 75 | 76 | :: 77 | 78 | dmesg |grep -n eth0 79 | grep -E '/.{2,3}' /etc/passwd 80 | 81 | 取掉空行 82 | 83 | :: 84 | 85 | grep -E -v "^$|^#" /etc/httpd/conf/httpd.conf 86 | 87 | 查看mysql中的库 88 | 89 | :: 90 | 91 | $(mysql -uroot -p'passwd' -e "show databases;"|egrep -v 'Database|^test|mysql|performance_schema|information_schema') 92 | -------------------------------------------------------------------------------- /source/shell-08-三剑客-sed.md: -------------------------------------------------------------------------------- 1 | # 八、三剑客之sed 2 | 3 | ## 一、概念: 4 | 5 | sed是一种流编辑的文本处理工具, 6 | * 工作模式:将当前处理的行存储在临时缓冲区(模式空间),对缓冲区中的内容利用制定的动作进行处理,完成后输出到屏幕,接着反复重复执行此操作完成整改文件的处理。 7 | 8 | ## 二、适用场景 9 | 10 | * 大文件 11 | * 有规律的文本 12 | 13 | ## 三、语法 14 | 15 | sed [option] 'Addresscommand' [file ...] 16 | 17 | 18 | * 选项 19 | 20 | ```` 21 | * -n:安静模式,仅显示script处理后的结果,不再默认显示模式空间中的内容 22 | * -e: