源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

linux Shell学习笔记最后一节,温故与知新

  • 时间:2020-05-23 17:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:linux Shell学习笔记最后一节,温故与知新
[b]脚本编写技巧[/b] [b]脚本文件名命名规则 [/b]         文件名的字符包括         字母、数字、"."、"_"和"-", 注意:脚本的文件名命名不能以数字开头。 脚本文件名的命名,尽量使用英文单词、词组或短句的缩写。 脚本版本管理:         脚本作用.主版本号.修改次数.shell类型 [b]变量命名规范 [/b]         变量名可以由数字和字母组成         变量名采用全部英文字符小写的方式         变量名称尽量使用缩写,最好不要超过6个字符         ${变量名} 例:${port},${ipaddr}… [b]脚本代码注释规范 [/b]         脚本代码注释可以使用"#"和";(分号)"两种方式进行注释。#一般由于描述性的注释,旨在说明代码的作用或怎么使用。         而;通常用于示例性的注释,特别是在一些配置文件中常常会用到,因此我们沿用这两种方式来对我们的脚本进行注释。 [b]引用符号使用规范 [/b]         尽量少用单引号         对极个特殊字符进行屏蔽特殊含义时,使用\(反斜线)进行屏蔽         使用带引号进行屏蔽字符时,单引号内一般不适用其他引用符号,除非是打印特殊符号本身。         使用反引号进行执行一个shell命令时,反引号内一般加其他引用符号。 [b]脚本函数定义规范 [/b]         变量名可以由数字和字母组成         使用驼峰命名法(从第二个单词开始,每个单词首字母大写)         名字尽量不使用缩写,除非它是众所周知的         名字可以有两个或三个单词组成,但通常不应多于三个
[b]文本输出排版[/b]
第一天课后作业 1、grep "20081011" text.txt                            取出这一天的数据 2、grep "200804" text.txt                            取出这一个月的数据 3、grep "200806" text.txt | grep -v "-"                    取出一个月的上涨数据 4、grep "200807" text.txt | grep -v "-" | wc -l            列出有几天是上涨个数,带统计 5、grep "200808" text.txt | sort -k5 -n | tail -1            判断上涨,然后tail取最后一行 grep "200808" text.txt | sort -k5 -r |head -1            判断上涨,然后head取第一行 6、grep "200810" text.txt | awk '{if($4>0){print $1,$5}}'    取出一月中上涨数据时间和上涨幅 7、grep "200811" text.txt | awk '{if($4>5 && $4<20){print $0}}'        判断区间取出整行   脚本代码实例分析1     编写一个shell脚本,执行脚本后自动ping以下地址:         192.168.1.1,192.168.1.31     以上IP地址直接写在脚本之中。执行完成后, 应显示能够ping通的IP地址和不能够ping通的IP地址         #!/bin/bash     if ping 192.168.1.1 -c 1 then     echo "192.168.1.1 online" else     echo "192.168.1.1 offline" fi if ping 192.168.1.31 -c 1 then     echo "192.168.1.31 online" else     echo "192.168.1.31 offline" fi   脚本代码实例分析2 修改分析1,但是从iplist.txt中读取IP   #!/bin/bash for ip in `cat iplist.txt` do     if ping $ip -c 1     then         echo "${ip} online"     else         echo "${ip} offline"     fi done   脚本代码实例分析3 修改分析2,去除无用的信息   #!/bin/bash for ip in `cat iplist.txt` do     if ping $ip -c 1 >/dev/null 2>&1     then         echo "${ip} online"     else         echo "${ip} offline"     fi done     脚本代码实例分析4 修改分析3,生成记录   #!/bin/bash >hoststatus.txt for ip in `cat iplist.txt` do     if ping $ip -c 1 >/dev/null 2>&1     then         echo "${ip} online" |tee -a hoststatus.txt     else         echo "${ip} offline" |tee -a hoststatus.txt     fi done   脚本代码实例分析5 产生一个IP地址池,生成255个IP,并修改分析4,实现多线程ping   #!/bin/bash >iplist for ip in `seq 1 255` do     echo "192.168.1.${ip}" >>iplist done   #!/bin/bash >hoststatus.txt >temp fastping() {     if ping ${1} -c 1 >/dev/null 2>&1     then         echo "${ip} online" |tee -a temp     else         echo "${ip} offline" |tee -a temp     fi } for ip in `cat iplist.txt` do     fastping $ip & done wait sort -t. -k4 -n temp >hoststatus.txt rm temp   temp ./ping.sh            测试一个脚本执行多久
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部