From a786dd2b37770699d2f8afbf4dfd6c8f0230162d Mon Sep 17 00:00:00 2001 From: Zhang Peng Date: Fri, 10 May 2019 11:17:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 118 ++++++--- codes/linux/ops/tool/Autoinstall_ELK_V1.3.sh | 164 ++++++++++++ codes/linux/ops/tool/Cpu_Limit.sh | 53 ++++ codes/linux/ops/tool/Custom_Rm.sh | 26 ++ codes/linux/ops/tool/Daily_Archive.sh | 66 +++++ codes/linux/ops/tool/Hourly_Archive.sh | 73 ++++++ codes/linux/ops/tool/Sys_Check.sh | 225 ++++++++++++++++ codes/linux/ops/tool/install_elasticserch.sh | 194 ++++++++++++++ codes/linux/ops/tool/install_filebeat.sh | 136 ++++++++++ codes/linux/ops/tool/install_git.sh | 155 +++++++++++ codes/linux/ops/tool/install_grafana.sh | 110 ++++++++ codes/linux/ops/tool/install_kafka.sh | 139 ++++++++++ codes/linux/ops/tool/install_kibana.sh | 134 ++++++++++ codes/linux/ops/tool/install_logstash.sh | 141 ++++++++++ codes/linux/ops/tool/install_maven.sh | 125 +++++++++ codes/linux/ops/tool/install_tomcat.sh | 126 +++++++++ codes/linux/ops/tool/install_zookeeper.sh | 137 ++++++++++ codes/linux/ops/tool/mgitstatus.sh | 246 +++++++++++++++++ codes/linux/ops/tool/删除用户脚本.sh | 247 ++++++++++++++++++ .../tool/查看磁盘目录使用情况.sh | 31 +++ .../mysql/向数据库中插入数据.sh | 21 ++ .../mysql/格式化输出数据.sh | 18 ++ .../mysql/连接数据库并发送命令.sh | 13 + .../示例脚本/基本脚本/exit命令.sh | 8 + .../使用expr执行数学运算.sh | 8 + .../使用内联重定向计算表达式.sh | 15 ++ .../使用方括号执行数学运算.sh | 7 + .../基本脚本/使用自定义变量.sh | 9 + .../基本脚本/反引号的使用.sh | 5 + .../基本脚本/在脚本中使用bc.sh | 6 + .../基本脚本/显示时间和登录者.sh | 12 + .../显示系统变量和转义字符.sh | 10 + ...得当前日期并生成唯一文件名.sh | 5 + .../示例脚本/控制/定时执行脚本.sh | 5 + .../shell/示例脚本/控制/捕捉信号.sh | 16 ++ .../控制/捕捉脚本的退出.sh | 13 + .../shell/示例脚本/控制/移除捕捉.sh | 16 ++ .../查找替换文本/gawk/gawk.sh | 19 ++ .../查找替换文本/gawk/gawk函数库 | 12 + .../查找替换文本/gawk/gawk脚本 | 6 + .../查找替换文本/gawk/script | 2 + .../示例脚本/查找替换文本/gawk/test | 4 + .../查找替换文本/gawk/使用变量.sh | 47 ++++ .../gawk/使用模式,结构化命令.sh | 57 ++++ .../gawk/自定义函数.sh | 12 + .../gawk/调用函数库和脚本.sh | 5 + .../查找替换文本/gawk/输出.sh | 66 +++++ .../regex/目录文件计数.sh | 18 ++ .../查找替换文本/regex/邮件验证.sh | 7 + .../查找替换文本/sed/sed文件操作.sh | 24 ++ .../sed/sed编辑器基础.sh | 104 ++++++++ .../示例脚本/查找替换文本/sed/test | 5 + .../查找替换文本/sed/保持空间.sh | 16 ++ ...指定的空白行和删除html标签.sh | 21 ++ .../sed/在脚本中使用sed.sh | 8 + .../查找替换文本/sed/排除命令.sh | 8 + .../查找替换文本/sed/模式替代.sh | 11 + .../查找替换文本/sed/测试.sh | 4 + .../sed/给文件中的行编号.sh | 3 + .../查找替换文本/sed/跳转.sh | 7 + .../sed/输出末尾指定行数的数据.sh | 10 + .../sed/重定向sed输出.sh | 21 ++ .../脚本函数/从函数返回数组.sh | 25 ++ .../脚本函数/使用return命令.sh | 12 + .../使用全局变量带来的问题.sh | 21 ++ .../脚本函数/使用函数输出.sh | 11 + .../使用命令行中传递的参数.sh | 16 ++ .../脚本函数/使用局部变量.sh | 21 ++ .../脚本函数/使用库函数.sh | 8 + .../示例脚本/脚本函数/全局变量.sh | 12 + .../示例脚本/脚本函数/函数递归.sh | 16 ++ .../脚本函数/在函数中使用参数.sh | 32 +++ .../脚本函数/基本的脚本函数.sh | 17 ++ .../脚本函数/想函数传数组数据.sh | 23 ++ .../脚本函数/累加数组中的值.sh | 20 ++ .../示例脚本/脚本函数/脚本库.sh | 20 ++ .../脚本函数/默认退出状态码.sh | 52 ++++ .../示例脚本/菜单/使用msgbox部件.sh | 3 + .../示例脚本/菜单/使用select命令.sh | 36 +++ .../示例脚本/菜单/使用脚本菜单.sh | 49 ++++ .../菜单/在脚本中使用dialog命令.sh | 47 ++++ codes/shell/示例脚本/输入和输出/test | 2 + .../shell/示例脚本/输入和输出/test1 | 1 + .../输入和输出/临时重定向.sh | 6 + .../从文件中读取数据.sh | 11 + .../输入和输出/使用getopts.sh | 12 + .../使用getopts处理选项和参数.sh | 20 ++ .../输入和输出/使用getopt命令.sh | 25 ++ .../输入和输出/使用shift命令.sh | 17 ++ .../输入和输出/分离参数和选项.sh | 23 ++ ...出当前脚本打开的文件描述符.sh | 14 + .../输入和输出/创建临时目录.sh | 16 ++ .../创建本地临时文件.sh | 22 ++ ...以重定向的文件描述符中恢复.sh | 13 + .../关闭文件描述符.sh | 16 ++ .../创建读写文件描述符.sh | 7 + .../创建输入文件描述符.sh | 22 ++ .../创建输出文件描述符.sh | 9 + .../输入和输出/参数计数.sh | 10 + .../在tmp目录创建临时文件.sh | 12 + .../在脚本中使用重定向输入.sh | 12 + .../输入和输出/处理带值的选项.sh | 25 ++ .../输入和输出/处理简单选项.sh | 13 + .../快速清除文件或日志.sh | 5 + .../输入和输出/抓取所有数据.sh | 16 ++ .../输入和输出/永久重定向.sh | 14 + .../示例脚本/输入和输出/测试.txt | 1 + .../输入和输出/获取用户输入.sh | 27 ++ .../输入和输出/记录信息.sh | 10 + .../输入和输出/读取参数.sh | 10 + .../读取多个命令行参数.sh | 8 + .../输入和输出/读取程序名.sh | 9 + .../输入和输出/超时和输入计数.sh | 22 ++ .../隐藏方式读取数据.sh | 8 + .../进阶脚本/创建捕捉脚本.sh | 24 ++ .../查看uptime获取在线用户数.sh | 3 + .../进阶脚本/查看僵尸进程.sh | 4 + .../查看内存使用百分比.sh | 4 + .../查看磁盘使用百分比.sh | 4 + ...报告脚本-基于创建捕捉脚本.sh | 45 ++++ .../进阶脚本/系统快照报告.sh | 92 +++++++ .../示例脚本/进阶脚本/输出颜色.sh | 113 ++++++++ .../问题跟踪数据库/Update_Problem.sh | 82 ++++++ .../问题跟踪数据库/查找问题.sh | 41 +++ .../问题跟踪数据库/记录问题.sh | 65 +++++ .../bash-shell无法处理浮点数.sh | 11 + .../continue退出多层循环.sh | 16 ++ .../逻辑控制/if-then-else语句.sh | 10 + .../逻辑控制/if-then语句.sh | 12 + .../示例脚本/逻辑控制/output.txt | 12 + .../while里面嵌套for循环.sh | 16 ++ .../逻辑控制/两个for嵌套循环.sh | 11 + .../逻辑控制/从变量读取列表.sh | 11 + .../逻辑控制/从命令读取值.sh | 18 ++ .../使用C语言风格的for命令.sh | 7 + .../使用break跳出外部循环.sh | 16 ++ .../逻辑控制/使用case语句.sh | 15 ++ .../逻辑控制/使用continue命令.sh | 12 + .../示例脚本/逻辑控制/使用elif.sh | 19 ++ .../使用test进行数值比较.sh | 15 ++ .../逻辑控制/使用until命令.sh | 21 ++ .../示例脚本/逻辑控制/使用while.sh | 10 + .../使用while多个测试命令.sh | 11 + .../逻辑控制/使用双圆括号.sh | 11 + ...引号圈起用空格分隔的字符串.sh | 7 + .../逻辑控制/使用双方括号.sh | 9 + .../使用嵌套循环并修改IFS.sh | 15 ++ .../逻辑控制/使用符合条件测试.sh | 9 + .../逻辑控制/使用管道或重定向.sh | 20 ++ .../使用通配符处理目录.sh | 12 + .../判断字符串长度是否为零.sh | 29 ++ .../在for语句中使用多个变量.sh | 7 + .../在then块中使用多条命令.sh | 8 + .../逻辑控制/字符串比较.sh | 9 + .../示例脚本/逻辑控制/嵌套if.sh | 2 + .../检查文件或目录是否存在.sh | 20 ++ .../示例脚本/逻辑控制/检查目录.sh | 11 + .../正确使用大于小于号.sh | 12 + .../注意test大小写顺序和sort不同.sh | 12 + .../解决读取列表中的复杂值.sh | 6 + .../逻辑控制/读取列表中的值.sh | 7 + .../读取里表中复杂的值.sh | 8 + .../错误的使用大于小于号.sh | 14 + docs/linux/README.md | 2 +- docs/linux/soft/mongodb-install.md | 49 ++++ docs/linux/soft/redis-install.md | 58 ++++ 166 files changed, 5223 insertions(+), 43 deletions(-) create mode 100644 codes/linux/ops/tool/Autoinstall_ELK_V1.3.sh create mode 100644 codes/linux/ops/tool/Cpu_Limit.sh create mode 100644 codes/linux/ops/tool/Custom_Rm.sh create mode 100644 codes/linux/ops/tool/Daily_Archive.sh create mode 100644 codes/linux/ops/tool/Hourly_Archive.sh create mode 100644 codes/linux/ops/tool/Sys_Check.sh create mode 100644 codes/linux/ops/tool/install_elasticserch.sh create mode 100644 codes/linux/ops/tool/install_filebeat.sh create mode 100644 codes/linux/ops/tool/install_git.sh create mode 100644 codes/linux/ops/tool/install_grafana.sh create mode 100644 codes/linux/ops/tool/install_kafka.sh create mode 100644 codes/linux/ops/tool/install_kibana.sh create mode 100644 codes/linux/ops/tool/install_logstash.sh create mode 100644 codes/linux/ops/tool/install_maven.sh create mode 100644 codes/linux/ops/tool/install_tomcat.sh create mode 100644 codes/linux/ops/tool/install_zookeeper.sh create mode 100644 codes/linux/ops/tool/mgitstatus.sh create mode 100644 codes/linux/ops/tool/删除用户脚本.sh create mode 100644 codes/linux/ops/tool/查看磁盘目录使用情况.sh create mode 100644 codes/shell/示例脚本/mysql/向数据库中插入数据.sh create mode 100644 codes/shell/示例脚本/mysql/格式化输出数据.sh create mode 100644 codes/shell/示例脚本/mysql/连接数据库并发送命令.sh create mode 100644 codes/shell/示例脚本/基本脚本/exit命令.sh create mode 100644 codes/shell/示例脚本/基本脚本/使用expr执行数学运算.sh create mode 100644 codes/shell/示例脚本/基本脚本/使用内联重定向计算表达式.sh create mode 100644 codes/shell/示例脚本/基本脚本/使用方括号执行数学运算.sh create mode 100644 codes/shell/示例脚本/基本脚本/使用自定义变量.sh create mode 100644 codes/shell/示例脚本/基本脚本/反引号的使用.sh create mode 100644 codes/shell/示例脚本/基本脚本/在脚本中使用bc.sh create mode 100644 codes/shell/示例脚本/基本脚本/显示时间和登录者.sh create mode 100644 codes/shell/示例脚本/基本脚本/显示系统变量和转义字符.sh create mode 100644 codes/shell/示例脚本/基本脚本/通过反引号获得当前日期并生成唯一文件名.sh create mode 100644 codes/shell/示例脚本/控制/定时执行脚本.sh create mode 100644 codes/shell/示例脚本/控制/捕捉信号.sh create mode 100644 codes/shell/示例脚本/控制/捕捉脚本的退出.sh create mode 100644 codes/shell/示例脚本/控制/移除捕捉.sh create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/gawk.sh create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/gawk函数库 create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/gawk脚本 create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/script create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/test create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/使用变量.sh create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/使用模式,结构化命令.sh create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/自定义函数.sh create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/调用函数库和脚本.sh create mode 100644 codes/shell/示例脚本/查找替换文本/gawk/输出.sh create mode 100644 codes/shell/示例脚本/查找替换文本/regex/目录文件计数.sh create mode 100644 codes/shell/示例脚本/查找替换文本/regex/邮件验证.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/sed文件操作.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/sed编辑器基础.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/test create mode 100644 codes/shell/示例脚本/查找替换文本/sed/保持空间.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/删除指定的空白行和删除html标签.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/在脚本中使用sed.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/排除命令.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/模式替代.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/测试.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/给文件中的行编号.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/跳转.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/输出末尾指定行数的数据.sh create mode 100644 codes/shell/示例脚本/查找替换文本/sed/重定向sed输出.sh create mode 100644 codes/shell/示例脚本/脚本函数/从函数返回数组.sh create mode 100644 codes/shell/示例脚本/脚本函数/使用return命令.sh create mode 100644 codes/shell/示例脚本/脚本函数/使用全局变量带来的问题.sh create mode 100644 codes/shell/示例脚本/脚本函数/使用函数输出.sh create mode 100644 codes/shell/示例脚本/脚本函数/使用命令行中传递的参数.sh create mode 100644 codes/shell/示例脚本/脚本函数/使用局部变量.sh create mode 100644 codes/shell/示例脚本/脚本函数/使用库函数.sh create mode 100644 codes/shell/示例脚本/脚本函数/全局变量.sh create mode 100644 codes/shell/示例脚本/脚本函数/函数递归.sh create mode 100644 codes/shell/示例脚本/脚本函数/在函数中使用参数.sh create mode 100644 codes/shell/示例脚本/脚本函数/基本的脚本函数.sh create mode 100644 codes/shell/示例脚本/脚本函数/想函数传数组数据.sh create mode 100644 codes/shell/示例脚本/脚本函数/累加数组中的值.sh create mode 100644 codes/shell/示例脚本/脚本函数/脚本库.sh create mode 100644 codes/shell/示例脚本/脚本函数/默认退出状态码.sh create mode 100644 codes/shell/示例脚本/菜单/使用msgbox部件.sh create mode 100644 codes/shell/示例脚本/菜单/使用select命令.sh create mode 100644 codes/shell/示例脚本/菜单/使用脚本菜单.sh create mode 100644 codes/shell/示例脚本/菜单/在脚本中使用dialog命令.sh create mode 100644 codes/shell/示例脚本/输入和输出/test create mode 100644 codes/shell/示例脚本/输入和输出/test1 create mode 100644 codes/shell/示例脚本/输入和输出/临时重定向.sh create mode 100644 codes/shell/示例脚本/输入和输出/从文件中读取数据.sh create mode 100644 codes/shell/示例脚本/输入和输出/使用getopts.sh create mode 100644 codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh create mode 100644 codes/shell/示例脚本/输入和输出/使用getopt命令.sh create mode 100644 codes/shell/示例脚本/输入和输出/使用shift命令.sh create mode 100644 codes/shell/示例脚本/输入和输出/分离参数和选项.sh create mode 100644 codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建临时目录.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建本地临时文件.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh create mode 100644 codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输出文件描述符.sh create mode 100644 codes/shell/示例脚本/输入和输出/参数计数.sh create mode 100644 codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh create mode 100644 codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh create mode 100644 codes/shell/示例脚本/输入和输出/处理带值的选项.sh create mode 100644 codes/shell/示例脚本/输入和输出/处理简单选项.sh create mode 100644 codes/shell/示例脚本/输入和输出/快速清除文件或日志.sh create mode 100644 codes/shell/示例脚本/输入和输出/抓取所有数据.sh create mode 100644 codes/shell/示例脚本/输入和输出/永久重定向.sh create mode 100644 codes/shell/示例脚本/输入和输出/测试.txt create mode 100644 codes/shell/示例脚本/输入和输出/获取用户输入.sh create mode 100644 codes/shell/示例脚本/输入和输出/记录信息.sh create mode 100644 codes/shell/示例脚本/输入和输出/读取参数.sh create mode 100644 codes/shell/示例脚本/输入和输出/读取多个命令行参数.sh create mode 100644 codes/shell/示例脚本/输入和输出/读取程序名.sh create mode 100644 codes/shell/示例脚本/输入和输出/超时和输入计数.sh create mode 100644 codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh create mode 100644 codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh create mode 100644 codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh create mode 100644 codes/shell/示例脚本/进阶脚本/查看僵尸进程.sh create mode 100644 codes/shell/示例脚本/进阶脚本/查看内存使用百分比.sh create mode 100644 codes/shell/示例脚本/进阶脚本/查看磁盘使用百分比.sh create mode 100644 codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh create mode 100644 codes/shell/示例脚本/进阶脚本/系统快照报告.sh create mode 100644 codes/shell/示例脚本/进阶脚本/输出颜色.sh create mode 100644 codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh create mode 100644 codes/shell/示例脚本/进阶脚本/问题跟踪数据库/查找问题.sh create mode 100644 codes/shell/示例脚本/进阶脚本/问题跟踪数据库/记录问题.sh create mode 100644 codes/shell/示例脚本/逻辑控制/bash-shell无法处理浮点数.sh create mode 100644 codes/shell/示例脚本/逻辑控制/continue退出多层循环.sh create mode 100644 codes/shell/示例脚本/逻辑控制/if-then-else语句.sh create mode 100644 codes/shell/示例脚本/逻辑控制/if-then语句.sh create mode 100644 codes/shell/示例脚本/逻辑控制/output.txt create mode 100644 codes/shell/示例脚本/逻辑控制/while里面嵌套for循环.sh create mode 100644 codes/shell/示例脚本/逻辑控制/两个for嵌套循环.sh create mode 100644 codes/shell/示例脚本/逻辑控制/从变量读取列表.sh create mode 100644 codes/shell/示例脚本/逻辑控制/从命令读取值.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用C语言风格的for命令.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用break跳出外部循环.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用case语句.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用continue命令.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用elif.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用test进行数值比较.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用until命令.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用while.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用while多个测试命令.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用双圆括号.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用双方括号.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh create mode 100644 codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh create mode 100644 codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh create mode 100644 codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh create mode 100644 codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh create mode 100644 codes/shell/示例脚本/逻辑控制/字符串比较.sh create mode 100644 codes/shell/示例脚本/逻辑控制/嵌套if.sh create mode 100644 codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh create mode 100644 codes/shell/示例脚本/逻辑控制/检查目录.sh create mode 100644 codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh create mode 100644 codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh create mode 100644 codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh create mode 100644 codes/shell/示例脚本/逻辑控制/读取列表中的值.sh create mode 100644 codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh create mode 100644 codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh create mode 100644 docs/linux/soft/mongodb-install.md create mode 100644 docs/linux/soft/redis-install.md diff --git a/README.md b/README.md index 304dfca..87dc1da 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,8 @@ # Linux 教程 -## [Linux 命令](docs/linux/cli) +## :shell: 脚本 -> 根据应用场景,将常见 Linux 命令分门别类的一一介绍。 -> -> 如果想快速学习,推荐参考这篇文章:[命令行的艺术(转载)](docs/linux/cli/命令行的艺术.md) - -1. [查看 Linux 命令帮助信息](docs/linux/cli/01.查看Linux命令帮助信息.md) - 关键词:`help`, `whatis`, `info`, `which`, `whereis`, `man` -2. [Linux 文件目录管理](docs/linux/cli/02.Linux文件目录管理.md) - 关键词:`cd`, `ls`, `pwd`, `mkdir`, `rmdir`, `tree`, `touch`, `ln`, `rename`, `stat`, `file`, `chmod`, `chown`, `locate`, `find`, `cp`, `mv`, `rm` -3. [Linux 文件内容查看命令](docs/linux/cli/03.Linux文件内容查看编辑.md) - 关键词:`cat`, `head`, `tail`, `more`, `less`, `sed`, `vi`, `grep` -4. [Linux 文件压缩和解压](docs/linux/cli/04.Linux文件压缩和解压.md) - 关键词:`tar`, `gzip`, `zip`, `unzip` -5. [Linux 用户管理](docs/linux/cli/05.Linux用户管理.md) - 关键词:`groupadd`, `groupdel`, `groupmod`, `useradd`, `userdel`, `usermod`, `passwd`, `su`, `sudo` -6. [Linux 系统管理](docs/linux/cli/06.Linux系统管理.md) - 关键词:`reboot`, `exit`, `shutdown`, `date`, `mount`, `umount`, `ps`, `kill`, `systemctl`, `service`, `crontab` -7. [Linux 网络管理](docs/linux/cli/07.Linux网络管理.md) - 关键词:关键词:`curl`, `wget`, `telnet`, `ip`, `hostname`, `ifconfig`, `route`, `ssh`, `ssh-keygen`, `firewalld`, `iptables`, `host`, `nslookup`, `nc`/`netcat`, `ping`, `traceroute`, `netstat` -8. [Linux 硬件管理](docs/linux/cli/08.Linux硬件管理.md) - 关键词:`df`, `du`, `top`, `free`, `iotop` -9. [Linux 软件管理](docs/linux/cli/09.Linux软件管理.md) - 关键词:`rpm`, `yum`, `apt-get` - -## [Linux 工具](docs/linux/tool) - -- [Git](docs/linux/tool/git) -- [Vim](docs/linux/tool/vim.md) - -## [Linux 脚本](docs/lang) - -- [一篇文章让你彻底掌握 shell 语言](docs/lang/shell.md) -- [一篇文章让你彻底掌握 python 语言](docs/lang/python.md) - -## [Linux 运维](docs/linux/ops) - -### Linux 服务器运维 - -- [Linux 典型运维应用](docs/linux/ops/linux典型运维应用.md) -- [samba 使用详解](docs/linux/ops/samba使用详解.md) - -### 软件安装、配置、调优 - -> :bulb: **说明** -> -> 这里总结了一些常用开发软件的安装、配置、使用指南。 -> -> 此外,提供了一键安装、配置脚本,:point_right: [**软件安装配置脚本**](https://github.com/dunwu/linux-tutorial/tree/master/codes/linux/ops/soft)。 -> -> [环境部署工具](https://github.com/dunwu/linux-tutorial/tree/master/codes/deploy) :适合开发、运维人员,在 [CentOS](https://www.centos.org/) 机器上安装常用命令工具或开发软件。 +以下内容均含配套执行脚本,方便日常的 linux 运维。 | 软件 | 说明 | | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | @@ -60,4 +21,77 @@ | [Zookeeper 安装](docs/linux/soft/zookeeper.md) | 分布式系统协调软件 | | [Nacos 安装](linux/soft/nacos.md) | 微服务发现、管理 | -## [Docker](docs/docker) +## :memo: 知识点 + +- [Linux 命令教程](docs/linux/cli) +- [Docker 教程](docs/docker) +- [Git 教程](docs/linux/tool/git) +- [Vim](docs/linux/tool/vim.md) +- 脚本语言 + - [一篇文章让你彻底掌握 shell 语言](docs/lang/shell.md) + - [一篇文章让你彻底掌握 python 语言](docs/lang/python.md) +- 运维 + - [Linux 典型运维应用](docs/linux/ops/linux典型运维应用.md) + - [samba 使用详解](docs/linux/ops/samba使用详解.md) + +## :books: 学习资源 + +### 命令行检索 + +- [Linux命令大全](http://man.linuxde.net/) +- [linux-command](https://jaywcjlove.gitee.io/linux-command/) + +### 社区网站 + +- [Linux 中国](https://linux.cn/) - 各种资讯、文章、技术 +- [实验楼](https://www.shiyanlou.com/) - 免费提供了 Linux 在线环境,不用在自己机子上装系统也可以学习 Linux,超方便实用。 +- [鸟哥的 linux 私房菜](http://linux.vbird.org/) - 非常适合 Linux 入门初学者看的教程。 +- [Linux 公社](http://www.linuxidc.com/) - Linux 相关的新闻、教程、主题、壁纸都有。 +- [Linux Today](http://www.linuxde.net) - Linux 新闻资讯发布,Linux 职业技术学习!。 + +### 知识相关 + +- [Linux 思维导图整理](http://www.jianshu.com/p/59f759207862) +- [Linux 初学者进阶学习资源整理](http://www.jianshu.com/p/fe2a790b41eb) +- [Linux 基础入门(新版)](https://www.shiyanlou.com/courses/1) +- [【译】Linux 概念架构的理解](http://www.jianshu.com/p/c5ae8f061cfe) [En](http://oss.org.cn/ossdocs/linux/kernel/a1/index.html) +- [Linux 守护进程的启动方法](http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html) +- [Linux 编程之内存映射](https://www.shiyanlou.com/questions/2992) +- [Linux 知识点小结](https://blog.huachao.me/2016/1/Linux%E7%9F%A5%E8%AF%86%E7%82%B9%E5%B0%8F%E7%BB%93/) +- [10 大白帽黑客专用的 Linux 操作系统](https://linux.cn/article-6971-1.html) + +### 软件工具 + +- [超赞的 Linux 软件](https://www.gitbook.com/book/alim0x/awesome-linux-software-zh_cn/details) Github 仓库[Zh](https://github.com/alim0x/Awesome-Linux-Software-zh_CN) [En](https://github.com/VoLuong/Awesome-Linux-Software) +- [程序员喜欢的 9 款最佳的 Linux 文件比较工具](http://os.51cto.com/art/201607/513796.htm) +- [提高 Linux 开发效率的 5 个工具](http://www.codeceo.com/article/5-linux-productivity-tools.html) +- [你要了解的 11 款面向 Linux 系统的一流备份实用工具](http://os.51cto.com/art/201603/508027.htm) +- [16 个很有用的在线工具](http://www.simlinux.com/archives/264.html) +- Adobe 软件的最佳替代品 [原文在这里](https://linux.cn/article-8928-1.html) + - [Evince (Adobe Acrobat Reader)](https://wiki.gnome.org/Apps/Evince) 一个“支持多种文档格式的文档查看器”,可以查看 PDF,还支持各种漫画书格式 + - [Pixlr (Adobe Photoshop)](https://pixlr.com/) 一个强大的图像编辑工具 + - [Inkscape (Adobe Illustrator)](https://inkscape.org/zh/) 一个专业的矢量图形编辑器 + - [Pinegrow Web Editor (Adobe Dreamweaver)](https://pinegrow.com/) 一个可视化编辑制作 HTML 网站 + - [Scribus (Adobe InDesign)](https://www.scribus.net/) 一个开源电子杂志制作软件 + - [Webflow (Adobe Muse)](https://webflow.com/) 一款可以帮助用户不用编码就可以快速创建网站的谷歌浏览器插件。 + - [Tupi (Adobe Animate)](http://www.maefloresta.com/portal/) 一款可以创建 HTML5 动画的工具。 + - [Black Magic Fusion (Adobe After Effects)](https://www.blackmagicdesign.com) 一款先进的合成软件,广泛应用于视觉特效、广电影视设计以及 3D 动画设计等领域。 + +### 中国开源镜像站点 + +- 阿里云开源镜像站:http://mirrors.aliyun.com/ +- 网易开源镜像站:http://mirrors.163.com/ +- 搜狐开源镜像站:http://mirrors.sohu.com/ +- 北京交通大学:http://mirror.bjtu.edu.cn/ \<教育网荐\> +- 兰州大学:http://mirror.lzu.edu.cn/ \<西北高校 FTP 搜索引擎\> +- 厦门大学:http://mirrors.xmu.edu.cn/ +- 上海交通大学:http://ftp.sjtu.edu.cn/ +- 清华大学:http://mirrors.tuna.tsinghua.edu.cn/ + - http://mirrors6.tuna.tsinghua.edu.cn/ + - http://mirrors4.tuna.tsinghua.edu.cn/ +- 中国科学技术大学:http://mirrors.ustc.edu.cn/ + - http://ipv4.ustc.edu.cn/ \<教育网、电信\> + - http://ipv6.ustc.edu.cn/ \ +- 东北大学:http://mirror.neu.edu.cn/ +- 浙江大学:http://mirrors.zju.edu.cn/ +- 东软信息学院:http://mirrors.neusoft.edu.cn/ diff --git a/codes/linux/ops/tool/Autoinstall_ELK_V1.3.sh b/codes/linux/ops/tool/Autoinstall_ELK_V1.3.sh new file mode 100644 index 0000000..4d1f583 --- /dev/null +++ b/codes/linux/ops/tool/Autoinstall_ELK_V1.3.sh @@ -0,0 +1,164 @@ +#!/bin/bash +#mail:xuel@anchnet.com +#data:2017/9/7 +#AutoInstall ELK scripts +#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1 +clear +echo "##########################################" +echo "# Auto Install ELK. ##" +echo "# Press Ctrl + C to cancel ##" +echo "# Any key to continue ##" +echo "##########################################" +read -p +software_dir="/usr/local/software" +elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz" +kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz" +logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz" +filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz" +sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1` +IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1` +jvm_conf="/usr/local/elasticsearch/config/jvm.options" +sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1` + +#wget software +wget_fun() { +if [ ! -d ${software_dir} ];then + mkdir -p ${software_dir} && cd ${software_dir} +else + cd ${software_dir} +fi +for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url +do + wget -c $software +done +clear +} +#initial system:install java wget;set hostname;disable firewalld +init_sys() { +[ -f /etc/init.d/functions ] && . /etc/init.d/functions +[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1 +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config +setenforce 0 +yum install -y java-1.8.0-openjdk wget net-tools +hostnamectl set-hostname elk-server +systemctl stop firewalld +cat >>/etc/security/limits.conf<>/etc/sysctl.conf && sysctl -p +if [ ${sys_mem} -eq 0 ];then + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} +else + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf} +fi +cat >>/usr/local/elasticsearch/config/elasticsearch.yml</usr/local/logstash/config/01-syslog.conf< "5044" + } + } +output { + elasticsearch { + hosts => "127.0.0.1:9200" + } + stdout { codec => rubydebug } +} +EOF +nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null +} + +#install filebeat +install_filebeat() { +cd $software_dir +tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz +mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat +cat >/usr/local/filebeat/filebeat.yml</dev/null +} + +#install kibana +install_kibana() { +cd $software_dir +tar -zxf kibana-5.4.1-linux-x86_64.tar.gz +mv kibana-5.4.1-linux-x86_64 /usr/local/kibana +cat >> /usr/local/kibana/config/kibana.yml </dev/null +} + +check() { +port=$1 +program=$2 +check_port=`netstat -lntup|grep ${port}|wc -l` +check_program=`ps -ef|grep ${program}|grep -v grep|wc -l` +if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then + action "${program} run is ok!" /bin/true +else + action "${program} run is error!" /bin/false +fi +} + +main() { +init_sys +wget_fun +install_elasticsearch +install_filebeat +install_logstash +install_kibana +echo -e "\033[32m Checking Elasticsearch...\033[0m" +sleep 20 +check :9200 "elasticsearch" +echo -e "\033[32m Checking Logstash...\033[0m" +sleep 2 +check ":9600" "logstash" +echo -e "\033[32m Checking Kibana...\033[0m" +sleep 2 +check ":5601" "kibana" +action "ELK install is success!" /bin/true +echo "url:http://$IP:5601" +} +main diff --git a/codes/linux/ops/tool/Cpu_Limit.sh b/codes/linux/ops/tool/Cpu_Limit.sh new file mode 100644 index 0000000..1d959da --- /dev/null +++ b/codes/linux/ops/tool/Cpu_Limit.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# auth:kaliarch +# func:sys info check +# version:v1.0 +# sys:centos6.x/7.x + +set -e +[ $(id -u) -gt 0 ] && exit 1 + +# cpu使用超过百分之多少进行限制 +PEC_CPU=80 + +# 限制进程使用百分之多少,如果程序为多线程,单个cpu限制为85,如果为多核心,就需要按照比例写,例如cpu为2c,像限制多线程占比80%,就写170 +LIMIT_CPU=85 +# 日志 +LOG_DIR=/var/log/cpulimit/ + +# 超过阀值进程pid +PIDARG=$(ps -aux |awk -v CPU=${PEC_CPU} '{if($3 > CPU) print $2}') +CPULIMITCMD=$(which cpulimit) + +install_cpulimit() { + [ ! -d /tmp ] && mkdir /tmp || cd /tmp + wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz + tar -zxf v0.2.tar.gz + cd cpulimit-0.2 && make + [ $? -eq 0 ] && cp src/cpulimit /usr/bin/ +} + + +do_cpulimit() { +[ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR} +for i in ${PIDARG}; +do + MSG=$(ps -aux |awk -v pid=$i '{if($2 == pid) print $0}') + echo ${MSG} + [ ! -d /tmp ] && mkdir /tmp || cd /tmp + nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} & + echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log +done +} + +main() { + + hash cpulimit + if [ $? -eq 0 ];then + do_cpulimit + else + install_cpulimit && do_cpulimit + fi +} + +main diff --git a/codes/linux/ops/tool/Custom_Rm.sh b/codes/linux/ops/tool/Custom_Rm.sh new file mode 100644 index 0000000..75a06d3 --- /dev/null +++ b/codes/linux/ops/tool/Custom_Rm.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# function:自定义rm命令,每天晚上定时清理 + +CMD_SCRIPTS=$HOME/.rm_scripts.sh +TRASH_DIR=$HOME/.TRASH_DIR +CRON_FILE=/var/spool/cron/root +BASHRC=$HOME/.bashrc + +[ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR} +cat > $CMD_SCRIPTS <> $CRON_FILE +echo "删除目录:$TRASH_DIR" +echo "删除脚本:$CMD_SCRIPTS" +echo "请执行:source $BASHRC 来加载文件或退出当前shell重新登录" diff --git a/codes/linux/ops/tool/Daily_Archive.sh b/codes/linux/ops/tool/Daily_Archive.sh new file mode 100644 index 0000000..7e614b5 --- /dev/null +++ b/codes/linux/ops/tool/Daily_Archive.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# +# Daily_Archive - Archive designated files & directories +###################################################### +# +# Gather Current Date +# +DATE=`date +%y%m%d` +# +# Set Archive File Name +# +FILE=archive$DATE.tar.gz +# +# Set Configuration and Destination File +# +CONFIG_FILE=/home/tiandi/archive/Files_To_Backup +DESTINATION=/home/tiandi/archive/$FILE +# +##################### Main Script ############### +# +# Check Backup Config file exists +# +if [ -f $CONFIG_FILE ] #Make sure the config file still exists +then + echo +else + echo + echo "$CONFIG_FILE does not exist." + echo "Backup not completed due to missing Configuration file" + echo + exit +fi +# +# Build the names of all the files to backup +# +FILE_NO=1 # Start on Line 1 of Config file. +exec < $CONFIG_FILE # Redirect Std Input to name of Config File +# +read FILE_NAME # Read 1st record +# +while [ $? -eq 0 ] +do + # Make sure the file or directory exists. + if [ -f $FILE_NAME -o -d $FILE_NAME ] + then + # If file exists, add its name to the lists + FILE_LIST="$FILE_LIST $FILE_NAME" + else + # If file doesn't exist, issue warning + echo + echo "$FILE_NAME, does not exist." + echo "Obviously, I will not include it in this archive." + echo "It is listed on line $FILE_NO of the config file." + echo "Continuing to build archive file." + echo + fi +# + FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one + read FILE_NAME # Read next record. +done +########################################################### +# +# Backup the files and Compress Archive +# +tar -czf $DESTINATION $FILE_LIST 2> /dev/null +# diff --git a/codes/linux/ops/tool/Hourly_Archive.sh b/codes/linux/ops/tool/Hourly_Archive.sh new file mode 100644 index 0000000..e0396f4 --- /dev/null +++ b/codes/linux/ops/tool/Hourly_Archive.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# +# Hourly_Archive - Every hour create an archive +###################################################### +# +# Set Configuration and Destination File +# +CONFIG_FILE=/home/tiandi/archive/Files_To_Backup +# +# Gather Current Date,Month & Time +# +DAY=`date +%d` +MONTH=`date +%m` +TIME=`date +%k%M` +# +# Set Base Archive Destination Location +# +BASEDEST=/home/tiandi/archive/hourly +# +# Create Archive Destination Directory +mkdir -p $BASEDEST/$MONTH/$DAY +# +# Build Archive Destination File Name +DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz +# +##################### Main Script ############### +# +# Check Backup Config file exists +# +if [ -f $CONFIG_FILE ] #Make sure the config file still exists +then + echo +else + echo + echo "$CONFIG_FILE does not exist." + echo "Backup not completed due to missing Configuration file" + echo + exit +fi +# +# Build the names of all the files to backup +# +FILE_NO=1 # Start on Line 1 of Config file. +exec < $CONFIG_FILE # Redirect Std Input to name of Config File +# +read FILE_NAME # Read 1st record +# +while [ $? -eq 0 ] +do + # Make sure the file or directory exists. + if [ -f $FILE_NAME -o -d $FILE_NAME ] + then + # If file exists, add its name to the lists + FILE_LIST="$FILE_LIST $FILE_NAME" + else + # If file doesn't exist, issue warning + echo + echo "$FILE_NAME, does not exist." + echo "Obviously, I will not include it in this archive." + echo "It is listed on line $FILE_NO of the config file." + echo "Continuing to build archive file." + echo + fi +# + FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one + read FILE_NAME # Read next record. +done +########################################################### +# +# Backup the files and Compress Archive +# +tar -czf $DESTINATION $FILE_LIST 2> /dev/null +# diff --git a/codes/linux/ops/tool/Sys_Check.sh b/codes/linux/ops/tool/Sys_Check.sh new file mode 100644 index 0000000..d94b3eb --- /dev/null +++ b/codes/linux/ops/tool/Sys_Check.sh @@ -0,0 +1,225 @@ +#!/bin/bash +# auth:kaliarch +# func:sys info check +# version:v1.0 + +[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1 +sysversion=$(rpm -q centos-release|cut -d- -f3) +line="-------------------------------------------------" + + +[ -d logs ] || mkdir logs + +sys_check_file="logs/$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')-`date +%Y%m%d`.txt" + +# 获取系统cpu信息 +function get_cpu_info() { + Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l) + Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l) + CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}') + CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq) + CPU_Arch=$(uname -m) +cat </dev/null) + if [ ${sysversion} -gt 6 ];then + service_config=$(systemctl list-unit-files --type=service --state=enabled|grep "enabled") + run_service=$(systemctl list-units --type=service --state=running |grep ".service") + else + service_config=$(/sbin/chkconfig | grep -E ":on|:启用" |column -t) + run_service=$(/sbin/service --status-all|grep -E "running") + fi +cat </dev/null|cut -d/ -f5;egrep -v "^$|^#" ${cronuser} 2>/dev/null;echo "";done) +cat < ${sys_check_file} diff --git a/codes/linux/ops/tool/install_elasticserch.sh b/codes/linux/ops/tool/install_elasticserch.sh new file mode 100644 index 0000000..9511390 --- /dev/null +++ b/codes/linux/ops/tool/install_elasticserch.sh @@ -0,0 +1,194 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:elasticsearch 5.4.1/6.0.1/6.3.1安装 +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_elasticsearch.log +env_file=/etc/profile.d/elasticsearch.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ +software_config_file=${install_path}elasticsearch/config/elasticsearch.yml +sysversion=$(rpm -q centos-release|cut -d- -f3) +jvm_conf="/usr/local/elasticsearch/config/jvm.options" +sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1` +hostname=elk-server + +clear +echo "##########################################" +echo "# #" +echo "# 安装 elasticsearch 5.4.1/6.0.1/6.3.1 #" +echo "# #" +echo "##########################################" +echo "1: Install elasticsearch 5.4.1" +echo "2: Install elasticsearch 6.0.1" +echo "3: Install elasticsearch 6.3.1" +echo "4: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-5.4.1.tar.gz" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.0.1.tar.gz" +elif [ "${softversion}" == "3" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.3.1.tar.gz" +elif [ "${softversion}" == "4" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3|4|5}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +# 配置主机名,第一个为主机名 +config_hostname() { +if [ ${sysversion} -eq 6 ];then + hostname $1 +elif [ ${sysversion} -eq 7 ];then + hostnamectl set-hostname $1 +else + echo "`date +%F' '%H:%M:%S` hostname $1 config fail">> ${install_log_path}${install_log_name} +fi +} + +config_limits() { + output_msg "配置limits" + cat >>/etc/security/limits.conf<>/etc/sysctl.conf +sysctl -p >/dev/null 2>&1 + +} + +# 添加配置文件 +add_config() { +cat > $1 << EOF +cluster.name: my-application +node.name: ${hostname} +path.data: /usr/local/elasticsearch/data +path.logs: /usr/local/elasticsearch/logs +network.host: 127.0.0.1 +http.port: 9200 +discovery.zen.ping.unicast.hosts: ["$hostname"] +EOF +} + +config_user() { +useradd $1 >/dev/null 2>&1 +if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $1 user add success">> ${install_log_path}${install_log_name} +else + echo "`date +%F' '%H:%M:%S` $1 user add fail">> ${install_log_path}${install_log_name} && exit 1 +fi +chown ${1}.${1} ${install_path}elasticsearch/ -R +} + +config_jvm() { +if [ ${sys_mem} -eq 0 ];then + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} +else + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf} +fi +} + + +main() { +check_dir $install_log_path $install_path +check_yum_command wget wget +download_file $URL +config_hostname $hostname + +software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') +for filename in `ls $download_path`;do + extract_file ${download_path}$filename +done + +rm -fr ${download_path} +ln -s $install_path$software_name ${install_path}elasticsearch +add_config $software_config_file +check_dir ${install_path}elasticsearch/{data,logs} +config_user elasticsearch +config_env ${install_path}elasticsearch/bin +config_limits +config_jvm +echo "请使用一下命令启动服务:'su - elasticsearch -c 'nohup /usr/local/elasticsearch/bin/elasticsearch &'" + +} + +main diff --git a/codes/linux/ops/tool/install_filebeat.sh b/codes/linux/ops/tool/install_filebeat.sh new file mode 100644 index 0000000..6c09aec --- /dev/null +++ b/codes/linux/ops/tool/install_filebeat.sh @@ -0,0 +1,136 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:filebeat 5.6.1/6.1.3/6.3.2 安装 + +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_filebeat.log +env_file=/etc/profile.d/filebeat.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ +software_config_file=${install_path}filebeat/filebeat.yml + +clear +echo "##########################################" +echo "# #" +echo "# 安装 filebeat 5.6.1/6.1.3/6.3.2 #" +echo "# #" +echo "##########################################" +echo "1: Install filebeat 5.6.1" +echo "2: Install filebeat 6.1.3" +echo "3: Install filebeat 6.3.2" +echo "4: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.1.3-linux-x86_64.tar.gz" +elif [ "${softversion}" == "3" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.3.2-linux-x86_64.tar.gz" +elif [ "${softversion}" == "4" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3|4}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +# 添加配置文件 +add_config() { +cat> $1 </dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# yum 安装软件包,可传入多个软件包,用空格隔开 +yum_install_software() { + output_msg "yum 安装软件" + yum -y install $* >/dev/null 2>${install_log_path}${install_log_name} + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S`yum install $* 完成" >>${install_log_path}${install_log_name} + else + exit 1 + fi +} + + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 编译安装git,传入$1 为解压后软件包的名称 +source_install_git() { + output_msg "编译安装git" + mv ${install_path}${1} ${install_path}tmp${1} + cd ${install_path}tmp${1} && make prefix=${install_path}git all >/dev/null 2>&1 + if [ $? -eq 0 ];then + make prefix=${install_path}git install >/dev/null 2>&1 echo "`date +%F' '%H:%M:%S` git source install success ">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` git source install fail!">>${install_log_path}${install_log_name} && exit 1 + fi +} + + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + + +main() { +check_dir $install_log_path $install_path +check_yum_command wget wget +check_yum_command make make +yum_install_software curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker +download_file $URL + +software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') +for filename in `ls $download_path`;do + extract_file ${download_path}$filename +done + +source_install_git ${software_name} +mv /usr/bin/git /usr/bin/git.bak + +rm -fr ${download_path} +config_env ${install_path}git/bin +} + +main + diff --git a/codes/linux/ops/tool/install_grafana.sh b/codes/linux/ops/tool/install_grafana.sh new file mode 100644 index 0000000..36ddc39 --- /dev/null +++ b/codes/linux/ops/tool/install_grafana.sh @@ -0,0 +1,110 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:grafana 5.1.0/5.1.5/5.2.2 安装 + +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_grafana.log +env_file=/etc/profile.d/grafana.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ + +clear +echo "##########################################" +echo "# #" +echo "# 安装 grafana 5.1.0/5.1.5/5.2.2 #" +echo "# #" +echo "##########################################" +echo "1: Install grafana 5.1.0" +echo "2: Install grafana 5.1.5" +echo "3: Install grafana 5.2.2" +echo "4: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.0-1.x86_64.rpm" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.5-1.x86_64.rpm" +elif [ "${softversion}" == "3" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.2.2-1.x86_64.rpm" +elif [ "${softversion}" == "4" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3|4}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 安装grafana插件,传入安装的插件的名称 +install_grafana_plugins() { + output_msg "grafana插件安装" + check_yum_command grafana-cli + grafana-cli plugins install $* >/dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` grafana plugins $* install success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` grafana plugins $* install success!">>${install_log_path}${install_log_name} && exit 1 + fi + + +} + +main() { +check_dir $install_log_path $install_path +check_yum_command wget wget +download_file $URL +for filename in `ls $download_path`;do + yum -y install $download_path$filename >/dev/null 2>&1 +done +install_grafana_plugins alexanderzobnin-zabbix-app +} + +main + diff --git a/codes/linux/ops/tool/install_kafka.sh b/codes/linux/ops/tool/install_kafka.sh new file mode 100644 index 0000000..e7aee23 --- /dev/null +++ b/codes/linux/ops/tool/install_kafka.sh @@ -0,0 +1,139 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:kafka 0.10.2/0.11.0/1.1.0/2.0.0 安装 + +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_kafka.log +env_file=/etc/profile.d/kafka.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ +software_config_file=${install_path}kafka/config/server.properties + +clear +echo "##########################################" +echo "# #" +echo "# 安装kafka 0.10.2/0.11.0/1.1.0/2.0.0 #" +echo "# #" +echo "##########################################" +echo "1: Install kafka 0.10.2" +echo "2: Install kafka 0.11.0" +echo "3: Install kafka 1.1.0" +echo "4: Install kafka 2.0.0" +echo "5: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-0.10.2.2.tgz" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-0.11.0.3.tgz" +elif [ "${softversion}" == "3" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-1.1.0.tgz" +elif [ "${softversion}" == "4" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-2.0.0.tgz" +elif [ "${softversion}" == "5" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3|4|5}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ] || [ "${file##*.}" == "tgz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +# 添加配置文件 +add_config() { +cat> $1 </dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +# 添加配置文件 +add_config() { +cat> $1 </dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +# 添加配置文件 +add_config() { +cat> $1 < "5044" + } + } +output { + elasticsearch { + hosts => "127.0.0.1:9200" + } + stdout { codec => rubydebug } +} +EOF +} + +main() { +check_dir $install_log_path $install_path +check_yum_command wget wget +download_file $URL + +software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') +for filename in `ls $download_path`;do + extract_file ${download_path}$filename +done +rm -fr ${download_path} +ln -s $install_path$software_name ${install_path}logstash +add_config ${software_config_file} +config_env ${install_path}logstash/bin +} + +main diff --git a/codes/linux/ops/tool/install_maven.sh b/codes/linux/ops/tool/install_maven.sh new file mode 100644 index 0000000..084dbbb --- /dev/null +++ b/codes/linux/ops/tool/install_maven.sh @@ -0,0 +1,125 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:maven 3.0.5/3.3.9/3.5.4 安装 + +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_maven.log +env_file=/etc/profile.d/maven.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ +#software_config_file=${install_path}filebeat/filebeat.yml + +clear +echo "##########################################" +echo "# #" +echo "# 安装 maven 3.0.5/3.3.9/3.5.4 #" +echo "# #" +echo "##########################################" +echo "1: Install maven 3.0.5" +echo "2: Install maven 3.2.9" +echo "3: Install maven 3.5.4" +echo "4: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/maven/apache-maven-3.0.5-bin.tar.gz" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/maven/apache-maven-3.3.9-bin.tar.gz" +elif [ "${softversion}" == "3" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/maven/apache-maven-3.5.4-bin.tar.gz" +elif [ "${softversion}" == "4" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3|4}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + + +main() { +check_dir $install_log_path $install_path +check_yum_command wget wget +download_file $URL + +software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'-bin.tar.gz' '{print $1}') +for filename in `ls $download_path`;do + extract_file ${download_path}$filename +done +rm -fr ${download_path} +ln -s $install_path$software_name ${install_path}maven +config_env ${install_path}maven/bin +} + +main + diff --git a/codes/linux/ops/tool/install_tomcat.sh b/codes/linux/ops/tool/install_tomcat.sh new file mode 100644 index 0000000..5f53019 --- /dev/null +++ b/codes/linux/ops/tool/install_tomcat.sh @@ -0,0 +1,126 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:tomcat 6.0/7.0/8.5/9.0 安装 + +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_tomcat.log +env_file=/etc/profile.d/tomcat.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ + +clear +echo "##########################################" +echo "# #" +echo "# 安装 tomcat 6.0/7.0/8.5/9.0 #" +echo "# #" +echo "##########################################" +echo "1: Install tomcat-6.0" +echo "2: Install tomcat-7.0" +echo "3: Install tomcat-8.5" +echo "4: Install tomcat-9.0" +echo "5: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-6.0.9.zip" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-7.0.79.zip" +elif [ "${softversion}" == "3" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-8.5.20.zip" +elif [ "${softversion}" == "4" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-9.0.0.M26.zip" +elif [ "${softversion}" == "5" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3|4|5}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +main() { +check_dir $install_log_path $install_path +check_yum_command wget wget +check_yum_command unzip unzip +download_file $URL + +software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.zip' '{print $1}') +for filename in `ls $download_path`;do + extract_file ${download_path}$filename +done +rm -fr ${download_path} +ln -s $install_path$software_name ${install_path}tomcat +config_env ${install_path}tomcat/bin +} + +main diff --git a/codes/linux/ops/tool/install_zookeeper.sh b/codes/linux/ops/tool/install_zookeeper.sh new file mode 100644 index 0000000..cc7a503 --- /dev/null +++ b/codes/linux/ops/tool/install_zookeeper.sh @@ -0,0 +1,137 @@ +#!/bin/bash +# auth:kaliarch +# version:v1.0 +# func:zookeeper 3.4/3.5 安装 + +# 定义安装目录、及日志信息 +. /etc/init.d/functions +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +download_path=/tmp/tmpdir/ +install_log_name=install_zookeeper.log +env_file=/etc/profile.d/zookeeper.sh +install_log_path=/var/log/appinstall/ +install_path=/usr/local/ +software_config_file=${install_path}zookeeper/conf/zoo.cfg + +clear +echo "##########################################" +echo "# #" +echo "# 安装 zookeeper 3.4/3.5 #" +echo "# #" +echo "##########################################" +echo "1: Install zookeeper 3.4" +echo "2: Install zookeeper 3.5" +echo "3: EXIT" +# 选择安装软件版本 +read -p "Please input your choice:" softversion +if [ "${softversion}" == "1" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/zookeeper/zookeeper-3.4.12.tar.gz" +elif [ "${softversion}" == "2" ];then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/zookeeper/zookeeper-3.5.4-beta.tar.gz" +elif [ "${softversion}" == "3" ];then + echo "you choce channel!" + exit 1; +else + echo "input Error! Place input{1|2|3}" + exit 0; +fi + +# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 +output_msg() { + for msg in $*;do + action $msg /bin/true + done +} + + +# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 +check_yum_command() { + output_msg "命令检查:$1" + hash $1 >/dev/null 2>&1 + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 + else + yum -y install $2 >/dev/null 2>&1 + # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } + fi +} + +# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 +check_dir() { + output_msg "目录检查" + for dirname in $*;do + [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} + done +} + +# 下载文件并解压至安装目录,传入url链接地址 +download_file() { + output_msg "下载源码包" + mkdir -p $download_path + for file in $*;do + wget $file -c -P $download_path &> /dev/null + if [ $? -eq 0 ];then + echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + + +# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 +extract_file() { + output_msg "解压源码" + for file in $*;do + if [ "${file##*.}" == "gz" ];then + tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + elif [ "${file##*.}" == "zip" ];then + unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 + fi + done +} + +# 配置环境变量,第一个参数为添加环境变量的绝对路径 +config_env() { + output_msg "环境变量配置" + echo "export PATH=\$PATH:$1" >${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + +} + +# 添加配置文件 +add_config() { +cat> $1 <${install_path}zookeeper/data/myid +config_env ${install_path}zookeeper/bin +} + +main + diff --git a/codes/linux/ops/tool/mgitstatus.sh b/codes/linux/ops/tool/mgitstatus.sh new file mode 100644 index 0000000..c650199 --- /dev/null +++ b/codes/linux/ops/tool/mgitstatus.sh @@ -0,0 +1,246 @@ +#!/bin/sh + +################################################################################### +# 此脚本用于检测 git 状态 +# Copy from https://github.com/fboender/multi-git-status +################################################################################### + +# MIT license + +if [ -t 1 ]; then + # Our output is not being redirected, so we can use colors. + C_RED="\033[1;31m" + C_GREEN="\033[1;32m" + C_YELLOW="\033[1;33m" + C_BLUE="\033[1;34m" + C_PURPLE="\033[1;35m" + C_CYAN="\033[1;36m" + C_RESET="$(tput sgr0)" +fi + +C_OK="$C_GREEN" +C_LOCKED="$C_RED" +C_NEEDS_PUSH="$C_YELLOW" +C_NEEDS_PULL="$C_BLUE" +C_NEEDS_COMMIT="$C_RED" +C_NEEDS_UPSTREAM="$C_PURPLE" +C_UNTRACKED="$C_CYAN" +C_STASHES="$C_YELLOW" + +DEBUG=0 + +usage () { + cat << EOF >&2 + +Usage: $0 [-w] [-e] [-f] [--no-X] [DIR] [DEPTH=2] + +Scan for .git dirs under DIR (up to DEPTH dirs deep) and show git status + + -w Warn about dirs that are not Git repositories + -e Exclude repos that are 'ok' + -f Do a 'git fetch' on each repo (slow for many repos) + +You can limit output with the following options: + + --no-push + --no-pull + --no-upstream + --no-uncommitted + --no-untracked + --no-stashes + +EOF +} + +# Handle commandline options +WARN_NOT_REPO=0 +EXCLUDE_OK=0 +DO_FETCH=0 +NO_PUSH=0 +NO_PULL=0 +NO_UPSTREAM=0 +NO_UNCOMMITTED=0 +NO_UNTRACKED=0 +NO_STASHES=0 + +while [ \! -z "$1" ]; do + # Stop reading when we've run out of options. + [ "$(echo "$1" | cut -c 1)" != "-" ] && break + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + usage + exit 1 + fi + if [ "$1" = "-w" ]; then + WARN_NOT_REPO=1 + fi + if [ "$1" = "-e" ]; then + EXCLUDE_OK=1 + fi + if [ "$1" = "-f" ]; then + DO_FETCH=1 + fi + if [ "$1" = "--no-push" ]; then + NO_PUSH=1 + fi + if [ "$1" = "--no-pull" ]; then + NO_PULL=1 + fi + if [ "$1" = "--no-upstream" ]; then + NO_UPSTREAM=1 + fi + if [ "$1" = "--no-uncommitted" ]; then + NO_UNCOMMITTED=1 + fi + if [ "$1" = "--no-untracked" ]; then + NO_UNTRACKED=1 + fi + if [ "$1" = "--no-stashes" ]; then + NO_STASHES=1 + fi + + shift +done + +if [ -z "$1" ]; then + ROOT_DIR="." +else + ROOT_DIR="$1" +fi + +if [ -z "$2" ]; then + DEPTH=2 +else + DEPTH="$2" +fi + +# Find all .git dirs, up to DEPTH levels deep +find -L "$ROOT_DIR" -maxdepth "$DEPTH" -type d | while read -r PROJ_DIR +do + GIT_DIR="$PROJ_DIR/.git" + + # If this dir is not a repo, and WARN_NOT_REPO is 1, tell the user. + if [ \! -d "$GIT_DIR" ]; then + if [ "$WARN_NOT_REPO" -eq 1 ] && [ "$PROJ_DIR" != "." ]; then + printf "${PROJ_DIR}: not a git repo\n" + fi + continue + fi + + [ $DEBUG -eq 1 ] && echo "${PROJ_DIR}" + + # Check if repo is locked + if [ -f "$GIT_DIR/index.lock" ]; then + printf "${PROJ_DIR}: ${C_LOCKED}Locked. Skipping.${C_RESET}\n" + continue + fi + + # Do a 'git fetch' if requested + if [ "$DO_FETCH" -eq 1 ]; then + git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" fetch -q >/dev/null + fi + + # Refresh the index, or we might get wrong results. + git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" update-index -q --refresh >/dev/null 2>&1 + + # Find all remote branches that have been checked out and figure out if + # they need a push or pull. We do this with various tests and put the name + # of the branches in NEEDS_XXXX, seperated by newlines. After we're done, + # we remove duplicates from NEEDS_XXX. + NEEDS_PUSH_BRANCHES="" + NEEDS_PULL_BRANCHES="" + NEEDS_UPSTREAM_BRANCHES="" + + for REF_HEAD in $(cd "$GIT_DIR/refs/heads" && find . -type 'f' | sed "s/^\.\///"); do + # Check if this branch is tracking an upstream (local/remote branch) + UPSTREAM=$(git --git-dir "$GIT_DIR" rev-parse --abbrev-ref --symbolic-full-name "$REF_HEAD@{u}" 2>/dev/null) + EXIT_CODE="$?" + if [ "$EXIT_CODE" -eq 0 ]; then + # Branch is tracking a remote branch. Find out how much behind / + # ahead it is of that remote branch. + CNT_AHEAD_BEHIND=$(git --git-dir "$GIT_DIR" rev-list --left-right --count "$REF_HEAD...$UPSTREAM") + CNT_AHEAD=$(echo "$CNT_AHEAD_BEHIND" | awk '{ print $1 }') + CNT_BEHIND=$(echo "$CNT_AHEAD_BEHIND" | awk '{ print $2 }') + + [ $DEBUG -eq 1 ] && echo "CNT_AHEAD_BEHIND: $CNT_AHEAD_BEHIND" + [ $DEBUG -eq 1 ] && echo "CNT_AHEAD: $CNT_AHEAD" + [ $DEBUG -eq 1 ] && echo "CNT_BEHIND: $CNT_BEHIND" + + if [ "$CNT_AHEAD" -gt 0 ]; then + NEEDS_PUSH_BRANCHES="${NEEDS_PUSH_BRANCHES}\n$REF_HEAD" + fi + if [ "$CNT_BEHIND" -gt 0 ]; then + NEEDS_PULL_BRANCHES="${NEEDS_PULL_BRANCHES}\n$REF_HEAD" + fi + + # Check if this branch is a branch off another branch. and if it needs + # to be updated. + REV_LOCAL=$(git --git-dir "$GIT_DIR" rev-parse --verify "$REF_HEAD" 2>/dev/null) + REV_REMOTE=$(git --git-dir "$GIT_DIR" rev-parse --verify "$UPSTREAM" 2>/dev/null) + REV_BASE=$(git --git-dir "$GIT_DIR" merge-base "$REF_HEAD" "$UPSTREAM" 2>/dev/null) + + [ $DEBUG -eq 1 ] && echo "REV_LOCAL: $REV_LOCAL" + [ $DEBUG -eq 1 ] && echo "REV_REMOTE: $REV_REMOTE" + [ $DEBUG -eq 1 ] && echo "REV_BASE: $REV_BASE" + + if [ "$REV_LOCAL" = "$REV_REMOTE" ]; then + : # NOOP + else + if [ "$REV_LOCAL" = "$REV_BASE" ]; then + NEEDS_PULL_BRANCHES="${NEEDS_PULL_BRANCHES}\n$REF_HEAD" + fi + if [ "$REV_REMOTE" = "$REV_BASE" ]; then + NEEDS_PUSH_BRANCHES="${NEEDS_PUSH_BRANCHES}\n$REF_HEAD" + fi + fi + else + # Branch does not have an upstream (local/remote branch). + NEEDS_UPSTREAM_BRANCHES="${NEEDS_UPSTREAM_BRANCHES}\n$REF_HEAD" + fi + + done + + # Remove duplicates from NEEDS_XXXX and make comma-seperated + NEEDS_PUSH_BRANCHES=$(printf "$NEEDS_PUSH_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/") + NEEDS_PULL_BRANCHES=$(printf "$NEEDS_PULL_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/") + NEEDS_UPSTREAM_BRANCHES=$(printf "$NEEDS_UPSTREAM_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/") + + # Find out if there are unstaged, uncommitted or untracked changes + UNSTAGED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" diff-index --quiet HEAD -- 2>/dev/null; echo $?) + UNCOMMITTED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" diff-files --quiet --ignore-submodules --; echo $?) + UNTRACKED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" ls-files --exclude-standard --others) + cd "$(dirname "$GIT_DIR")" || exit + STASHES=$(git stash list | wc -l) + cd "$OLDPWD" || exit + + # Build up the status string + IS_OK=0 # 0 = Repo needs something, 1 = Repo needs nothing ('ok') + STATUS_NEEDS="" + if [ \! -z "$NEEDS_PUSH_BRANCHES" ] && [ "$NO_PUSH" -eq 0 ]; then + STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_PUSH}Needs push ($NEEDS_PUSH_BRANCHES)${C_RESET} " + fi + if [ \! -z "$NEEDS_PULL_BRANCHES" ] && [ "$NO_PULL" -eq 0 ]; then + STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_PULL}Needs pull ($NEEDS_PULL_BRANCHES)${C_RESET} " + fi + if [ \! -z "$NEEDS_UPSTREAM_BRANCHES" ] && [ "$NO_UPSTREAM" -eq 0 ]; then + STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_UPSTREAM}Needs upstream ($NEEDS_UPSTREAM_BRANCHES)${C_RESET} " + fi + if [ "$UNSTAGED" -ne 0 ] || [ "$UNCOMMITTED" -ne 0 ] && [ "$NO_UNCOMMITTED" -eq 0 ]; then + STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_COMMIT}Uncommitted changes${C_RESET} " + fi + if [ "$UNTRACKED" != "" ] && [ "$NO_UNTRACKED" -eq 0 ]; then + STATUS_NEEDS="${STATUS_NEEDS}${C_UNTRACKED}Untracked files${C_RESET} " + fi + if [ "$STASHES" -ne 0 ] && [ "$NO_STASHES" -eq 0 ]; then + STATUS_NEEDS="${STATUS_NEEDS}${C_STASHES}$STASHES stashes${C_RESET} " + fi + if [ "$STATUS_NEEDS" = "" ]; then + IS_OK=1 + STATUS_NEEDS="${STATUS_NEEDS}${C_OK}ok${C_RESET} " + fi + + # Print the output, unless repo is 'ok' and -e was specified + if [ "$IS_OK" -ne 1 ] || [ "$EXCLUDE_OK" -ne 1 ]; then + printf "${PROJ_DIR}: $STATUS_NEEDS\n" + fi +done diff --git a/codes/linux/ops/tool/删除用户脚本.sh b/codes/linux/ops/tool/删除用户脚本.sh new file mode 100644 index 0000000..724dfc0 --- /dev/null +++ b/codes/linux/ops/tool/删除用户脚本.sh @@ -0,0 +1,247 @@ +#!/bin/bash +# +# Delete_User - Automates the 4 steps to remove an account +# +################################################################# +# +# Define Functions +# +################################################################# +function get_answer { +# + unset ANSWER + ASK_COUNT=0 +# + while [ -z "$ANSWER" ] # while no answer is given, keep asking + do + ASK_COUNT=$[ $ASK_COUNT + 1 ] +# + case $ASK_COUNT in # If user gives no answer in time allowed + 2) + echo + echo "Please answer the question." + echo + ;; + 3) + echo + echo "One last try... please answer the question." + echo + ;; + 4) + echo + echo "Since you refuse to answer the question..." + echo "exiting program." + echo + # + exit + ;; + esac +# + echo +# + if [ -n "$LINE2" ] + then + echo $LINE1 # Print 2 lines + echo -e $LINE2" \c" + else # Print 1 line + echo -e $LINE1" \c" + fi +# +# Allow 60 seconds to answer before time-out + read -t 60 ANSWER + done +# +# Do a little variable clean-up +# + unset LINE1 + unset LINE2 +# +} #end of get_answer function +# +################################################################# +function process_answer { +# + case $ANSWER in + y|Y|YES|yes|yEs|yeS|YEs|yES) + # If user answers "yes".do nothing. + ;; + *) + # If user answers anything but "yes", exit script + echo + echo $EXIT_LINE1 + echo $EXIT_LINE2 + echo + exit + ;; + esac + # + # Do a little variable clean-up + unset EXIT_LINE1 + unset EXIT_LINE2 +# +} #End of process_answer function +# +################################################################ +# +# End of Function Definitions +# +############### Main Script ################################# +# +# Get name of User Account to check +# +echo "Step #1 - Determine User Account name to delete " +echo +LINE1="Please enter the username of the user" +LINE2="account you wish to delete from system:" +get_answer +USER_ACCOUNT=$ANSWER +# +# Double check with script user that this is the correct User Account +# +LINE1="Is $USER_ACCOUNT the user account" +LINE2="you wish to delete from the system?[ y/n ]:" +get_answer +# +############################################################ +# +# Check that USER_ACCOUNT is really an account on the system +# +USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT) +# +if [ $? -eq 1 ] # If the account is not found, exit script +then + echo + echo "Account, $USER_ACCOUNT, not found." + echo "Leaving the script..." + echo + exit +fi +# +echo +echo "I found this record:" +echo $USER_ACCOUNT_RECORD +echo +# +LINE1="Is this the correct User Account?[y/n]:" +get_answer +# +# +# Call process_answer function: +# if user answers anything but "yes", exit script +# +EXIT_LINE1="Because the account, $USER_ACCOUNT, is not " +EXIT_LINE2="the one you wish to delete, we are leaving the script..." +process_anser +# +############################################################## +# +# Search for any running processes that belong to the User Account +# +echo +echo "Step #2 - Find process on system belonging to user account" +echo +echo "$USER_ACCOUNT has the following processes running: " +echo +# +ps -u $USER_ACCOUNT #List the processes running +# +case $? in +1) # No processes running for this User Account + # + echo "There are no processes for this account currently running." + echo +;; +0) # Processes running for this User Account. + # Ask Script User if wants us to kill the processes. + # + unset ANSWER # I think this line is not needed + LINE1="Would you like me to kill the process(es)? [y/n]:" + get_answer + # + case $ANSWER in + y|Y|YES|yes|Yes|yEs|yeS|YEs|yES) # if user answer "yes", + #kill User Account processes + # + echo + # + # Clean-up temp file upon signals + # + trap "rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT + # + # List user processes running + ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt + # + exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input + # + read USER_PROCESS_REC # First record will be blank + read USER_PROCESS_REC + # + while [ $? -eq 0 ] + do + # obtain PID + USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1 ) + kill -9 $USER_PID + echo "Killed process $USER_PID" + read USER_PROCESS_REC + done + # + echo + # + rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report + ;; + *) # If user answers anything but "yes", do not kill. + echo + echo "Will not kill the process(es)." + echo + ;; + esac +;; +esac +################################################################################### +# +# Create a report of all files owned by User Account +# +echo +echo "Step #3 - Find files on system belonging to user account" +echo +echo "Creating a report of all files owned by $USER_ACCOUNT." +echo +echo "It is recommended that you backup/archive these files." +echo "and then do one of two things:" +echo " 1) Delete the files" +echo " 2) Change the files' ownership to a current user account." +echo +echo "Please wait. This may take a while..." +# +REPORT_DATE=`date +%y%m%d` +REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt" +# +find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null +# +echo +echo "Report is complete." +echo "Name of report: $REPORT_FILE" +echo "Location of report: `pwd`" +echo +################################################################ +# +# Remove User Account +echo +echo "Step #4 - Remove user account" +echo +# +LINE1="Do you wish to remove $USER_ACCOUNT's account from system? [y/n]:" +get_answer +# +# Cass process_answer function: +# if user answers anything but "yes", exit script +# +EXIT_LINE1="Since you do not wish to remove the user account." +EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..." +process_answer +# +userdel $USER_ACCOUNT # delete user account +echo +echo "User account, $USER_ACCOUNT, has been removed" +echo +# diff --git a/codes/linux/ops/tool/查看磁盘目录使用情况.sh b/codes/linux/ops/tool/查看磁盘目录使用情况.sh new file mode 100644 index 0000000..0c2615a --- /dev/null +++ b/codes/linux/ops/tool/查看磁盘目录使用情况.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Big_Users - find big disk space users in various directories +############################################################# +#Parameters for script +# +CHECK_DIRECTORIES="/var/log /home" #directories to check +# +######################### Main Script ####################### +# +DATE=$(date '+%m%d%y') #Date for report file +# +exec > disk_space_$DATE.rpt #Make report file Std Output +# +echo "Top Ten Disk Space Usage" #Report header for while report +echo "for $CHECK_DIRECTORIES Directories" +# +for DIR_CHECK in $CHECK_DIRECTORIES #loop to du directories +do + echo "" + echo "The $DIR_CHECK Directory:" #Title header for each directory +# +# Creating a listing of top ten disk space users + du -S $DIR_CHECK 2>/dev/null | + sort -rn | + sed '{11,$D; =}' | + sed 'N; s/\n/ /' | + gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}' +# +done #End of for loop for du directories +# diff --git a/codes/shell/示例脚本/mysql/向数据库中插入数据.sh b/codes/shell/示例脚本/mysql/向数据库中插入数据.sh new file mode 100644 index 0000000..c2eaef4 --- /dev/null +++ b/codes/shell/示例脚本/mysql/向数据库中插入数据.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# send data to the the table in the MYSQL database + +MYSQL=`which mysql` + +if [ $# -ne 2 ] +then + echo "Usage:mtest2 emplid lastname firstname salary" +else + #脚本变量一定要用双引号,字符串变量使用单引号 + statement=" insert into em_admin values(NULL, '$1', $2)" + $MYSQL emwjs -u test < log.$today diff --git a/codes/shell/示例脚本/控制/定时执行脚本.sh b/codes/shell/示例脚本/控制/定时执行脚本.sh new file mode 100644 index 0000000..44a4049 --- /dev/null +++ b/codes/shell/示例脚本/控制/定时执行脚本.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# testing the at command + +at -f 4.sh 22:10 diff --git a/codes/shell/示例脚本/控制/捕捉信号.sh b/codes/shell/示例脚本/控制/捕捉信号.sh new file mode 100644 index 0000000..9d4473b --- /dev/null +++ b/codes/shell/示例脚本/控制/捕捉信号.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# testing signal trapping + +trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT SIGTERM + +echo this is a test program + +count=1 + +while [ $count -le 10 ] +do + echo "Loop #$count" + sleep 5 + count=$[ $count+1 ] +done diff --git a/codes/shell/示例脚本/控制/捕捉脚本的退出.sh b/codes/shell/示例脚本/控制/捕捉脚本的退出.sh new file mode 100644 index 0000000..ce3d5aa --- /dev/null +++ b/codes/shell/示例脚本/控制/捕捉脚本的退出.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# trapping the script exit + +trap "echo byebye" EXIT + +count=1 +while [ $count -le 5 ] +do + echo "Loop #$count" + sleep 3 + count=$[ $count + 1 ] +done diff --git a/codes/shell/示例脚本/控制/移除捕捉.sh b/codes/shell/示例脚本/控制/移除捕捉.sh new file mode 100644 index 0000000..22f4083 --- /dev/null +++ b/codes/shell/示例脚本/控制/移除捕捉.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# removeing a set trap + +trap "echo byebye" EXIT + +count=1 +while [ $count -le 5 ] +do + echo "Loop #$count" + sleep 3 + count=$[ $count + 1 ] +done +#移除捕捉 +trap - EXIT +echo "I just removed the trap" diff --git a/codes/shell/示例脚本/查找替换文本/gawk/gawk.sh b/codes/shell/示例脚本/查找替换文本/gawk/gawk.sh new file mode 100644 index 0000000..15f6088 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/gawk.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +BEGIN { +print "The latest list of users and shells" +print "Userid Shell" +print "------ -----" +FS=":" +} + +{ +print $1 " " $7 +} + +END { +print "This concludes the listing" +} + +#执行gawk命令截取/etc/passwd输出 +#gawk -f gawk.sh /etc/passwd diff --git a/codes/shell/示例脚本/查找替换文本/gawk/gawk函数库 b/codes/shell/示例脚本/查找替换文本/gawk/gawk函数库 new file mode 100644 index 0000000..c6673c1 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/gawk函数库 @@ -0,0 +1,12 @@ +#!/bin/bash + +function myprint() +{ + printf "%-16s - %s", $1, $4 +} + +function myrand(limit) +{ + return int(limit * rand()) +} + diff --git a/codes/shell/示例脚本/查找替换文本/gawk/gawk脚本 b/codes/shell/示例脚本/查找替换文本/gawk/gawk脚本 new file mode 100644 index 0000000..35597f9 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/gawk脚本 @@ -0,0 +1,6 @@ +#!/bin/bash + +BEGIN{FS="\n"; RS=""} +{ + myprint() +} diff --git a/codes/shell/示例脚本/查找替换文本/gawk/script b/codes/shell/示例脚本/查找替换文本/gawk/script new file mode 100644 index 0000000..da4681e --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/script @@ -0,0 +1,2 @@ +BEGIN{FS=","; print n} +{print $n} diff --git a/codes/shell/示例脚本/查找替换文本/gawk/test b/codes/shell/示例脚本/查找替换文本/gawk/test new file mode 100644 index 0000000..ec71abb --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/test @@ -0,0 +1,4 @@ +10 +5 +123 +50 diff --git a/codes/shell/示例脚本/查找替换文本/gawk/使用变量.sh b/codes/shell/示例脚本/查找替换文本/gawk/使用变量.sh new file mode 100644 index 0000000..32382c8 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/使用变量.sh @@ -0,0 +1,47 @@ +#!/bin/bash +#使用内建变量 + +# NF 当前记录的字段个数 +# NR 到目前为止读的记录数量 +#下面的程序在每行开头输出行号,并在最后输出文件的总字段数 +gawk '{ total+=NF; print NR, $0 }END{ print "Total: ", total}' + +gawk 'BEGIN {testing="This is a test"; print testing; testing=45; print testing}' + +#处理数字值 + +gawk 'BEGIN{x=4; x= x*2+3; printx}' + +#处理数组 +gawk 'BEGIN{capital["Ill"] = "SprintField"; print capital["Ill"]}' + +#遍历数组变量 +gawk 'BEGIN{ +var["a"] = 1 +var["g"] = 2 +var["m"] = 3 +for( test in var) +{ + print "Index:",test,"- Value:",var[test] +} +}' + +print "------" + +#删除数组变量 +gawk 'BEGIN{ +var["a"] = 1 +var["g"] = 2 +for (test in var) +{ + print "Index:",test," - Value:", var[test] +} +delete var["g"] + +print "----" + +for (test in var) +{ + print "Index;",test," - Value:", var[test] +} +}' diff --git a/codes/shell/示例脚本/查找替换文本/gawk/使用模式,结构化命令.sh b/codes/shell/示例脚本/查找替换文本/gawk/使用模式,结构化命令.sh new file mode 100644 index 0000000..4e2a3fc --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/使用模式,结构化命令.sh @@ -0,0 +1,57 @@ +#!/bin/bash +#正则表达式 + +gawk 'BEGIN{FS=","} +/11/{print $1} +' test + +#if-else语句 +gawk '{ +if($1 > 20) +{ + x=$1*20 + print x +} +else +{ + x=$1/2 + print x +} +}' test + +#while 语句 +gawk '{ +total = 0 +i=1 +while(i<4) +{ + total+=$i + i++ +} +avg = total/3 +print "Average:".avg +}' test + + +#do-while语句 +gawk '{ +total=0 +i=1 +do +{ + total += $i + i++ +}while(total < 150) +print total }' test + + +#for语句 +gawk '{ +total = 0 +for (i=1; i<4; i++) +{ + total+=$i +} +avg = total/3 +print "Average:".avg +}' test diff --git a/codes/shell/示例脚本/查找替换文本/gawk/自定义函数.sh b/codes/shell/示例脚本/查找替换文本/gawk/自定义函数.sh new file mode 100644 index 0000000..4f58519 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/自定义函数.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#gawk 自定义函数 + +gawk ' +function myprint() +{ + printf "%-16s - %s\n", $1, $4 +} +BEGIN{FS="\n"; RS=""} +{ + myprint() +}' test diff --git a/codes/shell/示例脚本/查找替换文本/gawk/调用函数库和脚本.sh b/codes/shell/示例脚本/查找替换文本/gawk/调用函数库和脚本.sh new file mode 100644 index 0000000..0ffaa67 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/调用函数库和脚本.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +#使用函数库和gawk脚本 + +gawk -f gawk函数库 -f gawk脚本 test diff --git a/codes/shell/示例脚本/查找替换文本/gawk/输出.sh b/codes/shell/示例脚本/查找替换文本/gawk/输出.sh new file mode 100644 index 0000000..3fff2fd --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/gawk/输出.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +#print用于产生简单输出 +#多个表达式的字符串值之间用输出字段分隔符分开 + +gawk '{ print $1, $2 }' + +#输出字段分割符与输出记录分隔符存储在内建变量OFS与ORS中, +#初始情况下,OFS与ORS被设置成一个空格符与一个换行符,但它们的值可以在任何时候改变 +#下面这个程序打印每一行的第1第2个字段,字段之间用分号分开,在每一行的第2个字段之后输出两个换行符 + +gawk 'BEGIN { OFS = ":"; ORS = "\n\n" } + { print $1, $2 }' + +#下面这个程序拼接第1个与第2个字段,两个字段之间没有输出字段分隔符插入 + +gawk '{ print $1 $2 }' + +#这两句话等价 + +gawk '{ print }' +gawk '{ print $0 }' + +#输出空行 + +gawk '{ print "" }' + + +#printf用于产生格式化输出 + +#printf不会自动换行,需要手动添加\n +#格式说明符以%开始,以转换字符结束 +# - 表达式在它的域内左对齐,没有则右对齐 +# width 为了达到规定的宽度,必要时填充空格 +# .prec 字符串最大宽度, 或十进制数的小数部分的位数 + +gawk '{ printf ("Name:%-10sAge:%-5dWeight:%7.2f\n", $1, $2, $3) }' + + +#输出到文件 +#重定向运算符>与>>用于将输出重定向到文件,文件名必须用双引号括起来 + +#下面这个程序将所有输入行的第1个与第3个字段输出到两个文件中:如果第3个字段大于100,则输出到bigpop,否则输出到smallpop +gawk '{ print($1, $3) > ($3 > 100 ? "bigpop" : "smallpop") }' + + +#输出到管道 +#print的输出将以管道的方式传递给command + +# Canada 3852 +# China 3705 +# USA 3615 +# Brazil 3286 + +gawk '{ pop[$1]+=$2 } +END{ for(c in pop) printf("%15-s%6d\n", c, pop[c]) | "sort -nk 2"; close("sort -nk 2") }' + + +#关闭文件与管道 +#语句close(expression)关闭一个文件或管道,文件或管道由expression指定。 +#expression的字符串值必须与最初用于创建文件或管道的字符串值相同。 +#在同一个程序中,如果你写了一个文件,而待会儿想要读取它,那么就需要调用close。 +#某一时刻,同时处于打开状态的文件或管道数量最大值由实现定义。 + +close("sort -nk 2") + diff --git a/codes/shell/示例脚本/查找替换文本/regex/目录文件计数.sh b/codes/shell/示例脚本/查找替换文本/regex/目录文件计数.sh new file mode 100644 index 0000000..689ca6f --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/regex/目录文件计数.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# count number of files in your PATH + +mypath=`echo $PATH | sed 's/:/ /g'` +count=0 +for directory in $mypath +do + check=`ls $directory` + echo $check + for item in $check + do + count=$[ $count + 1 ] + done + echo "$directory - $count" + count=0 +done + diff --git a/codes/shell/示例脚本/查找替换文本/regex/邮件验证.sh b/codes/shell/示例脚本/查找替换文本/regex/邮件验证.sh new file mode 100644 index 0000000..1857ac8 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/regex/邮件验证.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +#验证邮件 + +gawk --re-interval '/^([a-zA-Z0-9_\-\.\+]+)@([a-zA-Z0-9_\-\+]+)\.([a-zA-Z]{2,5})/{print $0}' + + diff --git a/codes/shell/示例脚本/查找替换文本/sed/sed文件操作.sh b/codes/shell/示例脚本/查找替换文本/sed/sed文件操作.sh new file mode 100644 index 0000000..046001e --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/sed文件操作.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +#向文件写入 +sed '1,2w test1' test1 + +echo -e "next\n" + +#从文件读取 +sed '3r ./test' ./test + +echo -e "next\n" + +#从文件读取,并插入字符流 +sed '/lazy/r test' test + +#向数据流末尾添加数据 +sed '$r test' test + +echo -e "next1\n" + +sed '/lazy/ { +r test +d +}' test diff --git a/codes/shell/示例脚本/查找替换文本/sed/sed编辑器基础.sh b/codes/shell/示例脚本/查找替换文本/sed/sed编辑器基础.sh new file mode 100644 index 0000000..011c93f --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/sed编辑器基础.sh @@ -0,0 +1,104 @@ +#!/bin/bash +#sed编辑器基础 + +#替换标记 +sed 's/lazy/ht/' ./test + +echo -e "next\n" + +#可用的替换标记 +#1.数字 表明新闻本将替换第几处模式匹配的地方 +sed 's/lazy/ht/2' ./test +#2.g 表明新文件将会替换所有已有文本出现的地方 +sed 's/lazy/ht/g' ./test +#3.p 表明原来行的内容要打印出来,替换后的 +sed 's/lazy/ht/p' ./test +#4.w file 将替换的结果写到文件中 +sed 's/lazy/ht/w test1' ./test + +echo -e "next\n" + +#替换字符 +sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd +#或者 +sed 's!/bin/bash!/bin/csh!' /etc/passwd + +echo -e "next\n" + +#使用地址 +#1.数字方式的行寻址 +sed '2s/lazy/cat/' ./test +sed '2,3s/lazy/cat/' ./test +sed '2,$s/lazy/cat/' ./test +#2.使用文本模式过滤器 +sed '/tiandi/s/bash/csh/' /etc/passwd + +echo -e "next\n" + +#组合命令 +sed '2{ +s/fox/elephant/ +s/dog/cat/ +}' test +sed '2,${ +s/fox/elephant/ +s/dog/cat/ +}' test + +echo -e "next\n" + +#删除行 +sed '3d' ./test +sed '2,$d' ./test +sed '/number 1/d' ./test +#删除两个文本模式来删除某个范围的行,第一个开启删除功能,第二个关闭删除功能 +sed '/1/,/3/d' ./test + +echo -e "next\n" + +#插入和附加文本 +sed '3i\ +This is an appended line.' ./test + +sed '$a\ +This is a new line of text.' ./test + +#修改行 +sed '3c\ +This a changed line of text.' ./test +sed '/number 1/c\ +This a changed line of text.' ./test +#替换两行文本 +#sed '2,3c\ +#This a changed line of text.' ./test + +#转换命令,处理单个字符 +#sed 'y/123/789/' ./test + +echo -e "next\n" + +#回顾打印 +# p 打印文本行 +# -n 禁止其他行,只打印包含匹配文本模式的行 +sed -n '/number 3/p' ./test + +#查看修改之前的行和修改之后的行 +#sed -n '/3/{ +#p +#s/line/test/p +#}' ./test + +echo -e "next\n" + +# 打印行号 +sed '=' ./test + +#打印指定的行和行号 +#sed -n '/lazy/{ +#= +#p +#}' ./test + +#列出行 打印数据流中的文本和不可打印的ASCII字符,任何不可打印的字符都用它们的八进制值前加一个反斜线或标准C风格的命名法,比如用\t来代表制表符 +sed -n 'l' ./test + diff --git a/codes/shell/示例脚本/查找替换文本/sed/test b/codes/shell/示例脚本/查找替换文本/sed/test new file mode 100644 index 0000000..61a4ee0 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/test @@ -0,0 +1,5 @@ +This is the header line +This is the first data line +This is the data line +This is the second data line +line This is the last line diff --git a/codes/shell/示例脚本/查找替换文本/sed/保持空间.sh b/codes/shell/示例脚本/查找替换文本/sed/保持空间.sh new file mode 100644 index 0000000..3c75f7d --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/保持空间.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +#h将模式空间保存到保持空间 +#H将模式空间附加到保持空间 +#g将保持空间复制到模式空间 +#G将保持空间保存到模式空间 +#x交换模式空间和保持空间的内容 + +sed -n '/first/{ +h +p +n +p +g +p +}' test diff --git a/codes/shell/示例脚本/查找替换文本/sed/删除指定的空白行和删除html标签.sh b/codes/shell/示例脚本/查找替换文本/sed/删除指定的空白行和删除html标签.sh new file mode 100644 index 0000000..2dcdd57 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/删除指定的空白行和删除html标签.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +#多个空格只保留一个 +#sed '/./,/^$/!d' test + +#删除开头的空白行 +#sed '/./,$!d' test + +#删除结尾的空白行 +sed '{ +:start +/^\n*$/{$d; N; b start} +}' test + +#删除html标签 +#有问题 +#s/<.*>//g + +#sed 's/<[^>]*>//g' test1 + +#sed 's/<[^>]*>//g;/^$/d' test1 diff --git a/codes/shell/示例脚本/查找替换文本/sed/在脚本中使用sed.sh b/codes/shell/示例脚本/查找替换文本/sed/在脚本中使用sed.sh new file mode 100644 index 0000000..41afaf1 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/在脚本中使用sed.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# shell wrapper for sed editor script to reverse lines + +sed -n '{ +1!G +h +$p +}' $1 diff --git a/codes/shell/示例脚本/查找替换文本/sed/排除命令.sh b/codes/shell/示例脚本/查找替换文本/sed/排除命令.sh new file mode 100644 index 0000000..9d1c0b3 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/排除命令.sh @@ -0,0 +1,8 @@ +!/bin/bash + +#排除命令,使本来起作用的命令不起作用 + +sed -n '/heade/!p' test + +#反转文本文件 +sed -n '{1!G ; h; $p}' test diff --git a/codes/shell/示例脚本/查找替换文本/sed/模式替代.sh b/codes/shell/示例脚本/查找替换文本/sed/模式替代.sh new file mode 100644 index 0000000..8682f6e --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/模式替代.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +#and符号,代表替换命令中的匹配模式,不管预定义模式是什么文本,都可以用and符号替换,and符号会提取匹配替换命令中指定替换模式中的所有字符串 +echo "The cat sleeps in his hat" | sed 's/.at/"&"/g' + +#替换单独的单词 +echo "The System Administrator manual" | sed 's/\(System\) Administrator/\1 user/' + +#在长数字中插入逗号 +echo "1234567" | sed '{:start; s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/; t start}' + diff --git a/codes/shell/示例脚本/查找替换文本/sed/测试.sh b/codes/shell/示例脚本/查找替换文本/sed/测试.sh new file mode 100644 index 0000000..65ef706 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/测试.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +#测试,如果测试成功,如果没有标签,sed会跳转到结尾,如果有标签,就跳转到标签,如果测试失败,则不会跳转 +sed -n '{s/first/matched/; t; s/This is the/No match on/}' test diff --git a/codes/shell/示例脚本/查找替换文本/sed/给文件中的行编号.sh b/codes/shell/示例脚本/查找替换文本/sed/给文件中的行编号.sh new file mode 100644 index 0000000..07bd0e6 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/给文件中的行编号.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +sed '=' test | sed 'N; s/\n/ /' diff --git a/codes/shell/示例脚本/查找替换文本/sed/跳转.sh b/codes/shell/示例脚本/查找替换文本/sed/跳转.sh new file mode 100644 index 0000000..69eff6b --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/跳转.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +#跳转到指定脚本 +sed '{/first/b jump1; s/This is the/No jump on/; :jump1; s/This is the/Jump here on/}' test + +#跳转到开头,删除每一个逗号,并保证删除最后一个逗号之后,跳出循环 +sed -n '{:start; s/,//1p; /,/b start}' test diff --git a/codes/shell/示例脚本/查找替换文本/sed/输出末尾指定行数的数据.sh b/codes/shell/示例脚本/查找替换文本/sed/输出末尾指定行数的数据.sh new file mode 100644 index 0000000..eb0f2ad --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/输出末尾指定行数的数据.sh @@ -0,0 +1,10 @@ +#!/bin/bash +#输出末尾10行数据 + +sed '{ +:start +$q +N +11,$D +b start +}' /etc/passwd diff --git a/codes/shell/示例脚本/查找替换文本/sed/重定向sed输出.sh b/codes/shell/示例脚本/查找替换文本/sed/重定向sed输出.sh new file mode 100644 index 0000000..ec05cf1 --- /dev/null +++ b/codes/shell/示例脚本/查找替换文本/sed/重定向sed输出.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# add commas to numbers in factorial answer + +factorial=1 +counter=1 +number=$1 + +while [ $counter -le $number ] +do + factorial=$[ $factorial * $counter ] + counter=$[ $counter + 1 ] +done + +result=`echo $factorial | sed '{ +:start +s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/ +t start +}'` + +echo "The result is $result" diff --git a/codes/shell/示例脚本/脚本函数/从函数返回数组.sh b/codes/shell/示例脚本/脚本函数/从函数返回数组.sh new file mode 100644 index 0000000..4425f55 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/从函数返回数组.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# returning an array value + +function arraydblr { + local origarry + local newarray + local elements + local i + origarry=(`echo "$@"`) + newarray=(`echo "$@"`) + elements=$[ $# - 1 ] + for (( i=0; i<=$elements; i++ )) + { + newarray[$i]=$[ ${origarry[$i]} * 2 ] + } + + echo ${newarray[*]} +} + +myarray=(1 2 3 4 5) +echo "The original array is : ${myarray[*]}" +arg1=`echo ${myarray[*]}` +result=(`arraydblr $arg1`) +echo "The new array is : ${result[*]}" diff --git a/codes/shell/示例脚本/脚本函数/使用return命令.sh b/codes/shell/示例脚本/脚本函数/使用return命令.sh new file mode 100644 index 0000000..412b86a --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/使用return命令.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# using the return command in a function + +function db1 { + read -p "Enter a value:" value + echo "doubling the value" + return $[ $value * 2 ] +} + +db1 +echo "The new value is $?" diff --git a/codes/shell/示例脚本/脚本函数/使用全局变量带来的问题.sh b/codes/shell/示例脚本/脚本函数/使用全局变量带来的问题.sh new file mode 100644 index 0000000..2758588 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/使用全局变量带来的问题.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# demonstrating a bad use of variables + +function func1 { + temp=$[ $value + 5 ] + result=$[ $temp * 2 ] +} + +temlp=4 +value=6 + +func1 +echo "The result is $result" + +if [ $temp -gt $value ] +then + echo "Temp is larger" +else + echo "temp is smaller" +fi diff --git a/codes/shell/示例脚本/脚本函数/使用函数输出.sh b/codes/shell/示例脚本/脚本函数/使用函数输出.sh new file mode 100644 index 0000000..4959d64 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/使用函数输出.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# using the echo to return a value + +function db1 { + read -p "Enter a value:" value + echo $[ $value*2 ] +} + +result=`db1` +echo "The new value is $result" + diff --git a/codes/shell/示例脚本/脚本函数/使用命令行中传递的参数.sh b/codes/shell/示例脚本/脚本函数/使用命令行中传递的参数.sh new file mode 100644 index 0000000..d31fe8b --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/使用命令行中传递的参数.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# using a global variable to pass a value + +function db1 { + # $1和$2 不能从命令行中传递,只能调用函数时,手动传递 + echo $[ $1 * $2 ] +} + +if [ $# -eq 2 ] +then + value=`db1 $1 $2` + echo "The result is $value" +else + echo "Usage: badtest1 a b" +fi diff --git a/codes/shell/示例脚本/脚本函数/使用局部变量.sh b/codes/shell/示例脚本/脚本函数/使用局部变量.sh new file mode 100644 index 0000000..198bd81 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/使用局部变量.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# demonstrating the local keyword + +function func1 { + local temp=$[ $value +5 ] + result=$[ $temp * 2 ] +} + +temp=4 +value=6 + +func1 + +echo "The result is $result" +if [ $temp -gt $value ] +then + echo "temp is larger" +else + echo "temp is smaller" +fi diff --git a/codes/shell/示例脚本/脚本函数/使用库函数.sh b/codes/shell/示例脚本/脚本函数/使用库函数.sh new file mode 100644 index 0000000..4b048c3 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/使用库函数.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +#using a library file the wrong way + +. ./脚本库.sh + +result=`addem 10 15` +echo "The result is $result" diff --git a/codes/shell/示例脚本/脚本函数/全局变量.sh b/codes/shell/示例脚本/脚本函数/全局变量.sh new file mode 100644 index 0000000..c4c3d70 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/全局变量.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# using a global variable to pass a value + +function db1 { + value=$[ $value * 2 ] +} + +read -p "Enter a value: " value +db1 +echo "The new value is : $value" + diff --git a/codes/shell/示例脚本/脚本函数/函数递归.sh b/codes/shell/示例脚本/脚本函数/函数递归.sh new file mode 100644 index 0000000..b362092 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/函数递归.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +function factorial { + if [ $1 -eq 1 ] + then + echo 1 + else + local temp=$[ $1 -1 ] + local result=`factorial $temp` + echo $[ $result * $1 ] + fi +} + +read -p "Please input a value: " value +result=`factorial $value` +echo "The factorial of $value is: $result" diff --git a/codes/shell/示例脚本/脚本函数/在函数中使用参数.sh b/codes/shell/示例脚本/脚本函数/在函数中使用参数.sh new file mode 100644 index 0000000..8ea4b1f --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/在函数中使用参数.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# passing parameters to a function + +function addem { + if [ $# -eq 0 ] || [ $# -gt 2 ] + then + echo -1 + elif [ $# -eq 1 ] + then + echo $[ $1 + $1 ] + else + echo $[ $1 + $2 ] + fi +} + +echo -n "Adding 10 and 15:" +value=`addem 10 15` +echo $value + +echo -n "Let's try adding just one number: " +value=`addem 10` +echo $value + +echo -n "Now trying adding no number: " +value=`addem` +echo $value + +echo -n "Finally, try adding three or more numbers: " +value=`addem 10 15 20` +echo $value + diff --git a/codes/shell/示例脚本/脚本函数/基本的脚本函数.sh b/codes/shell/示例脚本/脚本函数/基本的脚本函数.sh new file mode 100644 index 0000000..e881e97 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/基本的脚本函数.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# using a function in script + +function func1 { + echo "This is an example of a function" +} + +count=1 +while [ $count -le 5 ] +do + func1 + count=$[ $count+1 ] +done +echo "This is the end of the loop" +func1 +echo "Now this is the end of the script" diff --git a/codes/shell/示例脚本/脚本函数/想函数传数组数据.sh b/codes/shell/示例脚本/脚本函数/想函数传数组数据.sh new file mode 100644 index 0000000..ab8b48d --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/想函数传数组数据.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# trying to pass an array variable + +function testit { + echo "The parameters are : $@" + + #函数只会读取数组变量的第一个值 + thisarray=$1 + echo "The received array is ${thisarray[*]}" + + local newarray + newarray=(`echo "$@"`) + echo "The new array value is : ${newarray[*]}" +} + +myarray=(1 2 3 4 5) +echo "The original array is : ${myarray[*]}" + +#将数组变量当成一个函数参数,函数只会去函数变量第一个值 +#testit $myarray + +testit ${myarray[*]} diff --git a/codes/shell/示例脚本/脚本函数/累加数组中的值.sh b/codes/shell/示例脚本/脚本函数/累加数组中的值.sh new file mode 100644 index 0000000..266f77c --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/累加数组中的值.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +#adding values in the array + +function addarray { + local sum=0 + local newarray + newarray=(`echo "$@"`) + for value in ${newarray[*]} + do + sum=$[ $sum + $value ] + done + echo $sum +} + +myarray=(1 2 3 4 5) +echo "The original array is : ${myarray[*]}" +arg1=`echo ${myarray[*]}` +result=`addarray $arg1` +echo "The result is $result" diff --git a/codes/shell/示例脚本/脚本函数/脚本库.sh b/codes/shell/示例脚本/脚本函数/脚本库.sh new file mode 100644 index 0000000..1c52b0f --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/脚本库.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# myscript functions + +function addem { + echo $[ $1 + $2 ] +} + +function multem { + echo $[ $1 * $2 ] +} + +function divem { + if [ $2 -ne 0] + then + echo $[ $1/$2 ] + else + echo -1 + fi +} diff --git a/codes/shell/示例脚本/脚本函数/默认退出状态码.sh b/codes/shell/示例脚本/脚本函数/默认退出状态码.sh new file mode 100644 index 0000000..bd2d5b4 --- /dev/null +++ b/codes/shell/示例脚本/脚本函数/默认退出状态码.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# testing the exit status of a function + +func1() { + echo "Trying to display a non-existent file" + ls -l badfile +} + +#由于最后一条命令未执行成功,返回的状态码非0 +echo "testing the function" +func1 +echo "The exit status is : $?" + +func2() { + ls -l badfile + echo "Another test to display a non-existent file" +} + +#由于最后一条命令echo执行成功,返回的状态码为0 +echo "Another test" +func2 +echo "The exit status is : $?" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/codes/shell/示例脚本/菜单/使用msgbox部件.sh b/codes/shell/示例脚本/菜单/使用msgbox部件.sh new file mode 100644 index 0000000..c9aa826 --- /dev/null +++ b/codes/shell/示例脚本/菜单/使用msgbox部件.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +dialog --title text --msgbox "This is a test" 10 20 diff --git a/codes/shell/示例脚本/菜单/使用select命令.sh b/codes/shell/示例脚本/菜单/使用select命令.sh new file mode 100644 index 0000000..536a896 --- /dev/null +++ b/codes/shell/示例脚本/菜单/使用select命令.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# using select in the menu + +function diskspace { + clear + df -k +} + +function whoseon { + clear + who +} + +function menusage { + clear + cat /proc/meminfo +} + +PS3="Enter option:" +select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit program" +do + case $option in + "Exit program") + break;; + "Display disk space") + diskspace;; + "Display logged on users") + whoseon;; + "Display memory usage") + menusage;; + *) + clear + echo "Sorry, wrong selection";; + esac +done +clear diff --git a/codes/shell/示例脚本/菜单/使用脚本菜单.sh b/codes/shell/示例脚本/菜单/使用脚本菜单.sh new file mode 100644 index 0000000..8634404 --- /dev/null +++ b/codes/shell/示例脚本/菜单/使用脚本菜单.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +function menu { + clear + echo + echo -e "\t\tSys Admin Menu\n" + echo -e "\t1. Display disk space" + echo -e "\t2. Display logged on users" + echo -e "\t3. Display memory usage" + echo -e "\t0. Exit program\n\n" + echo -en "\t\tEnter option:" + read -n 1 option +} + +function diskspace { + clear + df -k +} + +function whoseon { + clear + who +} + +function menusage { + clear + cat /proc/meminfo +} + +while [ 1 ] +do + menu + case $option in + 0) + break;; + 1) + diskspace;; + 2) + whoseon;; + 3) + menusage;; + *) + clear + echo "Sorry, wrong selection";; + esac + echo -en "\n\n\t\tHit any key to continue" + read -n 1 line +done +clear diff --git a/codes/shell/示例脚本/菜单/在脚本中使用dialog命令.sh b/codes/shell/示例脚本/菜单/在脚本中使用dialog命令.sh new file mode 100644 index 0000000..eceb20b --- /dev/null +++ b/codes/shell/示例脚本/菜单/在脚本中使用dialog命令.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# using dialog to create a menu + +temp=`mktemp -t test.XXXXXX` +temp2=`mktemp -t test2.XXXXXX` + +function diskspace { + df -k > $temp + dialog --textbox $temp 20 60 +} + +function whoseon { + who > $temp + dialog --textbox $temp 20 50 +} + +function menusage { + cat /proc/meminfo > $temp + dialog --textbox $temp 20 50 +} + +while [ 1 ] +do + dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2> $temp2 + if [ $? -eq 1 ] + then + break + fi + + selection=`cat $temp2` + + case $selection in + 1) + diskspace;; + 2) + whoseon;; + 3) + menusage;; + 0) + break;; + *) + dialog --msgbox "Sorry,invalid selection" 10 30 + esac +done +rm -f $temp 2> /dev/null +rm -f $temp2 2> /dev/null diff --git a/codes/shell/示例脚本/输入和输出/test b/codes/shell/示例脚本/输入和输出/test new file mode 100644 index 0000000..6947bc4 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/test @@ -0,0 +1,2 @@ +ls: cannot access badtest: No such file or directory +but this should go to the testerror file diff --git a/codes/shell/示例脚本/输入和输出/test1 b/codes/shell/示例脚本/输入和输出/test1 new file mode 100644 index 0000000..fffebad --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/test1 @@ -0,0 +1 @@ +This is the end of the script diff --git a/codes/shell/示例脚本/输入和输出/临时重定向.sh b/codes/shell/示例脚本/输入和输出/临时重定向.sh new file mode 100644 index 0000000..4681abe --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/临时重定向.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# testing STDERR messages + +echo "This is an error " >&2 +echo "This is another error" +echo "This is also an error" >&2 diff --git a/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh b/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh new file mode 100644 index 0000000..015c852 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# reading data from a file + +count=1 +cat test | while read line +do + echo "Line $count: $line" + count=$[ $count + 1 ] +done +echo "Finished processing the file" + diff --git a/codes/shell/示例脚本/输入和输出/使用getopts.sh b/codes/shell/示例脚本/输入和输出/使用getopts.sh new file mode 100644 index 0000000..6338939 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/使用getopts.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# simple demonstration of the getopts command + +while getopts :ab:c opt +do + case "$opt" in + a) echo "Found the -a option";; + b) echo "Found the -b option, with value $OPTARG";; + c) echo "Found the -c option";; + *) echo "Unknown option:$opt";; + esac +done diff --git a/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh b/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh new file mode 100644 index 0000000..732ac69 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# processing options and parameters with getopts + +while getopts :ab:cd opt +do + case "$opt" in + a) echo "Found the -a option";; + b) echo "Found the -b option,with value $OPTARG";; + c) echo "Found the -c option";; + d) echo "Found the -d option";; + *) echo "Unknown option: $opt";; + esac +done +shift $[ $OPTIND - 1 ] +count=1 +for param in "$@" +do + echo "Parameter $count: $param" + count=$[ $count + 1 ] +done diff --git a/codes/shell/示例脚本/输入和输出/使用getopt命令.sh b/codes/shell/示例脚本/输入和输出/使用getopt命令.sh new file mode 100644 index 0000000..55c9867 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/使用getopt命令.sh @@ -0,0 +1,25 @@ +#!/bin/bash +#extracting command line options and values with getopt +# getopt command is not goot at dealing with space,we can use getopts +set -- `getopt -q ab:c "$@"` +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) param="$2" + echo "Found the -b option,with parameter value $param" + shift;; + -c) echo "Found the -c option";; + --) shift + break;; + *) echo "$1 is not an option";; + esac + shift +done + +count=1 +for param in "$@" +do + echo "Parameter #$count: $param" + count=$[ $count+1 ] +done diff --git a/codes/shell/示例脚本/输入和输出/使用shift命令.sh b/codes/shell/示例脚本/输入和输出/使用shift命令.sh new file mode 100644 index 0000000..11877cf --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/使用shift命令.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# shift n 移动变量 + +count=1 +while [ -n "$1" ] +do + echo "Parameter #$count = $1" + count=$[ $count+1 ] + shift +done + +echo -e "\n" + +# demonstrating a multi-position shift +echo "The original parameters : $*" +shift 2 +echo "Here's the new first parameter: $1" diff --git a/codes/shell/示例脚本/输入和输出/分离参数和选项.sh b/codes/shell/示例脚本/输入和输出/分离参数和选项.sh new file mode 100644 index 0000000..ee3da94 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/分离参数和选项.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +#extracting options and parameters + +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) echo "Found the -b option";; + -c) echo "Found the -c option";; + --) shift + break;; + *) echo "$1 is not an option";; + esac + shift +done + +count=1 +for param in $@ +do + echo "Parameter #$count: $param" + count=$[ $count + 1 ] +done diff --git a/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh b/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh new file mode 100644 index 0000000..417bfa4 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# testing lsof with file descriptors + +exec 3>test +exec 6>test +exec 7 $tempfile1 +exec 8> $tempfile2 + +echo "Sending data to directory $tempdir" +echo "This is a test line of data for $tempfile1" >&7 +echo "This is a test line of data for $tempfile2" >&8 + diff --git a/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh b/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh new file mode 100644 index 0000000..6d15a9d --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# creating and using a temp file + +tempfile=`mktemp test.XXXXXX` + +exec 3>$tempfile + +echo "This script writes to temp file $tempfile" + +echo "This is the first line" >&3 +echo "This is the second line" >&3 +echo "This is the last line" >&3 + +exec 3>&- + +echo "Done creating temp file. The contents are:" + +cat $tempfile + +rm -f $tempfile 2>/dev/null + diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh new file mode 100644 index 0000000..0face23 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +#storing STDOUT, then coming back to it + +exec 3>&1 +exec 1>test + +echo "This should store in output file" +echo "along with this line" + +exec 1>&3 + +echo "Now things should be back to normal" diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh new file mode 100644 index 0000000..154e2bf --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# testing closing file descriptors + +exec 3>test +echo "This is a test line of data" >&3 + +# closing file descriptor +exec 3>&- + +echo "This won't work" >&3 + +cat test + +#覆盖前一个test文件 +exec 3>test +echo "This'll be bad" >&3 diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh new file mode 100644 index 0000000..53b7645 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# testing inpiut/output file descriptor + +exec 3<> test +read line <&3 +echo "Read: $line" +echo "This is the test line" >&3 diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh new file mode 100644 index 0000000..c47be1b --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# redirecting input file descriptors + +exec 3>&1 +echo "This is the 3 file descriptor" >&3 + +exec 6>&0 +exec 0test + +echo "This should display on the monitor" +echo "and this should be stored in the file" >&3 +echo "Then this should be back on the monitor" diff --git a/codes/shell/示例脚本/输入和输出/参数计数.sh b/codes/shell/示例脚本/输入和输出/参数计数.sh new file mode 100644 index 0000000..65673e8 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/参数计数.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# getting the number of parameters + +echo There were $# parameters supplied + +#花括号里不能使用美元符号 +params=$# + +echo The last parameter is $params +echo The last parameter is ${!#} diff --git a/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh b/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh new file mode 100644 index 0000000..a4497a1 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# creating a temp file in /tmp + +tempfile=`mktemp -t tmp.XXXXXX` + +echo "This is a test file" > $tempfile +echo "This is the second line of the test" >> $tempfile + +echo ”The temp is locate at : $tempfile“ +cat $tempfile +rm -f $tempfile diff --git a/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh b/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh new file mode 100644 index 0000000..c3105fe --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# redirecting the inpiut + +# 从test中读取数据,而不是从STDIN中读取数据 +exec 0< test +count=1 +while read line +do + echo "Line #$count : $line " + count=$[ $count +1 ] +done + diff --git a/codes/shell/示例脚本/输入和输出/处理带值的选项.sh b/codes/shell/示例脚本/输入和输出/处理带值的选项.sh new file mode 100644 index 0000000..b8cc631 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/处理带值的选项.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# extracting command line options and values + +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) param="$2" + echo "Found the -b option, with parameter value $param" + shift;; + -c) echo "Found the -c option";; + --) shift + break;; + *) echo "$1 is not an option";; + esac + shift +done + +count=1 +for param in "$@" +do + echo "Parameter #$count : $param" + count=$[ $count + 1 ] +done diff --git a/codes/shell/示例脚本/输入和输出/处理简单选项.sh b/codes/shell/示例脚本/输入和输出/处理简单选项.sh new file mode 100644 index 0000000..60dd404 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/处理简单选项.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# extracting command line options as parameters + +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) echo "Found the -b optins";; + -c) echo "Found the -c optins";; + *) echo "$1 is not a valid options";; + esac + shift +done diff --git a/codes/shell/示例脚本/输入和输出/快速清除文件或日志.sh b/codes/shell/示例脚本/输入和输出/快速清除文件或日志.sh new file mode 100644 index 0000000..bd9fa64 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/快速清除文件或日志.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# 清除日志 + +cat /dev/null > [Logname] diff --git a/codes/shell/示例脚本/输入和输出/抓取所有数据.sh b/codes/shell/示例脚本/输入和输出/抓取所有数据.sh new file mode 100644 index 0000000..a93ccf0 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/抓取所有数据.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# testing $* and $@ + +count=1 +for param in "$*" +do + echo "\$* Parameter #$count = $param" + count=$[ $count+1 ] +done + +count=1 +for param in "$@" +do + echo "\$@ Paramenter #$count = $param" + count=$[ $count+1 ] +done diff --git a/codes/shell/示例脚本/输入和输出/永久重定向.sh b/codes/shell/示例脚本/输入和输出/永久重定向.sh new file mode 100644 index 0000000..d03a3bd --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/永久重定向.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# testing STDERR messages +# redirecting all to a file + +# 脚本执行期间,用exec命令告诉shell重定向某个特定文件描述符 +exec 2>test + +ls badtest +echo "This is test of redirecting all output" +echo "from a script to another file" + +exec 1>test1 +echo "This is the end of the script" +echo "but this should go to the testerror file" >&2 diff --git a/codes/shell/示例脚本/输入和输出/测试.txt b/codes/shell/示例脚本/输入和输出/测试.txt new file mode 100644 index 0000000..543f1b1 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/测试.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/codes/shell/示例脚本/输入和输出/获取用户输入.sh b/codes/shell/示例脚本/输入和输出/获取用户输入.sh new file mode 100644 index 0000000..4e36186 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/获取用户输入.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# testing the reading command + +echo -n "Enter your name:" +read name +echo "Hello $name, welcome to my program" + +read -p "Please enter your age: " age +days=$[ $age * 365 ] +echo "That makes you over $days days old" + +#指定多个变量,输入的每个数据值都会分配给表中的下一个变量,如果用完了,就全分配各最后一个变量 +read -p "Please enter name:" first last +echo "Checking data for $last. $first..." + +#如果不指定变量,read命令就会把它收到的任何数据都放到特殊环境变量REPLY中 +read -p "Enter a number:" +factorial=1 +for (( count=1; count<=$REPLY; count++)) +do + factorial=$[ $factorial * $count ] +done +echo "The factorial of $REPLY is $factorial" + + + diff --git a/codes/shell/示例脚本/输入和输出/记录信息.sh b/codes/shell/示例脚本/输入和输出/记录信息.sh new file mode 100644 index 0000000..84d31db --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/记录信息.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# using the tee command for logging +#将输入一边发送到STDOUT,一边发送到日志文件 +tempfile=test +echo "This is the start of the test" | tee $tempfile +echo "This is the second line of the test" | tee -a $tempfile +echo "This is the end line of the test" | tee -a $tempfile + + diff --git a/codes/shell/示例脚本/输入和输出/读取参数.sh b/codes/shell/示例脚本/输入和输出/读取参数.sh new file mode 100644 index 0000000..1a37a7c --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/读取参数.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# using one command line parameter + +factorial=1 +for (( number = 1; number <= $1; number++)) +do + factorial=$[ $factorial * $number ] +done +echo The factor of $1 is $factorial diff --git a/codes/shell/示例脚本/输入和输出/读取多个命令行参数.sh b/codes/shell/示例脚本/输入和输出/读取多个命令行参数.sh new file mode 100644 index 0000000..6d2efcf --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/读取多个命令行参数.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# handing lots of parameters + +total=$[ ${10} * ${11} ] +echo The tenth parameter is ${10} +echo The eleventh parameter is ${11} +echo The total is $total diff --git a/codes/shell/示例脚本/输入和输出/读取程序名.sh b/codes/shell/示例脚本/输入和输出/读取程序名.sh new file mode 100644 index 0000000..f148da0 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/读取程序名.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# testing the $0 parameter + +echo The command entered is $0 + +#当传给$0变量的真实字符串是整个脚本的路径是,程序中就会使用整个路径,而不仅仅是程序名 + +name=`basename $0` +echo The command entered is $name diff --git a/codes/shell/示例脚本/输入和输出/超时和输入计数.sh b/codes/shell/示例脚本/输入和输出/超时和输入计数.sh new file mode 100644 index 0000000..7dc8387 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/超时和输入计数.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# timing the data entry + +if read -t 5 -p "Please enter your name:" name +then + echo "Hello, $name, welcome to my script" +else + #起到换行的作用 + echo + #输入计数 -n1 + read -n1 -p "Do you want to continue [Y/N]?" answer + case $answer in + Y | y) echo + echo "Fine, continue on...";; + N | n) echo + echo "OK,goodbye";; + *) echo + echo "OK, wrong, goodbye" + esac + echo "Sorry, this is the end of the script" +fi + diff --git a/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh b/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh new file mode 100644 index 0000000..1299be2 --- /dev/null +++ b/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# hiding input data from monitor + +read -s -p "Please enter your password: " pass + +#添加了-s选项之后,不会自动换行,不添加-s 会自动换行 +echo +echo "Is your password really $pass?" diff --git a/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh b/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh new file mode 100644 index 0000000..8505945 --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Capture_Stats - Gather System Performance Statistics +# +######################################################### +# +# Set Script Variables +# +REPORT_FILE=/home/tiandi/Documents/capstats.csv +DATE=`date +%m/%d/%y` +TIME=`date +%k:%M:%S` +# +############################################################ +# +USERS=`uptime | sed 's/user.*$//' | gawk '{print $NF}'` +LOAD=`uptime | gawk '{print $NF}'` +# +FREE=`vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4}'` +IDLE=`vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $15}'` +# +########################################## +# +echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $REPORT_FILE +# diff --git a/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh b/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh new file mode 100644 index 0000000..07ba5ba --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# +uptime | sed 's/user.*$//' | gawk '{print $NF}' diff --git a/codes/shell/示例脚本/进阶脚本/查看僵尸进程.sh b/codes/shell/示例脚本/进阶脚本/查看僵尸进程.sh new file mode 100644 index 0000000..849cbe4 --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/查看僵尸进程.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +#查看僵尸进程 +ps -al | gawk '{print $2,$4}' | grep Z diff --git a/codes/shell/示例脚本/进阶脚本/查看内存使用百分比.sh b/codes/shell/示例脚本/进阶脚本/查看内存使用百分比.sh new file mode 100644 index 0000000..1b5ddfd --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/查看内存使用百分比.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +#查看内存使用百分比 +free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/' diff --git a/codes/shell/示例脚本/进阶脚本/查看磁盘使用百分比.sh b/codes/shell/示例脚本/进阶脚本/查看磁盘使用百分比.sh new file mode 100644 index 0000000..08c064a --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/查看磁盘使用百分比.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +#查看磁盘实用百分比 +df -h /dev/sda1 | sed -n '/% \//p' | gawk '{ print $5 }' diff --git a/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh b/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh new file mode 100644 index 0000000..35fce81 --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# +# Report_Stats - Generates Rpt from Captured Perf Stats +# +############################################################ +# +# Set Script Variables +# +REPORT_FILE=/home/tiandi/Documents/capstats.csv +TEMP_FILE=/home/tiandi/Documents/capstats.html +# +DATE=`date +%m/%d/%y` +# +MAIL=`which mutt` +MAIL_TO=tiandi +# +###############################################################3 +# +# Create Report Header +# +echo "

Report for $DATE

" > $TEMP_FILE +echo "" >> $TEMP_FILE +echo "" >> $TEMP_FILE +echo "" >> $TEMP_FILE +# +############################################################### +# +# Place Performance Stats in Report +# +cat $REPORT_FILE | gawk -F, '{ +printf "", $1, $2, $3; +printf "\n\n", $4, $5, $6; +}' >> $TEMP_FILE +# +echo "
DateTimeUsersLoadFree Memory%CPU Idle
%s%s%s%s%s%s
" >> $TEMP_FILE +# +################################################################ +# +# Mail Performance Report & Clean up +# +#$MAIL -a $TEMP_FILE -s "Performance Report $DATE" +#-- $MAIL_TO < /dev/null +# +#rm -r $TEMP_FILE +# diff --git a/codes/shell/示例脚本/进阶脚本/系统快照报告.sh b/codes/shell/示例脚本/进阶脚本/系统快照报告.sh new file mode 100644 index 0000000..8614a1c --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/系统快照报告.sh @@ -0,0 +1,92 @@ +#!/bin/bash +# +# Snapshot_Stats - produces a report for system stats +# +############################################## +# +# Set Script Variables +# +DATE=`date +%m%d%y` +DISKS_TO_MONITOR="/dev/sda1" +MAIL=`which mutt` +MAIL_TO=tiandi +REPORT=/home/tiandi/Documents/Snapshot_Stats_$DATE.rpt +# +#################################################### +# +# Create Report File +# +exec 3>&1 # Save file descriptor +# +exec 1> $REPORT # direct output to rpt file +# +################################################### +# +echo +echo -e "\t\tDaily System Report" +echo +# +################################################### +# Date Stamp the Report +# +echo -e "Today is" `date +%m%d%y` +echo +# +################################################## +# +#1) Gather System Uptime Statistics +# +echo -e "System has been \c" +uptime | sed -n '/,/s/,/ /gp' | gawk '{if($4 == "days" || $4 == "day") {print $2,$3,$4,$5} else {print $2,$3}}' +# +################################################# +# +#2) Gather Disk Usage Statistics +# +echo +for DISK in $DISK_TO_MONITOR # loop to check disk space +do + echo -e "$DISK usage: \c" + df -h $DISK | sed -n '/% \//p' | gawk '{ print $5 }' +done +# +################################################################## +# +#3) Gather Memory Usage Statstics +# +echo +echo -e "Memory Usage: \c" +# +free | sed -n '2p' | gawk 'x = int(($3 / $2) * 100) {print x}' | sed 's/$/%/' +# +############################################################### +# +#4) Gather Number of Zombie Processes +# +echo +ZOMBIE_CHECK=`ps -al | gawk '{print $2,$4}' | grep Z` +# +if [ "$ZOMBIE_CHECK" = "" ] +then + echo "No Zombie Process on System at this Time" +else + echo "Current System Zombie Processes" + ps -al | gawk '{print $2,$4}' | grep Z +fi +echo +# +##################################################################### +# +# Restore File Descriptor & Mail Report +# +exec 1>&3 # Restore output to STDOUT +# +#$MAIL -a $REPORT -s "System Sstatistics Report for $DATE" +#-- $MAIL_TO < /dev/null +# +############################################################### +# +# Clean up +# +#rm -f $REPORT +# diff --git a/codes/shell/示例脚本/进阶脚本/输出颜色.sh b/codes/shell/示例脚本/进阶脚本/输出颜色.sh new file mode 100644 index 0000000..4d2d710 --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/输出颜色.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + +#输出特效格式控制 +#${C_RESET} 关闭所有属性 +#\033[1m 设置高亮度 +#\03[4m 下划线 +#\033[5m 闪烁 +#\033[7m 反显 +#\033[8m 消隐 +#\033[30m -- \033[37m 设置前景色 +#\033[40m -- \033[47m 设置背景色 + +#光标位置等的格式控制 +#\033[nA 光标上移n行 +#\03[nB 光标下移n行 +#\033[nC 光标右移n行 +#\033[nD 光标左移n行 +#\033[y;xH设置光标位置 +#\033[2J 清屏 +#\033[K 清除从光标到行尾的内容 +#\033[s 保存光标位置 +#\033[u 恢复光标位置 +#\033[?25l 隐藏光标 +#\33[?25h 显示光标 + +#整理: +# 编码 颜色/动作 +#  0 重新设置属性到缺省设置 +#  1 设置粗体 +#  2 设置一半亮度(模拟彩色显示器的颜色) +#  4 设置下划线(模拟彩色显示器的颜色) +#  5 设置闪烁 +#  7 设置反向图象 +#  22 设置一般密度 +#  24 关闭下划线 +#  25 关闭闪烁 +#  27 关闭反向图象 +#  30 设置黑色前景 +#  31 设置红色前景 +#  32 设置绿色前景 +#  33 设置棕色前景 +#  34 设置蓝色前景 +#  35 设置紫色前景 +#  36 设置青色前景 +#  37 设置白色前景 +#  38 在缺省的前景颜色上设置下划线 +#  39 在缺省的前景颜色上关闭下划线 +#  40 设置黑色背景 +#  41 设置红色背景 +#  42 设置绿色背景 +#  43 设置棕色背景 +#  44 设置蓝色背景 +#  45 设置紫色背景 +#  46 设置青色背景 +#  47 设置白色背景 +#  49 设置缺省黑色背景 +#特效可以叠加,需要使用“;”隔开,例如:闪烁+下划线+白底色+黑字为 \033[5;4;47;30m闪烁+下划线+白底色+黑字为${C_RESET} + +C_RESET="$(tput sgr0)" +C_BLACK="\033[1;30m" +C_RED="\033[1;31m" +C_GREEN="\033[1;32m" +C_YELLOW="\033[1;33m" +C_BLUE="\033[1;34m" +C_PURPLE="\033[1;35m" +C_CYAN="\033[1;36m" +C_WHITE="\033[1;37m" +# 示例: +echo -e "${C_BLACK} 黑色字 ${C_RESET}" +echo -e "${C_RED} 红色字 ${C_RESET}" +echo -e "${C_GREEN} 绿色字 ${C_RESET}" +echo -e "${C_YELLOW} 黄色字 ${C_RESET}" +echo -e "${C_BLUE} 蓝色字 ${C_RESET}" +echo -e "${C_PURPLE} 紫色字 ${C_RESET}" +echo -e "${C_CYAN} 天蓝字 ${C_RESET}" +echo -e "${C_WHITE} 白色字 ${C_RESET}" +# 字背景颜色范围:40-47 +echo -e "\033[40;37m 黑底白字 ${C_RESET}" +echo -e "\033[41;30m 红底黑字 ${C_RESET}" +echo -e "\033[42;34m 绿底蓝字 ${C_RESET}" +echo -e "\033[43;34m 黄底蓝字 ${C_RESET}" +echo -e "\033[44;30m 蓝底黑字 ${C_RESET}" +echo -e "\033[45;30m 紫底黑字 ${C_RESET}" +echo -e "\033[46;30m 天蓝底黑字 ${C_RESET}" +echo -e "\033[47;34m 白底蓝字 ${C_RESET}" + +#控制选项说明 +#${C_RESET} 关闭所有属性 +#\033[1m 设置高亮度 +#\033[4m 下划线 +echo -e "\033[4;31m 下划线红字 ${C_RESET}" +#闪烁 +echo -e "\033[5;34m 红字在闪烁 ${C_RESET}" +#反影 +echo -e "\033[8m 消隐 ${C_RESET} " + +#\033[30m-\033[37m 设置前景色 +#\033[40m-\033[47m 设置背景色 +#\033[nA光标上移n行 +#\033[nB光标下移n行 +echo -e "\033[4A 光标上移4行 ${C_RESET}" +#\033[nC光标右移n行 +#\033[nD光标左移n行 +#\033[y;xH设置光标位置 +#\033[2J清屏 +#\033[K清除从光标到行尾的内容 +echo -e "\033[K 清除光标到行尾的内容 ${C_RESET}" +#\033[s 保存光标位置 +#\033[u 恢复光标位置 +#\033[?25| 隐藏光标 +#\033[?25h 显示光标 +echo -e "\033[?25l 隐藏光标 ${C_RESET}" +echo -e "\033[?25h 显示光标 ${C_RESET}" diff --git a/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh b/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh new file mode 100644 index 0000000..a935ad1 --- /dev/null +++ b/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# +# Update_Problem - updates problem record in database +# +############################################################ +# +# Determine sql location & set variable +# +MYSQL=`which mysql`" Problem_Trek -u root" +# +############################################################## +# +# Obtain Record Id +# +if [ $# -eq 0 ] # Check if id number was passed +then # If not passed ask for it +# +# Check if any unfinished records exist. + RECORDS_EXIST=`$MYSQL -Bse 'SELECT id_number FROM problem_logger where fixed_date="0000-00-00" OR prob_solutions=""'` +# + if [ "$RECORDS_EXIST" != "" ] + then + echo + echo "The following record(s) need updating..." + $MYSQL < 90)) +then + (( var2 = $var1 ** 2)) + echo "The square of $var1 if $var2" +fi + diff --git a/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh b/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh new file mode 100644 index 0000000..a965817 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# another example of how not to use the for command + +for test in Newada "New Hampshire" +do + echo "Now going to $test" +done diff --git a/codes/shell/示例脚本/逻辑控制/使用双方括号.sh b/codes/shell/示例脚本/逻辑控制/使用双方括号.sh new file mode 100644 index 0000000..4d76eca --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/使用双方括号.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# using pattern matching + +if [[ $USER == r* ]] +then + echo "Hello $USER" +else + echo "Sorry, I do not know you" +fi diff --git a/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh b/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh new file mode 100644 index 0000000..5887c99 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +#changing the IFS value + +IFS.OLD=$IFS +IFS=$'\n' +for entry in `cat /etc/passwd` +do + echo "Values in $entry -" + IFS=: + for value in $entry + do + echo " $value" + done +done diff --git a/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh b/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh new file mode 100644 index 0000000..89f7a8f --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#testing compound comparisons + +if [ -d $HOME ] && [ -w $HOME/testing ] +then + echo "The file exists and you can write to it" +else + echo "I cannot write to it" +fi diff --git a/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh b/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh new file mode 100644 index 0000000..7ec74a4 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# redirecting the for output to a file +for file in /home/tiandi/* +do + if [ -d "$file" ] + then + echo "$file is a directory" + else + echo "$file is a file" + fi +done > output.txt + +# piping a loop to another command + +for state in "North Dakota" Connecticut +do + echo "$state is next place to go" +done | sort +echo "This completes our travels" diff --git a/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh b/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh new file mode 100644 index 0000000..cc45dad --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#iterate through all the files in a directory +for file in /home/tiandi/test/* +do + if [ -d "$file" ] + then + echo "$file is a directory" + elif [ -f "$file" ] + then + echo "$file is a file" + fi +done diff --git a/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh b/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh new file mode 100644 index 0000000..13114cc --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# testing string length + +#-n 判断长度是否非零 +#-z 判断长度是否为零 + +val1=testing +val2='' + +if [ -n "$val1" ] +then + echo "The string $val1 is not empty" +else + echo "The string $val1 is empty" +fi + +if [ -z "$val2" ] +then + echo "The string $val2 is empty" +else + echo "The string $val2 is not empty" +fi +if [ -z "$val3" ] +then + echo "The string $val3 is empty" +else + echo "The string $val3 is not empty" +fi + diff --git a/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh b/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh new file mode 100644 index 0000000..48d4118 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# multiple variables + +for (( a=1, b=10; a<=10; a++,b-- )) +do + echo "$a - $b" +done diff --git a/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh b/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh new file mode 100644 index 0000000..a4d439c --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh @@ -0,0 +1,8 @@ +#!/bin/bash +#testing multiple commands in the then section +testuser=tiandi +if grep $testuser /etc/passwd +then + echo The bash files from user $testuser are: + ls -a /home/$testuser/.b* +fi diff --git a/codes/shell/示例脚本/逻辑控制/字符串比较.sh b/codes/shell/示例脚本/逻辑控制/字符串比较.sh new file mode 100644 index 0000000..0382761 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/字符串比较.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#testing string equality + +testuser=tiandi + +if [ $USER = $testuser ] +then + echo "Welcome $testuser" +fi diff --git a/codes/shell/示例脚本/逻辑控制/嵌套if.sh b/codes/shell/示例脚本/逻辑控制/嵌套if.sh new file mode 100644 index 0000000..05a7907 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/嵌套if.sh @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh b/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh new file mode 100644 index 0000000..f0380e8 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# checking if a directory or a file exists + +if [ -e $HOME ] +then + echo "OK on the directory.now to check the file" + #checking if a file exists + if [ -e $HOME/testing ] + then + #the file exists,append data to it + echo "Appending date to existing file" + date >> $HOME/testing + else + #the file is not exists,create a new file + echo "Creating a new file" + date > $HOME/testing + fi +else + echo 'Sorry. you do not have a $HOME directory' +fi diff --git a/codes/shell/示例脚本/逻辑控制/检查目录.sh b/codes/shell/示例脚本/逻辑控制/检查目录.sh new file mode 100644 index 0000000..7a1fe79 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/检查目录.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# look before you leap + +if [ -d $HOME ] +then + echo "Your home directory exists" + cd $HOME + ls -a +else + echo "There is a problem with your HOME direcotry" +fi diff --git a/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh b/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh new file mode 100644 index 0000000..3cc6876 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#正确使用大于小于号 + +val1=baseball +val2=hocky + +if [ $val1 \> $val2 ] +then + echo "$val1 is greater than $val2" +else + echo "$val1 is less than $val2" +fi diff --git a/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh b/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh new file mode 100644 index 0000000..96123e4 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#test命令中,大小字母会被当成小于小写字符,而在sort中,小写字母会先出现,test使用标准的ASCII排序,sort使用本地化语言设置进行排序,对于英语,本地化设置制定了排序顺序中小写字母出现在大写字母之前 + +var1=Testing +var2=testing + +if [ $val1 \> $val2 ] +then + echo '$val1 is greater than $val2' +else + echo '$val1 is less than $val2' +fi diff --git a/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh b/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh new file mode 100644 index 0000000..e5e442d --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for test in I don\'t know if "this'll" work +do + echo "word:$test" +done diff --git a/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh b/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh new file mode 100644 index 0000000..7063175 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# basic for command +for test in Alabama Alaska Arizona +do + echo The next state is $test +done diff --git a/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh b/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh new file mode 100644 index 0000000..52034e7 --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# another example of how not to use the for command + +for test in I don't know if this'll work +do + echo "word:$test" +done diff --git a/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh b/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh new file mode 100644 index 0000000..7bc876b --- /dev/null +++ b/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# 大于小于号必须转义,否则shell会将它们当做重定向符号而把字符串值当做文件名处理 +# 大于小于号顺序和sort命令所采用的有所不同 +# mis-using string comparisons + +val1=baseball +val2=hockey + +if [ $val1 > $val2 ] +then + echo "$val1 is greater than $val2" +else + echo "$val1 is less than $val2" +fi diff --git a/docs/linux/README.md b/docs/linux/README.md index e366df3..385173a 100644 --- a/docs/linux/README.md +++ b/docs/linux/README.md @@ -90,4 +90,4 @@ ## :door: 传送门 -| [回首頁](https://github.com/dunwu/linux-tutorial) | +| [linux-tutorial](https://github.com/dunwu/linux-tutorial) | [blog](https://github.com/dunwu/blog) | diff --git a/docs/linux/soft/mongodb-install.md b/docs/linux/soft/mongodb-install.md new file mode 100644 index 0000000..760558a --- /dev/null +++ b/docs/linux/soft/mongodb-install.md @@ -0,0 +1,49 @@ +# Mongodb 安装 + + + +- [安装](#安装) +- [启动](#启动) +- [脚本](#脚本) + + + +## 安装 + +安装步骤如下: + +(1)下载并解压到本地 + +进入官网下载地址:https://www.mongodb.com/download-center#community ,选择合适的版本下载。 + +我选择的是最新稳定版本 3.6.3:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgz + +我个人喜欢存放在:`/opt/mongodb` + +``` +wget -O /opt/mongodb/mongodb-linux-x86_64-3.6.3.tgz https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgz +cd /opt/mongodb +tar zxvf mongodb-linux-x86_64-3.6.3.tgz +mv mongodb-linux-x86_64-3.6.3 mongodb-3.6.3 +mkdir -p /data/db +``` + +## 启动 + +**启动 mongodb 服务** + +``` +cd /opt/mongodb/mongodb-3.6.3/bin +./mongod --dbpath=/data/db +``` + +**启动 mongodb 客户端** + +``` +cd /opt/mongodb/mongodb-3.6.3/bin +./mongo +``` + +## 脚本 + +| [安装脚本](https://github.com/dunwu/linux/tree/master/codes/deploy/tool/mongodb) | diff --git a/docs/linux/soft/redis-install.md b/docs/linux/soft/redis-install.md new file mode 100644 index 0000000..54a7959 --- /dev/null +++ b/docs/linux/soft/redis-install.md @@ -0,0 +1,58 @@ +# Redis 安装 + + + +- [安装](#安装) +- [启动](#启动) +- [脚本](#脚本) + + + +## 安装 + +安装步骤如下: + +(1)下载并解压到本地 + +进入官网下载地址:https://redis.io/download ,选择合适的版本下载。 + +我选择的是最新稳定版本 4.0.8:http://download.redis.io/releases/redis-4.0.8.tar.gz + +我个人喜欢存放在:`/opt/redis` + +``` +wget -O /opt/redis/redis-4.0.8.tar.gz http://download.redis.io/releases/redis-4.0.8.tar.gz +cd /opt/redis +tar zxvf redis-4.0.8.tar.gz +``` + +(2)编译安装 + +执行以下命令: + +``` +cd /opt/redis/redis-4.0.8 +make +``` + +## 启动 + +**启动 redis 服务** + +``` +cd /opt/redis/redis-4.0.8/src +./redis-server +``` + +**启动 redis 客户端** + +``` +cd /opt/redis/redis-4.0.8/src +./redis-cli +``` + +## 脚本 + +以上两种安装方式,我都写了脚本去执行: + +| [安装脚本](https://github.com/dunwu/linux/tree/master/codes/deploy/tool/redis) |