每日一文件

https://github.com/aminglinux/shell100/blob/master/

要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为2017-12-20.log,并且把磁盘的使用情况写到这个文件中,提示:date、df

  1. [root@centos-04 tmp]# date
  2. 2018 12 26 星期三 19:29:13 CST
  3. [root@centos-04 tmp]# date +%Y
  4. 2018
  5. [root@centos-04 tmp]# date +%y
  6. 18
  7. [root@centos-04 tmp]# date +%d
  8. 26
  9. [root@centos-04 tmp]# date +%m
  10. 12
  11. [root@centos-04 tmp]# date +%H
  12. 19
  13. [root@centos-04 tmp]# date +%M
  14. 30
  15. [root@centos-04 tmp]# date +%S
  16. 52
  17. [root@centos-04 tmp]# date +%s
  18. 1545823854
  19. [root@centos-04 tmp]# date +%F
  20. 2018-12-26
  21. [root@centos-04 tmp]# date +%T
  22. 19:31:04
  23. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# date +%w
  2. 3
  3. [root@centos-04 tmp]# date +%W
  4. 52
  5. [root@centos-04 tmp]#

昨天日期

  1. [root@centos-04 tmp]# date -d "-1 day" +%F
  2. 2018-12-25
  3. [root@centos-04 tmp]#

上一小时

  1. [root@centos-04 tmp]# date -d "-1 hours" +%T
  2. 18:34:54
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 1.sh
  2. #!/bin/bash
  3. d=`date +%F`
  4. df -h > $d.log
  5. ~
  6.  
  7. [root@centos-04 tmp]# sh 1.sh
  8. [root@centos-04 tmp]# ls
  9. 1.sh ansible-ssh-192.168.242.130-22-root ansible-ssh-192.168.242.133-22-root lua_uwpzx3 tmp.SLBPtZ45L9
  10. 2018-12-26.log ansible-ssh-192.168.242.131-22-root elasticsearch.4Kw1U8qo nginx_proxy_tmp
  11. 456.log ansible-ssh-192.168.242.132-22-root hsperfdata_root proxy.log
  12. [root@centos-04 tmp]#
  13. [root@centos-04 tmp]# cat 2018-12-26.log
  14. 文件系统 容量 已用 可用 已用% 挂载点
  15. /dev/mapper/centos-root 18G 6.2G 12G 36% /
  16. devtmpfs 898M 0 898M 0% /dev
  17. tmpfs 910M 0 910M 0% /dev/shm
  18. tmpfs 910M 30M 881M 4% /run
  19. tmpfs 910M 0 910M 0% /sys/fs/cgroup
  20. /dev/sda1 497M 167M 331M 34% /boot
  21. tmpfs 182M 0 182M 0% /run/user/0
  22. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged
  23. shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm
  24. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged
  25. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged
  26. shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm
  27. shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
  28. [root@centos-04 tmp]#   

改进版

  1. d=`date +%F` #获取当前日期
  2. dir=/data/logs/disklog  #指定日志文件生成的目录
  3. if [ ! -d $dir ]  #判断如果没有目录创建
  4. then
  5. mkdir -p $dir
  6. fi
  7. df -h > $dir/$d.log  #将硬盘信息写到日志
  8. find $dir/ -mtime +365 |xargs rm  #删除一年之前的文件

统计IP访问量  

  1. awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r|less
  1. [root@centos-04 tmp]# vim 2.sh
  2. #!/bin/bash
  3. awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r
  4. [root@centos-04 tmp]# sh 2.sh

统计内存占用之和

  1. ps aux|sed '1d' (删除结果中第一行)
  1. [root@centos-04 tmp]# vim 3.sh
  2. #!/bin/bash
  3. sum=0
  4. for n in `ps aux |grep -v 'TIME COMMAND' |awk '{print $6}'`
  5. do
  6. sum=$[$sum+$n]
  7. done
  8. echo $sum
  9. [root@centos-04 tmp]# sh 3.sh
  10. 114892
  11. [root@centos-04 tmp]# free
  12. total used free shared buff/cache available
  13. Mem: 1863224 104260 1610364 9976 148600 1593352
  14. Swap: 0 0 0
  15. [root@centos-04 tmp]#

检测机器存活

  1. [root@centos-04 tmp]# ping -c2 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $NF}'
  2. 0
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim mail.py
  2. #!/usr/bin/env python
  3. #-*- coding: UTF-8 -*-
  4. import os,sys
  5. reload(sys)
  6. sys.setdefaultencoding('utf8')
  7. import getopt
  8. import smtplib
  9. from email.MIMEText import MIMEText
  10. from email.MIMEMultipart import MIMEMultipart
  11. from subprocess import *
  12.  
  13. def sendqqmail(username,password,mailfrom,mailto,subject,content):
  14. gserver = 'smtp.qq.com'
  15. gport = 25
  16.  
  17. try:
  18. # msg = MIMEText(unicode(content).encode('utf-8')) //如果发送的邮件有乱码,可以尝试把这行改成如下:
  19. msg = MIMEText(content,'plan','utf-8')
  20. msg['from'] = mailfrom
  21. msg['to'] = mailto
  22. msg['Reply-To'] = mailfrom
  23. msg['Subject'] = subject
  24.  
  25. smtp = smtplib.SMTP(gserver, gport)
  26. smtp.set_debuglevel(0)
  27. smtp.ehlo()
  28. smtp.login(username,password)
  29.  
  30. smtp.sendmail(mailfrom, mailto, msg.as_string())
  31. smtp.close()
  32. except Exception,err:
  33. print "Send mail failed. Error: %s" % err
  34.  
  35. def main():
  36. to=sys.argv[1]
  37. subject=sys.argv[2]
  38. content=sys.argv[3]
  39. ##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨)
  40. sendqqmail('1234567@qq.com','aaaaaaaaaa','1234567@qq.com',to,subject,content)
  41.  
  42. if __name__ == "__main__":
  43. main()
  44.  
  45. #####脚本使用说明######
  46. #1. 首先定义好脚本中的邮箱账号和密码
  47. #2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
  1. [root@centos-04 tmp]# vim 4.sh
  1. #!/bin/bash
  2. n=`ping -c5 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $F
  3. N}'`
  4. if [ -z "$n" ]
  5. then
  6. echo "脚本有问题。"
  7. exit
  8. else
  9. n1=`echo $n|sed 's/[0-9]//g'`
  10. if [ -n "$n" ]
  11. then
  12. echo "脚本$0有问题。"
  13. exit
  14. fi
  15. fi
  16. m=123@qq.com
  17. while :
  18. do
  19. if [ $n -ge 50 ]
  20. then
  21. python mail.py $m "机器宕机" "丢包率$n%"
  22. fi
  23. sleep 30
  24. done

批量改文件名

  1. [root@centos-04 tmp]# cp -r /123/ /123.bak
  1. [root@centos-04 tmp]# tar -tf 123.tar.gz
  1. [root@centos-04 tmp]# vim 5.sh
  2. #!/bin/bash
  3. find /123/ -type f -name "*.txt" > /tmp/txt.list
  4. for f in `cat /tmp/txt.list`
  5. do
  6. mv $f $f.bak
  7. done
  8. #find /123/ -type f -name *.txt |xargs -i mv {} {}.bak
  9. #find /123/ -type f -name *.txt -exec mv {} {}.bak \;
  10. for f in `cat /tmp/txt.list`
  11. do
  12. echo $f.bak
  13. done > /tmp/txt.bak.list
  14. tar -czvf 123.tar.gz `cat /tmp/txt.bak.list |xargs`
  15. for f in `cat /tmp/txt.list`
  16. do
  17. mv $f.bak $f
  18. done
  1. [root@centos-04 tmp]# rsync -av /123.bak/ /123/

检测80端口

  1. [root@centos-04 tmp]# netstat -lntp |grep 80
  2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6938/nginx: master
  3. tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 6938/nginx: master
  4. tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 6938/nginx: master
  5. [root@centos-04 tmp]# netstat -lntp |grep ':80 '
  6. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6938/nginx: master
  7. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# yum install -y nmap
  2. [root@centos-04 tmp]# nmap -p 80 127.0.0.1
  3.  
  4. Starting Nmap 6.40 ( http://nmap.org ) at 2019-01-04 03:29 CST
  5. Nmap scan report for localhost (127.0.0.1)
  6. Host is up (0.00011s latency).
  7. PORT STATE SERVICE
  8. 80/tcp open http
  9.  
  10. Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
  11. [root@centos-04 tmp]#

  1. [root@centos-04 tmp]# vim 6.sh 
  1. #!/bin/bash
  2. m=123@123.com
  3. while :
  4. do
  5. n=`netstat -lntp |grep ':80 '|wc -l`
  6. if [ $n -eq 0 ]
  7. then
  8. /usr/local/apache2/bin/apachectl -k restart 2>/tmp/apache.err
  9. python mail.py $m "80端口关闭" "已经重启httpd服务"
  10. pn=`pgrep -l httpd|wc -l`
  11. if [ $pn -eq 0 ]
  12. then
  13. python mail.py $m "httpd重启失败" "`head -1 /tmp/apache.err`"
  14. fi
  15. sleep 30
  16. done

也可以用nohup或screen这screen里执行6.sh

  1. [root@centos-04 tmp]# nohup sh 6.sh &
  1. [root@centos-04 tmp]# nmap -p 80 127.0.0.1 |grep '80/tcp' |awk '{print $2}' (用nmap写法)
  2. open
  3. [root@centos-04 tmp]#

备份数据库

  1. [root@centos-04 tmp]# vim 7.sh
  2. #!/bin/bash
  3. d1=`date +%w`
  4. d2=`date +%d`
  5. local_backdir=/bak/mysql
  6. remote_backdir=192.168.242.130::backup
  7.  
  8. exec 1> /tmp/mysqlbak.log 2>/tmp/mysqlbak.err
  9. echo "mysql backup begin at `date`"
  10. mysqldump -uroot -pxxx discuz > $local_backdir/discuz.sql.$d1
  11. rsync -az $local_backdir/discuz.sql.$d1 $remote_backdir/discuz.sql.$d2
  12. echo "mysql backup end at `date`"

检测502

1.502的情况:php配置有问题、php脚本耗资源。

  1. [root@centos-04 tmp]# vim 8.sh
  2. #!/bin/bash
  3. log=/data/log/access.log
  4. while :
  5. do
  6. 502_n=`tail -n 300 $log |grep -c ' 502 '`
  7. if [ -z "$502_n" ]
  8. then
  9. exit
  10. fi
  11.  
  12. if [ $502_n -gt 100 ]
  13. then
  14. /etc/init.d/php-fpm restart >/dev/null 2>/tmp/php-fpm.err
  15. fpm_p_n=`pgrep -l php-fpm|wc -l`
  16. if [ $fpm_p_n -eq 0 ]
  17. then
  18. python mail.py xxx@xx.com "php-fpm err" "head -1 /tmp/php-fpm.err"
  19. exit
  20. fi
  21. fi
  22. sleep 10
  23. done

删除字母和行

1.删除前五行中包含字母的行

  1. [root@centos-04 tmp]# head -n5 test.sql |sed '/[a-zA-Z]/d'

2.查看文件前5行

  1. [root@centos-04 tmp]# sed -n '1,5'p mail.py
  2. #!/usr/bin/env python
  3. #-*- coding: UTF-8 -*-
  4. import os,sys
  5. reload(sys)
  6. sys.setdefaultencoding('utf8')
  7. [root@centos-04 tmp]#

3.把6到10行中的全部字母删掉

  1. [root@centos-04 tmp]# vim 9.sh
  2. #!/bin/bash
  3. sed -n '1,5'p 1.txt|sed '/[a-zA-Z]/d'
  4. sed '1,5d' 1.txt |sed '1,5s/[a-zA-Z]//g'

找单词

  1. [root@centos-04 tmp]# for w in Bash also interprets a number of multi-character options.; do echo $w; done
  2. Bash
  3. also
  4. interprets
  5. a
  6. number
  7. of
  8. multi-character
  9. options.
  10. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 10.sh
  2. #!/bin/bash
  3. c="Bash also interprets a number of multi-character options."
  4. n=`echo $c|awk -F '[ +-.]' '{print NF}'`
  5. for ((i=1;i<$n;i++))
  6. do
  7. l=`echo $c|awk -F '[ +-.]' -v j=$i '{print $j}'|wc -L`
  8. if [ $l -lt 6 ]
  9. then
  10. echo $c|awk -F '[ +-.]' -v j=$i '{print $j}'
  11. fi
  12. done
  13. [root@centos-04 tmp]# sh 10.sh
  14. Bash
  15. also
  16. a
  17. of
  18. multi
  19. [root@centos-04 tmp]#

输入数字执行命令  

  1. [root@centos-04 tmp]# vim 11.sh
  2. #!/bin/bash
  3. echo "*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd"
  4. read -p "Please input a number:" n
  5. if [ -z "$n" ]
  6. then
  7. echo "请输入一纯数字,范围1-4。"
         exit
  8. fi
  9. n1=`echo $n|sed 's/[0-9]//g'`
  10. if [ -n "$n1" ]
  11. then
  12. echo "请输入一个纯数字,范围1-4。"
  13. exit
  14. fi
  15. case $n in
  16. 1)
  17. date
  18. ;;
  19. 2)
  20. ls
  21. ;;
  22. 3)
  23. who
  24. ;;
  25. 4)
  26. pwd
  27. ;;
  28. *)
  29. echo "请输入1-4的数字"
  30. ;;
  31. esac
  32. [root@centos-04 tmp]# sh 11.sh
  33. *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd
  34. Please input a number:1
  35. 2019 01 04 星期五 21:43:35 CST
  36. [root@centos-04 tmp]# sh 11.sh
  37. *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd
  38. Please input a number:2
  39. 10.sh 11.sh 1.sh 2018-12-26.log 2.sh 3.sh 456.log 4.sh 5.sh 6.sh 7.sh 8.sh 9.sh mail.py test.sql tmp.SzNhh17qiE
  40. [root@centos-04 tmp]# sh 11.sh
  41. *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd
  42. Please input a number:3
  43. root pts/1 2019-01-04 18:26 (192.168.242.1)
  44. [root@centos-04 tmp]# sh 11.sh
  45. *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd
  46. Please input a number:4
  47. /tmp
  48. [root@centos-04 tmp]# sh 11.sh
  49. *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd
  50. Please input a number:5
  51. 请输入1-4的数字
  52. [root@centos-04 tmp]# sh 11.sh
  53. *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd
  54. Please input a number:fsaf
  55. 请输入一个纯数字,范围1-4
  56. [root@centos-04 tmp]#

批量创建用户

  1. [root@centos-04 tmp]# seq -w 00 09
  2. 00
  3. 01
  4. 02
  5. 03
  6. 04
  7. 05
  8. 06
  9. 07
  10. 08
  11. 09
  12. [root@centos-04 tmp]# seq 0 9
  13. 0
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5
  19. 6
  20. 7
  21. 8
  22. 9
  23. [root@centos-04 tmp]#     

给用户添加密码

  1. [root@centos-04 tmp]# useradd user1
  2. [root@centos-04 tmp]# passwd user1
  3. 更改用户 user1 的密码
  4. 新的 密码:
  5. 无效的密码: 密码少于 7 个字符
  6. 重新输入新的 密码:
  7. passwd:所有的身份验证令牌已经成功更新。
  8. [root@centos-04 tmp]#

在脚本中自动给用户添加密码两种方式

  1. [root@centos-04 tmp]# echo -e "user1\nuser1\n" |passwd user1
  2. 更改用户 user1 的密码
  3. 新的 密码:无效的密码: 密码少于 7 个字符
  4. 重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。
  5. [root@centos-04 tmp]# echo "user1" |passwd --stdin user1
  6. 更改用户 user1 的密码
  7. passwd:所有的身份验证令牌已经成功更新。
  8. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 12.sh
  2. #!/bin/bash
  3. for i in `seq -w 00 09`
  4. do
  5. useradd user_$i
  6. p=`mkpasswd -l 10 -s 0`
  7. echo "user_$i $p" >> /tmp/pass.tmp
  8. echo $p |passwd --stdin user_$i
  9. done
  10. [root@centos-04 tmp]# sh 12.sh
  11. 更改用户 user_00 的密码
  12. passwd:所有的身份验证令牌已经成功更新。
  13. 更改用户 user_01 的密码
  14. passwd:所有的身份验证令牌已经成功更新。
  15. 更改用户 user_02 的密码
  16. passwd:所有的身份验证令牌已经成功更新。
  17. 更改用户 user_03 的密码
  18. passwd:所有的身份验证令牌已经成功更新。
  19. 更改用户 user_04 的密码
  20. passwd:所有的身份验证令牌已经成功更新。
  21. 更改用户 user_05 的密码
  22. passwd:所有的身份验证令牌已经成功更新。
  23. 更改用户 user_06 的密码
  24. passwd:所有的身份验证令牌已经成功更新。
  25. 更改用户 user_07 的密码
  26. passwd:所有的身份验证令牌已经成功更新。
  27. 更改用户 user_08 的密码
  28. passwd:所有的身份验证令牌已经成功更新。
  29. 更改用户 user_09 的密码
  30. passwd:所有的身份验证令牌已经成功更新。
  31. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# tail /etc/passwd
  2. user_00:x:1001:1001::/home/user_00:/bin/bash
  3. user_01:x:1002:1002::/home/user_01:/bin/bash
  4. user_02:x:1003:1003::/home/user_02:/bin/bash
  5. user_03:x:1004:1004::/home/user_03:/bin/bash
  6. user_04:x:1005:1005::/home/user_04:/bin/bash
  7. user_05:x:1006:1006::/home/user_05:/bin/bash
  8. user_06:x:1007:1007::/home/user_06:/bin/bash
  9. user_07:x:1008:1008::/home/user_07:/bin/bash
  10. user_08:x:1009:1009::/home/user_08:/bin/bash
  11. user_09:x:1010:1010::/home/user_09:/bin/bash
  12. [root@centos-04 tmp]# cat /tmp/pass.tmp
  13. user_00 8xiwgZSce6
  14. user_01 yaMp6cb2gA
  15. user_02 jx0QtlL2fw
  16. user_03 69bwuqgEDf
  17. user_04 p3fpvMMl9c
  18. user_05 fm5Bv4Xssx
  19. user_06 ivx69zVIpy
  20. user_07 l77CvxvuHy
  21. user_08 MZfmi6kx4f
  22. user_09 4bAkzeaKa6
  23. [root@centos-04 tmp]#

登录测试(复制user_00的密码粘贴密码登录成功)

  1. [root@centos-04 ~]# ssh user_00@192.168.242.130
  2. user_00@192.168.242.130's password:
  3. Last login: Sat Jan 5 00:00:28 2019
  4. [user_00@centos-04 ~]$   

删掉刚刚创建的用户

  1. [root@centos-04 tmp]# for i in `seq -w 00 09`;do userdel -r user_$i; done
  2. [root@centos-04 tmp]# tail /etc/passwd
  3. tcpdump:x:72:72::/:/sbin/nologin
  4. rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
  5. rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
  6. nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
  7. haproxy:x:188:188:haproxy:/var/lib/haproxy:/sbin/nologin
  8. dockerroot:x:994:991:Docker User:/var/lib/docker:/sbin/nologin
  9. epmd:x:993:990:Erlang Port Mapper Daemon:/tmp:/sbin/nologin
  10. rabbitmq:x:992:989:RabbitMQ messaging server:/var/lib/rabbitmq:/bin/bash
  11. user1:x:1000:1000::/home/user1:/bin/bash

监控httpd进程  

  1. [root@centos-04 tmp]# vim 13.sh
  2. #!/bin/bash
  3. check_service()
  4. {
  5. c=0
  6. for i in `seq 1 5`
  7. do
  8. /usr/local/apache2/bin/apachectl -k restart 2> /tmp/httpd.err
  9. if [ ! $? -eq 0 ]
  10. then
  11. c=$[$c+1]
  12. else
  13. break
  14. fi
  15. done
  16. if [ $c -eq 5 ]
  17. then
  18. python mail.py "123@qq.com " "apache进程数量大于500,重启失败。" "`head -1 /tmp/httpd.err`"
  19. exit
  20. fi
  21. }
  22.  
  23. while :
  24. do
  25. n=`ps -C httpd --no-heading|wc -l`
  26. if [ $n -ge 500 ]
  27. then
  28. check_service
  29. sleep 60
  30. n_new=`ps -C httpd --no-heading|wc -l`
  31. if [ $n_new -ge 500 ]
  32. then
  33. python mail.py "123@qq.com " "apache重启一分钟后进程数量仍然大于500" "请登录服务器排查问题"
  34. exit
  35. fi
  36. fi
  37.  
  38. sleep 10
  39. done

封ip  

  1. [root@centos-04 logs]# tail access.log
  2. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/replicationcontrollers?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  3. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/services?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  4. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/apps/v1beta1/statefulsets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  5. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/storage.k8s.io/v1/storageclasses?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/
    a452946/scheduler"
  6. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  7. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/policy/v1beta1/poddisruptionbudgets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/
    a452946/scheduler"
  8. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/extensions/v1beta1/replicasets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  9. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/nodes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  10. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumeclaims?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  11. 127.0.0.1 - - [19/Dec/2018:23:46:50 +0800] "GET /api/v1/pods?fieldSelector=status.phase%21%3DFailed%2Cstatus.phase%21%3DSucceeded&limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "
    kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  12. [root@centos-04 logs]#
  13. [root@centos-04 logs]# egrep '2018:23:46:[0-9]+' access.log |tail (这里使用egrep,因为正则中有+号)
  14. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/replicationcontrollers?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  15. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/services?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  16. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/apps/v1beta1/statefulsets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  17. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/storage.k8s.io/v1/storageclasses?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/
    a452946/scheduler"
  18. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  19. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/policy/v1beta1/poddisruptionbudgets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/
    a452946/scheduler"
  20. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/extensions/v1beta1/replicasets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  21. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/nodes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  22. 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumeclaims?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/
    scheduler"
  23. 127.0.0.1 - - [19/Dec/2018:23:46:50 +0800] "GET /api/v1/pods?fieldSelector=status.phase%21%3DFailed%2Cstatus.phase%21%3DSucceeded&limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-"
    "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"
  24. [root@centos-04 logs]#   

求出上一分钟时间

  1. [root@centos-04 logs]# echo `date -d "-1 min" +%Y:%H:%M`
  2. 2019:01:39
  3. [root@centos-04 logs]# date
  4. 2019 01 05 星期六 01:40:53 CST
  5. [root@centos-04 logs]#

查看iptables (pkts有多少个数据包,bytes有多少字节)

  1. [root@centos-04 tmp]# iptables -nvL
  2. Chain INPUT (policy ACCEPT 6947 packets, 5761K bytes)
  3. pkts bytes target prot opt in out source destination
  4.  
  5. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  6. pkts bytes target prot opt in out source destination
  7. 46 86418 DOCKER-ISOLATION all -- * * 0.0.0.0/0 0.0.0.0/0
  8. 46 86418 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0
  9. 44 86298 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
  10. 0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0
  11. 2 120 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0
  12.  
  13. Chain OUTPUT (policy ACCEPT 5072 packets, 1722K bytes)
  14. pkts bytes target prot opt in out source destination
  15.  
  16. Chain DOCKER (1 references)
  17. pkts bytes target prot opt in out source destination
  18. 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.2 tcp dpt:3306
  19. 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.3 tcp dpt:9000
  20. 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.4 tcp dpt:80
  21.  
  22. Chain DOCKER-ISOLATION (1 references)
  23. pkts bytes target prot opt in out source destination
  24. 46 86418 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
  25. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# iptables -I INPUT -p tcp --dport 80 -s 1.1.1.1 -j REJECT (封掉1.1.1.1)
  2. [root@centos-04 tmp]# iptables -nvL
  3. Chain INPUT (policy ACCEPT 55 packets, 3968 bytes)
  4. pkts bytes target prot opt in out source destination
  5. 0 0 REJECT tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable
  6.  
  7. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  8. pkts bytes target prot opt in out source destination
  9. 46 86418 DOCKER-ISOLATION all -- * * 0.0.0.0/0 0.0.0.0/0
  10. 46 86418 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0
  11. 44 86298 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
  12. 0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0
  13. 2 120 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0
  14.  
  15. Chain OUTPUT (policy ACCEPT 34 packets, 3144 bytes)
  16. pkts bytes target prot opt in out source destination
  17.  
  18. Chain DOCKER (1 references)
  19. pkts bytes target prot opt in out source destination
  20. 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.2 tcp dpt:3306
  21. 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.3 tcp dpt:9000
  22. 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.4 tcp dpt:80
  23.  
  24. Chain DOCKER-ISOLATION (1 references)
  25. pkts bytes target prot opt in out source destination
  26. 46 86418 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
  27. [root@centos-04 tmp]#
  1. #!/bin/bash
  2. block_ip
  3. {
  4. t1=`date -d "-1 min" +%Y:%H:%M`
  5. log/data/logs/access_log
  6. egrep "$t1:[0-9]+" $log > /tmp/tmp_last_min.log
  7. awk '{print $1}' /tmp/tmp_last_min.log |sort -n |uniq -c|sort -n |awk '$1>100 {print $2}' > /tmp/bad_ip.list
  8. n=`wc -l /tmp/bad_ip.list|awk '{print $1}'`
  9. if [ $n -ne 0 ]
  10. then
  11. for ip in `cat /tmp/bad_ip.list`
  12. do
  13. iptables -I INPUT -s $ip -j REJECT
  14. done
  15. fi
  16. }
  17.  
  18. unblock_ip()
  19. {
  20. iptables -nvL INPUT |sed '1d' |awk '$1<5 {print $8}' > /tmp/good_ip.list
  21. n=`wc -l /tmp/good_ip.list|awk '{print $1}'`
  22. if [ $n -ne 0 ]
  23. then
  24. for ip in `cat /tmp/good_ip.list`
  25. do
  26. iptables -D INPUT -s $ip -j REJECT
  27. done
  28. fi
  29. iptables -Z
  30. }
  31.  
  32. t=`date +%M`
  33. if [ $t == "00" ] || [ $t == "30" ]
  34. then
  35. unblock_ip
  36. block_ip
  37. else
  38. block_ip
  39. fi
  1. [root@centos-04 tmp]# vim 14.sh

将该脚本放到计划任务中每分钟执行一次

算数字

  1. [root@centos-04 tmp]# vim 15.sh
  2. #!/bin/bash
  3. x=10
  4. y=21
  5. for i in `seq 0 15`;
  6. do
  7. echo $x;
  8. x=$[$x+$y]
  9. z=$[2**$i] (求幂)
  10. y=$[$y+$z]
  11. done
  12. [root@centos-04 tmp]# sh 15.sh
  13. 10
  14. 31
  15. 53
  16. 77
  17. 105
  18. 141
  19. 193
  20. 277
  21. 425
  22. 701
  23. 1233
  24. 2277
  25. 4345
  26. 8461
  27. 16673
  28. 33077

查用户 

获取linux版本

  1. [root@centos-04 tmp]# awk -F 'release ' '{print $2}' /etc/redhat-release |cut -d '.' -f1
  2. 7
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 16.sh
  2. #!/bin/bash
  3. v=`awk -F 'release ' '{print $2}' /etc/redhat-release |cut -d '.' -f1`
  4. user()
  5. {
  6. if [ $1 -eq 0 ]
  7. then
  8. echo "系统没有自定义的用户"
  9. else
  10. echo "系统存在自定义用户。有$1个"
  11. fi
  12. }
  13.  
  14. case $v in
  15. 5|6)
  16. n=`awk -F ':' '$3>=500' /etc/passwd|wc -l`
  17. user $n
  18. ;;
  19. 7)
  20. n=`awk -F ':' '$3>=1000' /etc/passwd|wc -l`
  21. user $n
  22. ;;
  23. *)
  24. echo "脚本出错"
  25. ;;
  26. esac
  27. [root@centos-04 tmp]# sh 16.sh
  28. 系统存在自定义用户。有3
  29. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# awk -F ':' '$3 >= 1000' /etc/passwd
  2. nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
  3. user1:x:1000:1000::/home/user1:/bin/bash
  4. user_00:x:1001:1001::/home/user_00:/bin/bash
  5. [root@centos-04 tmp]#

检测磁盘

查看空间使用量和inode使用量

  1. [root@centos-04 tmp]# df
  2. [root@centos-04 tmp]# df -i
  1. [root@centos-04 tmp]# df |awk '{print $5}'
  2. 已用%
  3. 36%
  4. 0%
  5. 0%
  6. 2%
  7. 0%
  8. 34%
  9. 36%
  10. 0%
  11. 36%
  12. 0%
  13. 36%
  14. 0%
  15. 0%
  16. [root@centos-04 tmp]# df |awk '{print $5}'|sed 's/%//'
  17. 已用
  18. 36
  19. 0
  20. 0
  21. 2
  22. 0
  23. 34
  24. 36
  25. 0
  26. 36
  27. 0
  28. 36
  29. 0
  30. 0
  31. [root@centos-04 tmp]#

去除标头、解决有小数点的情况

  1. [root@centos-04 tmp]# df|sed '1d' |awk '{print $5}'|sed 's/%//'|cut -d '.' -f1
  2. 36
  3. 0
  4. 0
  5. 2
  6. 0
  7. 34
  8. 36
  9. 0
  10. 36
  11. 0
  12. 36
  13. 0
  14. 0
  15. [root@centos-04 tmp]#

最终获取大于85的命令

  1. [root@centos-04 tmp]# df|sed '1d' |awk -F ' +|%' '$5>85 {print $7}'
  2. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# df|sed '1d' |awk -F ' +|%' '$5<85 {print $7}'
  2. /
  3. /dev
  4. /dev/shm
  5. /run
  6. /sys/fs/cgroup
  7. /boot
  8. /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged
  9. /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm
  10. /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged
  11. /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
  12. /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged
  13. /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm
  14. /run/user/[root@centos-04 tmp]# vim 17.sh
  1. #!/bin/bash
  2. dir=/tmp/disk
  3. d=`date +%F`
  4. m=123@123.com
  5. [ -d $dir ] || mkdir $dir
  6. df >> $dir/$d.log
  7. df -i >> $dir/$d.log
  8.  
  9. df|sed '1d' |awk -F ' +|%' '$5>=85 {print $7}' > $dir/df.tmp
  10. df -i|sed '1d' |awk -F ' +|%' '$5>=85 {print $7}' > $dir/df_i.tmp
  11. n1=`wc -l $dir/df.tmp|awk '{print $1}'`
  12. n2=`wc -l $dir/df_i.tmp|awk '{print $1}'`
  13.  
  14. tag=0
  15. if [ $n1 -gt 0 ]
  16. then
  17. if [ $n2 -gt 0 ]
  18. then
  19. tag=11
  20. else
  21. tag=10
  22. fi
  23. else
  24. if [ $n2 -gt 0 ]
  25. then
  26. tag=01
  27. else
  28. tag=00
  29. fi
  30.  
  31. fi
  32.  
  33. case $tag in
  34. 11)
  35. python mail.py $m "磁盘空间和inode使用率高于85" "`cat $dir/df.tmp $dir/df_i.tmp|xargs`"
  36. ;;
  37. 10)
  38. python mail.py $m "磁盘空使用率高于85" "`cat $dir/df.tmp|xargs`"
  39. ;;
  40. 01)
  41. python mail.py $m "磁盘inode使用率高于85" "`cat $dir/df_i.tmp|xargs`"
  42. ;;
  43. *)
  44. ;;
  45. esac

检测新文件 

需要任务计划执行

  1. [root@centos-04 tmp]# vim 18.sh
  2. #!/bin/bash
  3. basedir=/tmp/
  4. t=`date +%Y%m%d%H%M`
  5.  
  6. find $basedir/ -type f -mmin -5 > /tmp/file.list
  7. n=`wc -l /tmp/file.list|awk '{print $1}'`
  8. if [ $n -gt 0 ]
  9. then
  10. mv /tmp/file.list /tmp/$t.list
  11. fi
  1. [root@centos-04 tmp]# sh 18.sh
  2. [root@centos-04 tmp]# ls
  3. 10.sh 12.sh 14.sh 16.sh 18.sh 2018-12-26.log 2.sh 456.log 5.sh 7.sh 9.sh mail.py test.sql
  4. 11.sh 13.sh 15.sh 17.sh 1.sh 201901050745.list 3.sh 4.sh 6.sh 8.sh disk pass.tmp tmp.SzNhh17qiE

最常用的命令  

history命令调用的就是~/.bash_history文件的内容,统计最常用的10条命令

  1. [root@centos-04 tmp]# cat ~/.bash_history |sort |uniq -c |sort -nr |head
  2. 238 ls
  3. 34 docker ps
  4. 25 cd ../
  5. 24 docker ps -a
  6. 18 docker exec -it c58be577ba9f bash
  7. 17 docker images
  8. 13 docker exec -it c00e5859f876 bash
  9. 10 ll
  10. 10 docker restart c00e5859f876
  11. 9 ps aux|grep nginx
  12. [root@centos-04 tmp]#

统计文件大小  

计划任务执行

时间计算

  1. [root@centos-04 tmp]# date -d "-16 hour" +%H
  2. 08
  3. [root@centos-04 tmp]# date +%H
  4. 00
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 20.sh
  2. #!/bin/bash
  3. dir=/tmp/log_stat
  4. t=`date +%d%H`
  5. t1=`date +%H`
  6. logdir=/data/log
  7.  
  8. [ -f $dir/$t.log ] && rm -f $dir/$t.log
  9. [ -d $dir ] || mkdir $dir
  10.  
  11. if [ $t == "00" -o $t == "12" ]
  12. then
  13. for f in `find $logdir/ -type f`
  14. do
  15. > $f
  16. done
  17. else
  18. for f in `find $logdir/ -type f`
  19. do
  20. du -sh $f >> $dir/$t.log
  21. done
  22. fi

计算数字个数

-e的用法

  1. [root@centos-04 tmp]# for i in `echo -e "123\nabc 123"`; do echo $i; done
  2. 123
  3. abc
  4. 123
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# wc -l 20
  2. 2018-12-26.log 201901050745.list 20.sh
  3. [root@centos-04 tmp]# wc -l 2018-12-26.log
  4. 14 2018-12-26.log
  5. [root@centos-04 tmp]# for i in `seq 1 14`; do sed -n "$i"p 2018-12-26.log; done
  6. 文件系统 容量 已用 可用 已用% 挂载点
  7. /dev/mapper/centos-root 18G 6.2G 12G 36% /
  8. devtmpfs 898M 0 898M 0% /dev
  9. tmpfs 910M 0 910M 0% /dev/shm
  10. tmpfs 910M 30M 881M 4% /run
  11. tmpfs 910M 0 910M 0% /sys/fs/cgroup
  12. /dev/sda1 497M 167M 331M 34% /boot
  13. tmpfs 182M 0 182M 0% /run/user/0
  14. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged
  15. shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm
  16. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged
  17. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged
  18. shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm
  19. shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
  20. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# while read line; do echo $line; done < 2018-12-26.log
  2. 文件系统 容量 已用 可用 已用% 挂载点
  3. /dev/mapper/centos-root 18G 6.2G 12G 36% /
  4. devtmpfs 898M 0 898M 0% /dev
  5. tmpfs 910M 0 910M 0% /dev/shm
  6. tmpfs 910M 30M 881M 4% /run
  7. tmpfs 910M 0 910M 0% /sys/fs/cgroup
  8. /dev/sda1 497M 167M 331M 34% /boot
  9. tmpfs 182M 0 182M 0% /run/user/0
  10. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged
  11. shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm
  12. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged
  13. overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged
  14. shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm
  15. shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
  16. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 21.sh
  2. #!/bin/bash
  3. sum=0
  4. while read line
  5. do
  6. line_n=`echo $line|sed 's/[^0-9]//g'|wc -L`
  7. echo $line_n
  8. sum=$[$sum+$line_n]
  9. done < $1
  10. echo "sum:$sum"
  11. [root@centos-04 tmp]# sh 21.sh 2018-12-26.log
  12. 0
  13. 8
  14. 8
  15. 8
  16. 9
  17. 8
  18. 12
  19. 9
  20. 53
  21. 52
  22. 47
  23. 47
  24. 48
  25. 50
  26. sum:359
  27. [root@centos-04 tmp]# head -1 2018-12-26.log
  28. 文件系统 容量 已用 可用 已用% 挂载点
  29. [root@centos-04 tmp]# tail -1 2018-12-26.log
  30. shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
  31. [root@centos-04 tmp]#

对比文件差异

将文件做MD5

  1. [root@centos-04 tmp]# md5sum 2018-12-26.log
  2. b11c7a1a9da1ab2b474ce5dea5e02fe1 2018-12-26.log
  3. [root@centos-04 tmp]#

EOF嵌入文档

第一步:传列表文件,第二步我们又写了一个脚本,第三步将脚本传到1.1.1.1机器

  1. [root@centos-04 tmp]# cat > 2.txt << EOF
  2. > 1
  3. > 2
  4. > 3
  5. > EOF
  6. [root@centos-04 tmp]# cat 2.txt
  7. 1
  8. 2
  9. 3
  10. [root@centos-04 tmp]#

  

  1. #!/bin/bash
  2. dir=/data/web
  3. [ -f /tmp/md5.list ] && rm -f /tmp/md5.list
  4. find $dir/ -type f > /tmp/file.list
  5. while read line
  6. do
  7. md5sum $line >> /tmp/md5.list
  8. done < /tmp/file.list
  9.  
  10. scp /tmp/md5.list B:/tmp/
  11. [ -f /tmp/check_md5.sh ] && rm -f /tmp/check_md5.sh
  12.  
  13. cat >/tmp/check_md5.sh << EOF
  14. #!/bin/bash
  15. dir=/data/web
  16. n=\`wc -l /tmp/md5.list|awk '{print \$1}'\`
  17. for i in \`seq 1 \$n\`
  18. do
  19. file_name=\`sed -n "\$i"p /tmp/md5.list |awk '{print \$1}'\`
  20. md5=\`sed -n "\$i"p /tmp/md5.list|awk '{print \$2}'\`
  21. if [ -f \$file_name ]
  22. then
  23. md5_b=\`md5sum \$file_name\`
  24. if [\$md5_b != \$md5 ]
  25. then
  26. echo "\$file_name changed."
  27. fi
  28. else
  29. echo "\$file_name lose."
  30. fi
  31. done
  32. EOF
  33. scp /tmp/check_md5.sh B:/tmp/
  34. ssh B "/bin/bash /tmp/check_md5.sh"

检测网卡流量

sar命令,我们这里看第五列和第六列(8bit=1byte,100Mbit=12.5MByte/s)

  1. [sun.yujun@kddi-zol-php-test-web1 routes]$ sar -n DEV 1 5
  2. Linux 2.6.32-696.18.7.el6.x86_64 (kddi-zol-php-test-web1.zoldc.com.cn) 01/14/2019 _x86_64_ (12 CPU)
  3.  
  4. 07:19:09 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
  5. 07:19:10 PM lo 5.00 5.00 0.50 0.50 0.00 0.00 0.00
  6. 07:19:10 PM eth0 13.00 6.00 0.77 0.42 0.00 0.00 0.00
  7.  
  8. 07:19:10 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
  9. 07:19:11 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
  10. 07:19:11 PM eth0 12.00 6.00 0.71 0.73 0.00 0.00 0.00
  11.  
  12. 07:19:11 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
  13. 07:19:12 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
  14. 07:19:12 PM eth0 12.00 5.00 0.72 0.68 0.00 0.00 0.00
  15.  
  16. 07:19:12 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
  17. 07:19:13 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
  18. 07:19:13 PM eth0 16.83 5.94 1.00 0.74 0.00 0.00 0.00
  19.  
  20. 07:19:13 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
  21. 07:19:14 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
  22. 07:19:14 PM eth0 8.00 3.00 0.48 0.56 0.00 0.00 0.00
  23.  
  24. Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
  25. Average: lo 1.00 1.00 0.10 0.10 0.00 0.00 0.00
  26. Average: eth0 12.38 5.19 0.74 0.62 0.00 0.00 0.00
  27. [sun.yujun@kddi-zol-php-test-web1 routes]$ ^C
  28. [sun.yujun@kddi-zol-php-test-web1 routes]$   

将脚本放到crontab里面,一分钟执行一次

  1. [root@centos-04 tmp]# vim 23.sh
  2. #!/bin/bash
  3. logdir=/tmp/sar_log
  4. file=$logdir/`date +%d$H`.log
  5. t=`date +"%F %H:%M"`
  6. [ -d $logdir ] || mkdir -p $logdir
  7. LANG=en
  8. sar -n DEV 1 5 |grep eth0 |grep "Average" > /tmp/sar.tmp
  9. exec >>$file
  10. echo "$t"
  11. awk '{print "input:",$5*8000"bps""\n""output:",$6*8000"bps"}' /tmp/sar.tmp
  12. echo "###################"
  13. [root@centos-04 tmp]# sh 23.sh
  1. [root@centos-04 tmp]# ls /tmp/sar_log/
  2. 15.log
  3. [root@centos-04 tmp]# cat /tmp/sar_log/15.log
  4. 2019-01-15 03:48
  5. input: 160bps
  6. output: 80bps
  7. ###################
  8. [root@centos-04 tmp]#

批量杀进程

  1. [root@centos-04 tmp]# vim 24.sh
  2. #!/bin/bash
  3. for pid in `ps aux|grep clearnen.sh|awk '{print $2}'`;
  4. do
  5. echo $pid;
  6. kill -9 $pid;
  7. done

判断web服务

  1. [root@centos-04 tmp]# netstat -lntp|grep ':80 '|awk -F '/' '{print $NF}'
  2. nginx: master
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# lsof -i :80 |grep 'LISTEN'
  2. nginx 6958 root 11u IPv4 38456 0t0 TCP *:http (LISTEN)
  3. nginx 6959 nobody 11u IPv4 38456 0t0 TCP *:http (LISTEN)
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 25.sh
  2. #!/bin/bash
  3. n=`netstat -lntp |grep ':80 '|wc -l`
  4. if [ $n -eq 0 ]
  5. then
  6. echo "It not listen port 80"
  7. else
  8. ser=`netstat -lntp |grep ':80 '|awk -F '/' '{print $NF}'|sed 's/ //g'`
  9. echo "It is listenning port 80,and the service is $ser."
  10. fi
  11. [root@centos-04 tmp]# sh 25.sh
  12. It is listenning port 80,and the service is nginx:master.
  13. [root@centos-04 tmp]#

监控mysql服务

  1. [root@centos-04 tmp]# mysql -uroot -p123456 -h172.17.0.2 -e "show processlist"
  2. +----+------+------------------+------+---------+------+-------+------------------+
  3. | Id | User | Host | db | Command | Time | State | Info |
  4. +----+------+------------------+------+---------+------+-------+------------------+
  5. | 3 | root | 172.17.0.1:50676 | NULL | Query | 0 | init | show processlist |
  6. +----+------+------------------+------+---------+------+-------+------------------+
  7. [root@centos-04 tmp]#     

判断是否是从数据库

  1. [root@centos-04 tmp]# mysql -uroot -p123456 -h172.17.0.2 -e "show slave status\G"
  2. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 26.sh
  2. #!/bin/bash
  3. mysql = "/usr/local/mysql/bin/mysql -uroot -p123456"
  4. if ! $mysql -e "show processlist" >/dev/null 2>/dev/null
  5. then
  6. echo "MySql service is down."
  7. exit
  8. else
  9. $mysql -e "show slave status\G" 2>/dev/null >/tmp/slave.stat
  10. n=`wc -l /tmp/slave.stat|awk '{print $1}'`
  11. if [ $n -eq 0 ]
  12. then
  13. echo "This is master."
  14. else
  15. echo "This is slave."
  16. egrep 'Slave_10_Runing:|Slave_SQL_Running:'/tmp/slave/.stat|awk -F ': ' '{print
  17. $2}' > /tmp/SQL.tmp
  18. if grep -qw "No" /tmp/SQL.tmp
  19. then
  20. echo "The slave is down."
  21. fi
  22. fi
  23. fi

增删用户

  1. [root@centos-04 tmp]# vim 27.sh
  2. #!/bin/bash
  3. if [ $# -eq 0 ] || [ $# -gt 2 ]
  4. then
  5. echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help"
  6. exit
  7. fi
  8.  
  9. ex_user()
  10. {
  11. if ! id $1 2>/dev/null >/dev/null
  12. then
  13. useradd $1 && echo "$1 add successful."
  14. else
  15. echo $1 exist.
  16. fi
  17. }
  18.  
  19. notex_user()
  20. {
  21. if id $1 2>/dev/null >/dev/null
  22. then
  23. userdel $1 && echo "$1 delete successful."
  24. else
  25. echo $1 not exist.
  26. fi
  27. }
  28.  
  29. case $1 in
  30. --add)
  31. if [ $# -eq 1 ]
  32. then
  33. echo "Wrong, use bash $0 --add user or bash $0 --add user1,user2,user3..."
  34. exit
  35. else
  36. n=`echo $2| awk -F ',' '{print NF}'`
  37. if [ $n -gt 1 ]
  38. then
  39. for i in `seq 1 $n`
  40. do
  41. username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`
  42. ex_user $username
  43. done
  44. else
  45. ex_user $2
  46. fi
  47. fi
  48. ;;
  49. --del)
  50. if [ $# -eq 1 ]
  51. then
  52. echo "Wrong, use bash $0 --del user or bash $0 --del user1,user2,user3..."
  53. exit
  54. else
  55. n=`echo $2| awk -F ',' '{print NF}'`
  56. if [ $n -gt 1 ]
  57. then
  58. for i in `seq 1 $n`
  59. do
  60. username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`
  61. notex_user $username
  62. done
  63. else
  64. notex_user $2
  65. fi
  66. fi
  67. ;;
  68. --help)
  69. if [ $# -ne 1 ]
  70. then
  71. echo "Wrong, use bash $0 --help"
  72. exit
  73. else
  74.  
  75. echo "Use bash $0 --add username or bash $0 --add user1,user2,user3... add user."
  76. echo " bash $0 --del username -r bash $0 --del user1,user2,user3... delete user."
  77. echo " bash $0 --help print this info."
  78. fi
  79. ;;
  80. *)
  81. echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help"
  82. ;;
  83. esac

计算和

  1. #!/bin/bash
  2. sum=0
  3. for i in `seq 1 100`
  4. do
  5. j=$[$i%3]
  6. if [ $j -eq 0 ]
  7. then
  8. sum=$[$sum+$i]
  9. fi
  10. done
  11. echo $sum

加减乘除

  1. [root@centos-04 tmp]# vim 29.sh
  2. #!/bin/bash
  3. is_nu()
  4. {
  5. n=`echo $1 |sed 's/[0-9]//g'`
  6. if [ -n "$n" ]
  7. then
  8. echo "给出的参数必须是正整数"
  9. exit
  10. fi
  11. }
  12. if [ $# -ne 2 ]
  13. then
  14. echo "必须要输入两个参数"
  15. exit
  16. else
  17. is_nu $1
  18. is_nu $2
  19. fi
  20.  
  21. big()
  22. {
  23. if [ $1 -gt $2 ]
  24. then
  25. echo $1
  26. else
  27. echo $2
  28. fi
  29. }
  30.  
  31. small()
  32. {
  33. if [ $1 -lt $2 ]
  34. then
  35. echo $1
  36. else
  37. echo $2
  38. fi
  39. }
  40.  
  41. add()
  42. {
  43. sum=$[$1+$2]
  44. echo "$1+$2=$sum"
  45. }
  46.  
  47. jian()
  48. {
  49. b=`big $1 $2`
  50. s=`small $1 $2`
  51. cha=$[$b-$s]
  52. echo "$b-$s=$cha"
  53. }
  54.  
  55. cheng()
  56. {
  57. ji=$[$1*$2]
  58. echo "$1x$2=$ji"
  59. }
  60. chu()
  61. {
  62. b=`big $1 $2`
  63. s=`small $1 $2`
  64. v=`echo "scale=2;$b/$s"|bc`
  65. echo "$b/$s=$v"
  66. }
  67.  
  68. add $1 $2
  69. jian $1 $2
  70. cheng $1 $2
  71. chu $1 $2

输入数字

  1. [root@centos-04 tmp]# vim 30.sh
  2. #!/bin/bash
  3. while :
  4. do
  5. read -p "请输入一个数字:" n
  6. if [ -z "$n" ]
  7. then
  8. echo "请输入一个纯数字。"
  9. continue
  10. fi
  11. if echo $n |grep -qi 'end'
  12. then
  13. exit
  14. fi
  15.  
  16. n1=`echo $n|sed 's/[0-9]//g'`
  17. if [ -n "$n1" ]
  18. then
  19. echo "请输入一个纯数字"
  20. continue
  21. else
  22. echo "你输入的数字是:$n"
  23. continue
  24. fi
  25. done

获取网卡ip

  1. [root@centos-04 tmp]# ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}'
  2. lo
  3. ens33
  4. docker0
  5. vethd17e886@if4
  6. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# ip add show dev ens33 |grep ' inet '|awk '{print $2}'
  2. 192.168.242.130/24
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# ip add show dev ens33 |grep ' inet '|awk '{print $2}'|awk -F '/' '{print $1}'
  2. 192.168.242.130
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 31.sh
  2. #!/bin/bash
  3. ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/eth.list
  4. while :
  5. do
  6. eths=`cat /tmp/eth.list |xargs`
  7. read -p "Please input a if name(The eths is `echo -e "\033[31m$eths\033[0m"`): " eth
  8. if [ -z "$eth" ]
  9. then
  10. echo "Please input a if name."
  11. continue
  12. fi
  13. if ! grep -qw "$eth" /tmp/eth.list
  14. then
  15. echo "The if name is error."
  16. continue
  17. else
  18. break
  19. fi
  20. done
  21.  
  22. if_ip()
  23. {
  24. ip add show dev $1 |grep ' inet ' |awk '{print $2}'|awk -F '/' '{print $1}' >/tmp/$1.txt
  25. n=`wc -l /tmp/$1.txt|awk '{print $1}'`
  26. if [ $n -eq 0 ]
  27. then
  28. echo "There is no ip address on the eth."
  29. else
  30. echo "The ip addreess is:"
  31. for ip in `cat /tmp/$1.txt`
  32. do
  33. echo -e "\033[33m$ip\033[0m"
  34. done
  35. fi
  36. }
  37.  
  38. if_ip $eth

列出目录内容

$@指的是1.sh 后面的一堆参数1 2 3 4 a b,$#表示所有参数的个数

  1. [root@centos-04 tmp]# sh 1.sh 1 2 3 4 a b
  1. [root@centos-04 tmp]# vim 32.sh
  2. #!/bin/bash
  3. if [ $# -eq 0 ]
  4. then
  5. ls -l .
  6. else
  7. for d in $@
  8. do
  9. echo "There are these directorys in $d:"
  10. find $d -type d
  11. done
  12. fi
  13. [root@centos-04 tmp]# sh 32.sh /tmp/ /usr/local/
  14. There are these directorys in /tmp/:
  15. /tmp/
  16. /tmp/.XIM-unix
  17. /tmp/.font-unix
  18. /tmp/.X11-unix
  19. /tmp/.ICE-unix
  20. /tmp/.Test-unix
  21. There are these directorys in /usr/local/:
  22. /usr/local/
  23. /usr/local/bin
  24. /usr/local/etc
  25. /usr/local/games
  1. [root@centos-04 tmp]# sh 32.sh
  2. 总用量 32
  3. -rw-r--r-- 1 root root 1771 1 19 00:06 27.sh
  4. -rw-r--r-- 1 root root 122 1 19 00:28 28.sh
  5. -rw-r--r-- 1 root root 742 1 19 00:58 29.sh
  6. -rw-r--r-- 1 root root 343 1 19 01:06 30.sh
  7. -rw-r--r-- 1 root root 765 1 19 02:39 31.sh
  8. -rw-r--r-- 1 root root 134 1 28 22:34 32.sh
  9. -rw-r--r-- 1 root root 33 1 19 02:32 eth.list
  10. -rw-r--r-- 1 root root 10 1 19 02:32 lo.txt
  11. [root@centos-04 tmp]#
  1. (优化版)
    #!/bin/bash
  2. if [ $# -eq 0 ]
  3. then
  4. echo "当前目录下的文件是:"
  5. ls -l .
  6. else
  7. for d in $@
  8. do
  9. if [ -d $d ]
  10. then
  11. echo "There are these directorys in $d:"
  12. find $d -type d
  13. else
  14. echo "并没有该目录:$d"
  15. fi
  16. done
  17. fi

下载文件  

  1. [root@centos-04 tmp]# vim 33.sh
  2. #!/bin/bash
  3. if [ $# -ne 2 ]
  4. then
  5. echo "你必须要输入两个参数,第一个参数是网址,第二个参数是目录。"
  6. exit 1
  7. fi
  8.  
  9. if [ ! -d $2 ]
  10. then
  11. while :
  12. do
  13. echo "你输入的第二个参数并不是一个存在的目录,是否要创建该目录呢?(y|n):"c
  14.  
  15. case $c in
  16. y|Y)
  17. mkdir -p $2
  18. ;;
  19. n|N)
  20. exit 51
  21. ;;
  22. *)
  23. echo "请输入y或n"
  24. continue
  25. ;;
  26. esac
  27. done
  28. else
  29. cd $2
  30. wget $1
  31. if [ $? -eq 0 ]
  32. then
  33. exit 0
  34. else
  35. echo "下载失败"
  36. exit 52
  37. fi
  38. fi

猜数字

1.返回随机数

  1. [root@centos-04 tmp]# echo $RANDOM
  2. 15845
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# echo $[$RANDOM%101] (取0-100的数)
  2. 77
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 34.sh
  2. #!/bin/bash
  3. n=$[$RANDOM%101]
  4. while :
  5. do
  6. read -p "请输入一个0-100的数字:" n1

      if [ -z "$n1" ]
        then
        echo "必须输入一个数字"
        continue
      fi

  1. n2=`echo $n1 |sed 's/[0-9]//g'`
  2. if [ -n "$n2" ]
  3. then
  4. echo "你输入的数字并不是正整数。"
  5. continue
  6. else
  7. if [ $n -gt $n1 ]
  8. then
  9. echo "你输入的数字小了"
  10. continue
  11. elif [ $n -lt $n1 ]
  12. then
  13. echo "你输入的数字大了"
  14. continue
  15. else
  16. echo "恭喜你"
  17. break
  18. fi
  19. fi
  20. done
  21. [root@centos-04 tmp]# sh 34.sh
  22. 请输入一个0-100的数字:10
  23. 你输入的数字小了
  24. 请输入一个0-100的数字:20
  25. 你输入的数字小了
  26. 请输入一个0-100的数字:50
  27. 你输入的数字小了
  28. 请输入一个0-100的数字:90
  29. 你输入的数字大了
  30. 请输入一个0-100的数字:60
  31. 你输入的数字小了
  32. 请输入一个0-100的数字:70
  33. 你输入的数字小了
  34. 请输入一个0-100的数字:80
  35. 你输入的数字小了
  36. 请输入一个0-100的数字:85
  37. 你输入的数字大了
  38. 请输入一个0-100的数字:83
  39. 你输入的数字小了
  40. 请输入一个0-100的数字:84
  41. 恭喜你
  42. [root@centos-04 tmp]#

根据名字得数字  

  1. [root@centos-04 tmp]# vim 35.sh
  2. #!/bin/bash
  3. f=/tmp/user_number.txt
  4. while :
  5. do
  6. read -p "Please input a username:" u
  7. u1=`echo $u|sed 's/[a-zA-Z0-9]//g'`
  8. if [ -n "$u1" ]
  9. then
  10. echo "你输入的用户名不符合规范,正确的用户名应该是大小写字母和数字的组合"
  11. continue
  12. else
  13. if [ -f $f ]
  14. then
  15. u_n=`awk -v uu=$u '$1==uu {print $2}' $f`
  16. if [ -n "$u_n" ]
  17. then
  18. echo "用户$u对应的数字是:$u_n"
  19. else
  20. n=$[$RANDOM%100]
  21. echo "用户$u对应的数字是:$n"
  22. echo $u $n >> $f
  23. fi
  24. else
  25. n=$[$RANDOM%100]
  26. echo "用户$u对应的数字是:$n"
  27. echo $u $n >> $f
  28. fi
  29. fi
  30. done
  31. [root@centos-04 tmp]# sh 35.sh
  32. Please input a username:user1
  33. 用户user1对应的数字是:66
  34. Please input a username:user2
  35. 用户user2对应的数字是:45
  36. Please input a username:

根据名字得数字优化

  1. [root@centos-04 tmp]# vim 35.sh
  2. #!/bin/bash
  3. f=/tmp/user_number.txt
  4. jude_n()
  5. {
  6. #!/bin/bash
  7. f=/tmp/user_number.txt
  8. j_n()
  9. {
  10. while :
  11. do
  12. n=$[RANDOM%100]
  13. if awk '{print $2}' $f|grep -qw $n
  14. then
  15. continue
  16. else
  17. break
  18. n=$[$RANDOM%100]
  19. n=$[$RANDOM%100]
  20. if awk '{print $2} $f|grep -qw $n'
  21. fi
  22. done
  23. }
  24.  
  25. while :
  26. do
  27. read -p "Please input a username:" u
  28. if [ -z "$u" ]
  29. then
  30. echo "请输入用户名"
  31. continue
  32. fi
  33.  
  34. if [ $u == "q" ] || [ $u=="Q" ]
  35. then
  36. exit
  37. fi
  38.  
  39. u1=`echo $u|sed 's/[a-zA-Z0-9]//g'`
  40. if [ -n "$u1" ]
  41. then
  42. echo "你输入的用户名不符合规范,正确的用户名应该是大小写字母和数字的组合"
  43. continue
  44. else
  45. if [ -f $f ]
  46. then
  47. u_n=`awk -v uu=$u '$1==uu {print $2}' $f`
  48. if [ -n "$u_n" ]
  49. then
  50. echo "用户$u对应的数字是:$u_n"
  51. else
  52. j_n
  53. echo "用户$u对应的数字是:$n"
  54. echo $u $n >> $f
  55. fi
  56. else
  57. j_n
  58. echo "用户$u对应的数字是:$n"
  59. echo $u $n >> $f
  60. fi
  61. fi
  62. done

一个数字的行

  1. [root@centos-04 tmp]# echo "aaaaa1bbbbb" |sed "s/[^0-9]//g"
  2. 1
  3. [root@centos-04 tmp]# echo "aaaaa1bbbbb" |sed "s/[^0-9]//g" |wc -L
  4. 1
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 1.txt
  2. 222222222222
  3. fasf333
  4. 222222222222
  5. fasf333
  6. 222222222222
  7. fasf333
  8. 222222222222
  9. fasf333
  10. 222222222222
  11. fasf333
  12. 222222222222
  13. fasf333
  14. 222222222222
  15. fasf333
  16. 222222222222
  17. fasf333
  18. 222222222222
  19. fasf333
  20. 222222222222
  21. fasf333
  22. 222222222222
  23. fasf333
  24. 222222222222
  25. fasf333
  26. 222222222222
  27. fasf333
  28. 222222222222
  29. fasf333
  30. 222222222222
  31. fasf333
  32. 222222222222
  33. fasf333
  34. 222222222222
  35. fasf333
  36. 222222222222
  37. fasf333
  38. [root@centos-04 tmp]# vim 36.sh
  39. #!/bin/bash
  40. while read line
  41. do
  42. n=`echo $line |sed 's/[^0-9]//g'|wc -L`
  43. if [ $n -eq 1 ]
  44. then
  45. echo $line
  46. fi
  47. done < 1.txt

日志切割归档  

想要实现的效果,当有日志文件1.log.5时把1.log.5文件删除,将1.log.4文件移到为1.log.5,以此类推。

  1. #!/bin/bash
  2. cd /data/logs
  3. #rm 1.log.5
  4. #mv 1.log.4 1.log.5
  5. #mv 1.log.3 1.log.4
  6. #mv 1.log.2 1.log.3
  7. #mv 1.log.1 1.log.2
  8. #mv 1.log 1.log.1
  1. [root@centos-04 tmp]# vim 37.sh
  2. #!/bin/bash
  3. cd /data/logs
  4. log=1.log
  5. mv_log()
  6. {
  7. [ -f $1 ] && mv $1 $2
  8. }
  9.  
  10. [ -f $log.5 ] && rm -f $log.5
  11. for i in `seq 4 -1 1`
  12. do
  13. j=$[$i+1]
  14. mv_log $log.$i $log.$j
  15. done
  16. mv 1.log 1.log.1
  17.  
  18. #rm 1.log.5
  19. #mv 1.log.4 1.log.5
  20. #mv 1.log.3 1.log.4
  21. #mv 1.log.2 1.log.3
  22. #mv 1.log.1 1.log.2
  23. #mv 1.log 1.log.1
  1. [root@centos-04 tmp]# cd /data/logs/
  2. [root@centos-04 logs]# touch 1.log
  3. [root@centos-04 logs]# echo "111" > 1.log
  4. [root@centos-04 logs]# cat 1.log
  5. 111
  6. [root@centos-04 logs]# cd /tmp/
  7. [root@centos-04 tmp]# sh 37.sh
  8. [root@centos-04 tmp]# cd /data/logs/
  9. [root@centos-04 logs]# ls
  10. 1.log.1
  11. [root@centos-04 logs]# cat 1.log.1
  12. 111
  13. [root@centos-04 logs]# touch 1.log
  14. [root@centos-04 logs]# echo '000' > 1.log
  15. [root@centos-04 logs]# cd /tmp/
  16. [root@centos-04 tmp]# sh 37.sh
  17. [root@centos-04 tmp]# cd /data/logs/
  18. [root@centos-04 logs]# ls
  19. 1.log.1
  20. 1.log.2
  21. [root@centos-04 logs]# cat 1.log.1
  22. 000
  23. [root@centos-04 logs]# cat 1.log.2
  24. 111
  25. [root@centos-04 logs]#

查找在线IP  

  1. [root@centos-04 tmp]# vim 38.sh
  2. #!/bin/bash
  3. for i in `seq 1 254`
  4. do
  5. if ping -c 2 -W 2 10.19.37.$i > /dev/null 2>/dev/null
  6. then
  7. echo "10.19.37.$i 是通的。"
  8. else
  9. echo "10.19.37.$i 不通。"
  10. fi
  11. done

检查脚本错误

1.-n 检查脚本错误

2.演示语法错误,我们故意给for前加一个i

  1. [root@centos-04 tmp]# vim 38.sh
  2. #!/bin/bash
  3. ifor i in `seq 1 254`
  4. do
  5. if ping -c 2 -W 2 10.19.37.$i > /dev/null 2>/dev/null
  6. then
  7. echo "10.19.37.$i 是通的。"
  8. else
  9. echo "10.19.37.$i 不通。"
  10. fi
  11. done
  1. [root@centos-04 tmp]# sh -n 38.sh
  2. 38.sh:行3: 未预期的符号 `do' 附近有语法错误
  3. 38.sh:行3: `do'
  4. [root@centos-04 tmp]#
  5. [root@centos-04 tmp]# sh -n 38.sh > /tmp/1.txt 2> /tmp/2.txt
  6. [root@centos-04 tmp]# echo $?
  7. 2
  8. [root@centos-04 tmp]#
  9. [root@centos-04 tmp]# cat /tmp/2.txt
  10. 38.sh:行3: 未预期的符号 `do' 附近有语法错误
  11. 38.sh:行3: `do'
  12. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# sh 39.sh 1.sh
  2. sh: 1.sh: 没有那个文件或目录
  3. 请输入q|Q退出脚本。q
  4. [root@centos-04 tmp]# ls
  5. 1.txt 31.sh 33.sh 35.sh 37.sh 39.sh proxy.log user_number.txt
  6. 2.txt 32.sh 34.sh 36.sh 38.sh 456.log sh.err
  7. [root@centos-04 tmp]# sh 39.sh 31.sh
  8. 脚本31.sh没有语法错误。
  9. [root@centos-04 tmp]# sh 39.sh 38.sh
  10. 38.sh:行3: 未预期的符号 `do' 附近有语法错误
  11. 38.sh:行3: `do'
  12. 请输入q|Q退出脚本。
  13. #!/bin/bash
  14. ifor i in `seq 1 254`
  15. do
  16. if ping -c 2 -W 2 10.19.37.$i > /dev/null 2>/dev/null
  17. then
  18. echo "10.19.37.$i 是通的。"
  19. else
  20. echo "10.19.37.$i 不通。"
  21. fi
  22. done
  23. "38.sh" 10L, 178C 已写入
  24. [root@centos-04 tmp]#

格式化数字  

给每个数字前面添加空格s代表替换,点当前数字&点号代表的数字。

  1. [root@centos-04 tmp]# echo "1234"|sed 's/./& /g'
  2. 1 2 3 4
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 40.sh
  2. #!/bin/bash
  3. n=`echo $1|wc -L`
  4. for d in `echo $1|sed 's/./& /g'`
  5. do
  6. n2=$[$n%3]
  7. if [ $n2 -eq 0 ]
  8. then
  9. echo -n ",$d"
  10. else
  11. echo -n "$d"
  12. fi
  13. n=$[$n-1]
  14. done |sed 's/^,//'
  15. echo
  1. [root@centos-04 tmp]# sh 40.sh 12345
  2. 12,345
  3. [root@centos-04 tmp]# sh 40.sh 123
  4. 123
  5. [root@centos-04 tmp]# sh 40.sh 1235555
  6. 1,235,555

问候脚本  

获取10小时之前的时间

  1. [root@centos-04 tmp]# d=`date -d "-10 hours" +%H`
  2. [root@centos-04 tmp]# echo $d
  3. 17
  1. [root@centos-04 tmp]# vim 41.sh
  2. #!/bin/bash
  3. d=`date +%H`
  4. if [ $d -ge 0 -a $d -lt 7 ]
  5. then
  6. tag=1
  7. elif [ $d -ge 7 -a $d -lt 12 ]
  8. then
  9. tag=2
  10. elif [ $d -ge 12 -a $d -lt 18 ]
  11. then
  12. tag=3
  13. else
  14. tag=4
  15. fi
  16.  
  17. case $tag in
  18. 1)
  19. echo "早上好"
  20. ;;
  21. 2)
  22. echo "上午好"
  23. ;;
  24. 3)
  25. echo "下午好"
  26. ;;
  27. 4)
  28. echo "晚上好"
  29. ;;
  30. *)
  31. echo "脚本出错了"
  32. ;;
  33. esac
  34. ~
  35. ~
  36. "41.sh" 32L, 333C 已写入
  37. [root@centos-04 tmp]#
  38. [root@centos-04 tmp]# sh 41.sh
  39. 早上好
  40. [root@centos-04 tmp]#

菜单脚本

  1. [root@centos-04 tmp]# vim 42.sh
  2. #!/bin/bash
  3. PS3="Please input your choice:" 为了去掉结果中的#?
  4. select i in w ls pwd quit
  5. do
  6. case $i in
  7. w)
  8. w
  9. ;;
  10. ls)
  11. ls
  12. ;;
  13. pwd)
  14. pwd
  15. ;;
  16. quit)
  17. exit
  18. ;;
  19. *)
  20. echo "please input 1-3."
  21. ;;
  22. esac
  23. done
  24.  
  25. "42.sh" [新] 21L, 179C 已写入
  26. [root@centos-04 tmp]# sh 4
  27. 40.sh 41.sh 42.sh 456.log
  28. [root@centos-04 tmp]# sh 42.sh
  29. 1) w
  30. 2) ls
  31. 3) pwd
  32. 4) quit
  33. #? 1
  34. 03:48:09 up 4 days, 8:11, 1 user, load average: 0.00, 0.01, 0.05
  35. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  36. root pts/0 192.168.242.1 21:44 1.00s 0.26s 0.01s w
  37. #? 2
  38. 1.txt 31.sh 33.sh 35.sh 37.sh 39.sh 41.sh 456.log sh.err
  39. 2.txt 32.sh 34.sh 36.sh 38.sh 40.sh 42.sh proxy.log user_number.txt
  40. #? 3
  41. /tmp
  42. #? 4
  43. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# echo -e "1) w\n2) ls\n3) pwd\n4)"
  2. 1) w
  3. 2) ls
  4. 3) pwd
  5. 4)
  6. [root@centos-04 tmp]#
  7. [root@centos-04 tmp]# vim 42-2.sh
  8. #!/bin/bash
  9. echo -e "1) w\n2) ls\n3) pwd\n4) quit"
  10. while :
  11. do
  12. read -p "Please input your choice(1-4):" c
  13. case $c in
  14. 1)
  15. w
  16. ;;
  17. 2)
  18. ls
  19. ;;
  20. 3)
  21. pwd
  22. ;;
  23. 4)
  24. exit
  25. ;;
  26. *)
  27. echo "Please input 1-4"
  28. ;;
  29. esac
  30. done
  31. "42-2.sh" 23L, 219C 已写入
  32. [root@centos-04 tmp]# sh 42-2.sh
  33. 1) w
  34. 2) ls
  35. 3) pwd
  36. 4) quit
  37. Please input your choice(1-4):1
  38. 04:42:46 up 4 days, 9:06, 1 user, load average: 0.00, 0.01, 0.05
  39. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  40. root pts/0 192.168.242.1 21:44 6.00s 0.27s 0.00s w
  41. Please input your choice(1-4):2
  42. 1.txt 31.sh 33.sh 35.sh 37.sh 39.sh 41.sh 42.sh proxy.log user_number.txt
  43. 2.txt 32.sh 34.sh 36.sh 38.sh 40.sh 42-2.sh 456.log sh.err
  44. Please input your choice(1-4):
  45. Please input 1-4
  46. Please input your choice(1-4):3
  47. /tmp
  48. Please input your choice(1-4):4
  49. [root@centos-04 tmp]#

检查用户是否登录  

思路:通过w命令获取第一列的用户

  1. [root@centos-04 tmp]# vim 43.sh
  2. #!/bin/bash
  3. while :
  4. do
  5. if w|sed '1'd|awk '{print $1}'|grep -qw "$1"
  6. then
  7. echo "用户$1已经登录系统"
  8. exit
  9. fi
  10. sleep 300
  11. done
  12.  
  13. [root@centos-04 tmp]# sh 43.sh root
  14. 用户root已经登录系统
  15. [root@centos-04 tmp]#

检查系统是否入侵

  1. $0 当前脚本的文件名
  2. $n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2
  3. $# 传递给脚本或函数的参数个数。
  4. $* 传递给脚本或函数的所有参数。
  5. $@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。
  6. $? 上个命令的退出状态,或函数的返回值。
  7. $$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID

有时候,你会想手动跟踪命令的输出内容,同时又想将输出的内容写入文件

tee 命令基于标准输入读取数据,标准输出或文件写入数据

  1. [root@centos-04 tmp]# vim 44.sh
  2. #!/bin/bash
  3. pp=$$
  4. ps -elf |sed '1'd > /tmp/pid.txt
  5. for pid in `awk -v ppn=$pp '$5!=ppn {print $4}' /tmp/pid.txt`
  6. do
  7. if ! [ -d /proc/$pid ]
  8. then
  9. echo "系统中并没有pid为$pid的目录,需要检查。"
  10. fi
  11. done
  12. [root@centos-04 tmp]# sh 44.sh

三行并一行

  1. [root@centos-04 tmp]# vim 45.sh
  2. #!/bin/bash
  3. n=1
  4. cat $1 |while read line
  5. do
  6. n1=$[$n%3]
  7. if [ $n1 -eq 0 ]
  8. then
  9. echo "$line"
  10. else
  11. echo -n "$line"
  12. fi
  13. n=$[$n+1]
  14. done
  15.  
  16. [root@centos-04 tmp]# sh 45.sh 1.txt
  17. 123
  18. 456
  19. 78 3333[root@centos-04 tmp]#

网卡和ip

~就是表示用来匹配后面的正则表达式,告诉awk后面开始是正则语法。

NF 表示的是浏览记录的元素的个数 
$NF 表示的最后一个Field(列),即输出最后一个字段的内容

  1. [root@localhost SHELL]# free -m | grep buffers\/
  2. -/+ buffers/cache: 1815 1859
  3. [root@localhost SHELL]# free -m | grep buffers\/ | awk '{print $NF}'
  4. 1859
  5. [root@localhost SHELL]# free -m | grep buffers\/ | awk '{print NF}'
  6. 4
  7. [root@localhost SHELL]#

  

  1. [root@centos-04 tmp]# ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}'
  2. lo
  3. ens33
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 46.sh
  2. #!/bin/bash
  3. ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/ifs.txt
  4. get_ip()
  5. {
  6. ip add show dev $1 |grep inet |awk '{print $2}' |awk -F '/' '{print $1}'
  7. }
  8.  
  9. for eth in `cat /tmp/ifs.txt`
  10. do
  11. myip=`get_ip $eth`
  12. if [ -z "$myip" ]
  13. then
  14. echo $eth
  15. else
  16. echo $eth $myip
  17. fi
  18. done > /tmp/if_ip.txt
  19.  
  20. if [ $# -ne 2 ]
  21. then
  22. echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip"
  23. exit
  24. fi
  25.  
  26. if [ $1 == "-i" ]
  27. then
  28. if awk '{print $1}' /tmp/if_ip.txt |grep -qw $2
  29. then
  30. eth=$2
  31. ip1=`awk -v aeth=$eth '$1==aeth' /tmp/if_ip.txt|sed "s/$eth //"`
  32. echo "网卡$2的ip是 $ip1"
  33. else
  34. echo "你指定的网卡不对,系统中的网卡有:`cat /tmp/ifs.txt|xargs`"
  35. exit
  36. fi
  37. elif [ $1 == "-I" ]
  38. then
  39. if grep -qw " $2 " /tmp/if_ip.txt
  40. then
  41. eth=`grep -w " $2 " /tmp/if_ip.txt|awk '{print $1}'`
  42. echo "IP $2对应的网卡是$eth"
  43. else
  44. echo "你指定的ip不对,系统中的IP有:`ip add |grep inet |awk '{print $2}'|awk -F '/' '{print $1}'|xargs`"
  45. exit
  46. fi
  47. else
  48. echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip"
  49. fi
  1. [root@centos-04 tmp]# sh 46.sh
  2. 请输入正确的格式: bash 46.sh -i 网卡 或者 bash 46.sh -I ip
  3. [root@centos-04 tmp]# sh 46.sh -i eth33
  4. 你指定的网卡不对,系统中的网卡有:lo ens33
  5. [root@centos-04 tmp]# sh 46.sh -i ens33
  6. 网卡ens33ip 192.168.242.130 fe80::6244:d336:eacf:c4d6
  7. [root@centos-04 tmp]# sh 46.sh -i lo
  8. 网卡loip 127.0.0.1 ::1
  9. [root@centos-04 tmp]# sh 46.sh -I 192.168.242.130
  10. 你指定的ip不对,系统中的IP有:127.0.0.1 ::1 192.168.242.130 fe80::6244:d336:eacf:c4d6
  11. [root@centos-04 tmp]#

随机3位数

  1. [root@centos-04 tmp]# echo $RANDOM
  2. 26768
  3. [root@centos-04 tmp]# echo $[$RANDOM%10]
  4. 6
  5. [root@centos-04 tmp]# echo $[$RANDOM%10]
  6. 8
  7. [root@centos-04 tmp]# for i in `seq 0 2`; do a[$i]=$[$RANDOM%10]; done; echo ${a[@]}|sed s'/ //g'
  8. 203
  9. [root@centos-04 tmp]#

把数组按字符串输出

  1. echo ${a[@]}
  1. [root@centos-04 tmp]#vim 47.sh
  2. #!/bin/bash
  3. get_number()
  4. {
  5. for i in `seq 0 2`
  6. do
  7. a[$i]=$[$RANDOM%10]
  8. done
  9. echo ${a[@]}|sed s'/ //g'
  10. }
  11.  
  12. if [ $# -eq 0 ]
  13. then
  14. get_number
  15. elif [ $# -eq 1 ]
  16. then
  17. n=`echo $1|sed 's/[0-9]//g'`
  18. if [ -n "$n" ]
  19. then
  20. echo "给定的参数必须是一个数字"
  21. exit
  22. fi
  23. for i in `seq 1 $1`
  24. do
  25. get_number
  26. done |xargs
  27. else
  28. echo "格式不对,正确的格式是sh $0 [n],这里的n是一个数字。"
  29. fi
  30. ~
  31. ~
  32. ~
  33. ~
  34. ~
  35. "47.sh" 28L, 414C 已写入
  36. [root@centos-04 tmp]# sh 47.sh
  37. 845
  38. [root@centos-04 tmp]# sh 47.sh 10
  39. 898 422 695 369 417 402 573 800 957 614
  40. [root@centos-04 tmp]#

是否安装包

查看是否安装了包,未安装的包echo $? 会输出1,安装的包输出0

  1. [root@centos-04 tmp]# rpm -q httpd
  2. httpd-2.4.6-88.el7.centos.x86_64
  3. [root@centos-04 tmp]# yum list|grep httpd
  4. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
  5. httpd.x86_64 2.4.6-88.el7.centos @base
  1. [root@centos-04 tmp]# rpm -q mysql
  2. 未安装软件包 mysql
  3. [root@centos-04 tmp]# echo $?
  4. 1
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 48.sh
  2. #!/bin/bash
  3. if_install()
  4. {
  5. rpm -q $1 >/dev/null 2>/dev/null
  6. if [ $? -eq 0 ]
  7. then
  8. echo "$1已经安装"
  9. return 0
  10. else
  11. echo "$1没有安装"
  12. return 1
  13. fi
  14. }
  15.  
  16. if_install httpd
  17. if [ $? -eq 0 ]
  18. then
  19. if ! pgrep httpd >/dev/null
  20. then
  21. service httpd start
  22. fi
  23. else
  24. yum install -y httpd
  25. fi
  26.  
  27. if_install mysql-server
  28. if [ $? -eq 0 ]
  29. then
  30. if ! pgrep mysqld >/dev/null
  31. then
  32. service mysqld start
  33. fi
  34. else
  35. yum install -y mysql-server
  36. fi
  1. [root@centos-04 tmp]# sh 48.sh
  2. httpd已经安装
  3. Redirecting to /bin/systemctl start httpd.service
  4. Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
  5. mysql-server没有安装
  6. yum install -y mysql-server
  7. [root@centos-04 tmp]#

日期是否合法  

查看日期是否在指定的月中

  1. [root@centos-04 tmp]# cal 02 2010
  2. 二月 2010

  3. 1 2 3 4 5 6
  4. 7 8 9 10 11 12 13
  5. 14 15 16 17 18 19 20
  6. 21 22 23 24 25 26 27
  7. 28
  8.  
  9. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# cal 02 2010
  2. 二月 2010

  3. 1 2 3 4 5 6
  4. 7 8 9 10 11 12 13
  5. 14 15 16 17 18 19 20
  6. 21 22 23 24 25 26 27
  7. 28
  8.  
  9. [root@centos-04 tmp]# cal 02 2010|grep -w 28
  10. 28
  11. [root@centos-04 tmp]# cal 02 2010|grep -w 30
  12. [root@centos-04 tmp]#
  1. [root@centos-04 php_client]# y=2012; m=02; d=30
  2. [root@centos-04 php_client]# cal $m $y
  3. 二月 2012

  4. 1 2 3 4
  5. 5 6 7 8 9 10 11
  6. 12 13 14 15 16 17 18
  7. 19 20 21 22 23 24 25
  8. 26 27 28 29
  9.  
  10. [root@centos-04 php_client]# cal $m $y|grep -w $d grep发现没有东西,那说明我们的日期不合法)
  11. [root@centos-04 php_client]#
  1. [root@centos-04 /]# a=20180418
  2. [root@centos-04 /]# echo ${a:0:4}(取变量的前四个字符)
  3. 2018
  4. [root@centos-04 /]#
  1. ${#1} (获取参数的长度,$1的长度)
  1. [root@centos-04 /]# echo '2018'|grep -q "^0"
  2. [root@centos-04 /]# echo $?
  3. 1
  4. [root@centos-04 /]# echo '0018'|grep -q "^0"
  5. [root@centos-04 /]# echo $?
  6. 0
  7. [root@centos-04 /]#
  1. [root@centos-04 /]# vim 49.sh
  2. #!/bin/bash
  3. if [ $# -ne 1 ] || [ ${#1} -ne 8 ]
  4. then
  5. echo 'please input sh $0 yyyymmdd'
  6. exit 1
  7. fi
  8.  
  9. y=`echo ${1:0:4}`
  10. m=`echo ${1:4:2}`
  11. d=`echo ${1:6:2}`
  12.  
  13. if echo $d|grep -q "^0"
  14. then
  15. d=`echo ${1:6:2}|sed 's/^0//'`
  16. fi
  17.  
  18. if cal $m $y > /dev/null 2>/dev/null
  19. then
  20. if ! cal $m $y |grep -qw "$d"
  21. then
  22. echo "你给的日期是不合法的"
  23. else
  24. echo "日期合法"
  25. fi
  26. else
  27. echo "你给的日期不合法"
  28. fi
  29. [root@centos-04 /]# sh 49.sh 20190101
  30. 日期合法
  31. [root@centos-04 /]# sh 49.sh 20190131
  32. 日期合法
  33. [root@centos-04 /]# sh 49.sh 20190132
  34. 你给的日期是不合法的
  35. [root@centos-04 /]#

监控流量 

检查网卡流量

  1. [root@centos-04 /]# sar -n DEV 1 5 |grep ens33
  2. 014232 ens33 1.01 0.00 0.06 0.00 0.00 0.00 0.00
  3. 014233 ens33 1.01 1.01 0.06 0.22 0.00 0.00 0.00
  4. 014234 ens33 0.99 0.99 0.06 0.21 0.00 0.00 0.00
  5. 014235 ens33 1.01 1.01 0.06 0.22 0.00 0.00 0.00
  6. 014236 ens33 1.00 1.00 0.06 0.21 0.00 0.00 0.00
  7. 平均时间: ens33 1.00 0.80 0.06 0.17 0.00 0.00 0.00
  8. [root@centos-04 /]#
  1. root@centos-04 /]# vim 50.sh
  2. #!/bin/bash
  3. LANG=en
  4. sar -n DEV 1 10|grep -w "$1" > /tmp/sar.tmp
  5. in=`grep "Average:" /tmp/sar.tmp|awk '{print $5}'|sed 's/\.//'`
  6. out=`grep "Average:" /tmp/sar.tmp|awk '{print $6}'|sed 's/\.//'`
  7.  
  8. if [ $in == "000" ] && [ $out == '000']
  9. then
  10. ifdown $1
  11. ifup $1
  12. fi
  13. [root@centos-04 /]# sh 50.sh ens33

判断网站是否正常  

  1. [root@centos-04 /]# curl -I www.baidu.com 2>/dev/null |head -1 (加上将错误输出到null里,不加会输出下面的红色部分)
  2. HTTP/1.1 200 OK
  3. [root@centos-04 /]# curl -I www.baidu.com |head -1
  4. % Total % Received % Xferd Average Speed Time Time Time Current
  5. Dload Upload Total Spent Left Speed
  6. 0 277 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
  7. HTTP/1.1 200 OK
  8. [root@centos-04 /]#
  1. [root@centos-04 /]# curl -I www.baidu.com 2>/dev/null |head -1|awk '{print $2}'
  2. 200
  3. [root@centos-04 /]#
  1. [root@centos-04 tmp]# vim 50.sh
  2. #!/bin/bash
  3. #这个脚本用来判断一个网址是否正常
  4. url='http://www.baidu.com'
  5. mail_user=sun.yujun@zol.com.cn
  6. code=`curl -I $url 2>/tmp/curl.err|head -1|awk '{print $2}'`
  7.  
  8. if [ -z $code ]
  9. then
  10. python mail.py $mail_user "$url访问异常" "`cat /tmp/curl.err`"
  11. exit
  12. elif [ $code != "200" ]
  13. then
  14. curl -I $url &>/tmp/curl.log
  15. python mail.py $mail_user "$url访问异常 状态码$code" "`/tmp/curl.log`"
  16. fi
  17.  
  18. [root@centos-04 tmp]# sh -x 50.sh
  19. + url=http://www.baidu.com
  20. + mail_user=sun.yujun@zol.com.cn
  21. ++ curl -I http://www.baidu.com
  22. ++ head -1
  23. ++ awk '{print $2}'
  24. + code=200
  25. + '[' -z 200 ']'
  26. + '[' 200 '!=' 200 ']'
  27. [root@centos-04 tmp]#   

这里发邮件需要把mail.py文件放上  

小于5k文件打包

$HOME变量可以代替用户的家目录,当前登录的是哪个用户变量就指向这个用户的家目录。

  1. [root@centos-04 tmp]# date +%F
  2. 2019-03-10
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# tar zcvf 2018-02-02.tar.gz 1.txt 2.txt^C
  2. [root@centos-04 tmp]# find $HOME/ -type f -size -5k |xargs xargs命令可以将打印结果放到一行)
  1. root@centos-04 tmp]# vim 52.sh
  2.  
  3. #!/bin/bash
  4. t=`date +%F`
  5.  
  6. cd $HOME
  7. tar czf $t.tar.gz `find ./ -type f -size -5k|xargs`
  8. [root@centos-04 tmp]# sh -x 52.sh
  9. ++ date +%F
  10. + t=2019-03-10
  11. + cd /root
  12. ++ find ./ -type f -size -5k
  13. ++ xargs
  14. + tar czf 2019-03-10.tar.gz ./.bash_logout ./.bash_profile
  15. [root@centos-04 home]# cd ~
  16. [root@centos-04 ~]# ls
  17. 2019-03-10.tar.gz clouddemo.py
  18. [root@centos-04 ~]# tar -tf 2019-03-10.tar.gz (-tf查看压缩包里的文件列表)

监控22端口是否被封  

  1. [root@centos-04 ~]# iptables -nvL INPUT
  2. Chain INPUT (policy ACCEPT 123K packets, 40M bytes)
  3. pkts bytes target prot opt in out source destination
  4. [root@centos-04 ~]# iptables -I INPUT -p tcp --dport 22 -s 1.1.1.1 -j DROP
  5. [root@centos-04 ~]# iptables -I INPUT -p tcp --dport 22 -s 1.1.1.2 -j REJECT
  6. [root@centos-04 ~]# iptables -nvL INPUT
  7. Chain INPUT (policy ACCEPT 9 packets, 568 bytes)
  8. pkts bytes target prot opt in out source destination
  9. 0 0 REJECT tcp -- * * 1.1.1.2 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable
  10. 0 0 DROP tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:22
  11. [root@centos-04 ~]#
  1. [root@centos-04 tmp]# iptables -nvL INPUT |grep -w 'dpt:22' (或者' dpt:22 '
  2. 0 0 REJECT tcp -- * * 1.1.1.2 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable
  3. 0 0 DROP tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:22
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# iptables -nvL INPUT |grep -w 'dpt:22' |awk '$3 ~/REJECT|DROP/' (正则匹配第三段的REJECT|DROP)
  2. 0 0 REJECT tcp -- * * 1.1.1.2 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable
  3. 0 0 DROP tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:22
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}'
  2. 1
  3. 2
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 53.sh
  2. #!/bin/bash
  3.  
  4. iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}' > /tmp/iptables.log
  5. n=`cat /tmp/iptables.log |wc -l`
  6.  
  7. if [ $n -gt 0 ]
  8. then
  9. for n in `tac /tmp/iptables.log`
  10. do
  11. iptables -D INPUT $n
  12. done
  13. fi
  14.  
  15. [root@centos-04 tmp]# sh -x 53.sh
  16. + iptables -nvL INPUT --line-numbers
  17. + grep -w dpt:22
  18. + awk '$4 ~/REJECT|DROP/ {print $1}'
  19. ++ cat /tmp/iptables.log
  20. ++ wc -l
  21. + n=2
  22. + '[' 2 -gt 0 ']'
  23. ++ tac /tmp/iptables.log
  24. + for n in '`tac /tmp/iptables.log`'
  25. + iptables -D INPUT 2
  26. + for n in '`tac /tmp/iptables.log`'
  27. + iptables -D INPUT 1
  28. [root@centos-04 tmp]# iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}'
  29. [root@centos-04 tmp]#

分析日志  

查看date函数用法

  1. [root@centos-04 logs]# man date
  1. [root@centos-04 logs]# 28/Jul/2018:08:04:10^C
  2. [root@centos-04 logs]# LANG=en
  3. [root@centos-04 logs]# date +%d/%b/%Y:%T
  4. 11/Mar/2019:04:30:44
  5. [root@centos-04 logs]#   

核心命令

  1. [root@centos-04 logs]# egrep "date +%d/%b/%Y:1[01]:[0-5]:[0-9]:" /usr/local/nginx/logs/access.log |awk '{print $1}' |sort -n |uniq -c |sort -n |tail -1 |awk '{print $2}'
  1. [root@centos-04 tmp]# vim 54.sh
  2. #!/bin/bash
  3. #这个脚本用来分析Nginx访问日志
  4. #作者:SYJ
  5. #日期:2019-03-22
  6. export LANG=en
  7. log="/usr/local/nginx/logs/access.log"
  8. t=`date +%d/%b/%Y:1[01]:[0-5][0-9]:`
  9.  
  10. egrep "$t" $log |awk '{print $1}' |sort -n |uniq -c |sort -n |tail -1 |awk '{print $2}'

打印数字 

  1. [root@centos-04 tmp]# read -p "please: " n
  2. please: 4
  3. [root@centos-04 tmp]# echo $n
  4. 4
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# for i in `seq 1 $n`; do echo $i; done
  2. 1
  3. 2
  4. 3
  5. 4
  6. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# echo $n
  2. 4
  3. [root@centos-04 tmp]# echo $n |sed 's/[0-9]//g'(判断变量中是否全是数字,将数字全都替换成空,如果还有值说明不全是数字)
  4.  
  5. [root@centos-04 tmp]# n=a
  6. [root@centos-04 tmp]# echo $n |sed 's/[0-9]//g'
  7. a
  8. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 55.sh
  2. #!/bin/bash
  3. #这个脚本用来打印数字
  4. #作者:SYJ
  5. #日期:2019-03-22
  6. read -p "Please input a number: " n
  7. n1=`echo $n |sed 's/[0-9]//g'`
  8. if [ -n "$n1" ]
  9. then
  10. echo "Please input a number."
  11. exit
  12. fi
  13.  
  14. for i in `seq 1 $n`
  15. do
  16. echo $i
  17. done
  18.  
  19. read -p "If continue? y/n" c
  20. case $c in
  21. n|N)
  22. exit
  23. ;;
  24. y|Y)
  25. read -p "Please input a number: " n2
  26. n3=`echo $n2 |sed 's/[0-9]//g'`
  27. if [ -n "$n3" ]
  28. then
  29. echo "Please input a number."
  30. exit
  31. fi
  32. if [ $n2 -le $n ]
  33. then
  34. echo "$n2 should grater than $n."
  35. exit
  36. fi
  37. for i in `seq $[$n+1] $n2`
  38. do
  39. echo $i
  40. done
  41. ;;
  42. *)
  43. echo "Please input y or n."
  44. ;;
  45. esac
  1. [root@centos-04 tmp]# sh 55.sh
  2. Please input a number: 5
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. If continue? y/ny
  9. Please input a number: 8
  10. 6
  11. 7
  12. 8
  13. [root@centos-04 tmp]#

给文件增加内容  

创建一个文件写入1、2、3、4、5、6

  1. [root@centos-04 tmp]# vi 1.txt
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6  

在第五列后面添加文本123456789

  1. [root@centos-04 tmp]# sed '5a123456789' 1.txt
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 123456789
  8. 6  

添加\n就是回车换行加上-i参数直接修改文件

  1. [root@centos-04 tmp]# sed -i '5a12345\n6789' 1.txt
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 12345
  8. 6789
  9. 6
  10. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 56.sh
  2. #!/bin/bash
  3. #这个脚本用来为文件增加行
  4. #作者:star
  5. #日期:2019-03-25
  6. n=0
  7. cat 1.txt |while read line
  8. do
  9. n=$[$n+1]
  10. if [ $n -eq 5 ]
  11. then
  12. echo $line
  13. echo -e "# This is a test file.\n# Test insert line into this file."
  14. else
  15. echo $line
  16. fi
  17. done
  18.  
  19. [root@centos-04 tmp]# sh 56.sh
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. # This is a test file.
  26. # Test insert line into this file.
  27. 6
  28. [root@centos-04 tmp]#

备份etc目录  

  1. [root@centos-04 tmp]# date +%d
  2. 11
  3. [root@centos-04 tmp]# date +%y
  4. 19
  5. [root@centos-04 tmp]# date +%Y
  6. 2019
  7. [root@centos-04 tmp]# date +%y%m%d
  8. 190311
  9. [root@centos-04 tmp]# date +%Y%m%d
  10. 20190311
  11. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# date +%F
  2. 2019-03-11
  3. [root@centos-04 tmp]#   

自动添加shell脚本注释

  1. [root@centos-04 tmp]# vim add_title.sh
  2. #!/bin/bash
  3. d=`date +%F`
  4. cat >$1<<EOF
  5. #!/bin/bash
  6. #这个脚本用来
  7. #作者:SYJ
  8. #日期:$d
  9. EOF
  10.  
  11. [root@centos-04 tmp]# sh add_title.sh 57.sh
  12. [root@centos-04 tmp]# vim 57.sh
  13. #!/bin/bash
  14. #这个脚本用来
  15. #作者:SYJ
  16. #日期:2019-03-11
  1. [root@centos-04 tmp]# vim 57.sh
  2. #!/bin/bash
  3. #这个脚本用来备份/etc/目录
  4. #作者:SYJ
  5. #日期:2019-03-11
  6.  
  7. d1=`date +%d`
  8. d2=`date +%y%m%d`
  9. if [ $d1 == "01" ]
  10. then
  11. cd /etc/
  12. tar -czf "/root/bak/${d2}_etc.tar.gz" ./
  13. fi
  1. [root@centos-04 tmp]# sh 57.sh
  1. [root@centos-04 tmp]# cd /root/bak/
  2. [root@centos-04 bak]# ls
  3. 190301_etc.tar.gz
  4. [root@centos-04 bak]#

找出重复的单词  

将非字母的字符全部替换成空格

  1. [root@centos-04 tmp]# cat /etc/passwd |sed 's/[^a-zA-Z]/ /g'
  2. root x root root bin bash
  3. bin x bin bin sbin nologin  

将字母放到一列

  1. [root@centos-04 tmp]# for w in `cat /etc/passwd|sed 's/[^a-zA-Z]/ /g'`; do echo $w; done
  2. root
  3. x
  4. root
  5. root
  6. bin
  7. bash
  8. bin
  9. x
  10. bin
  1. [root@centos-04 tmp]# for w in `cat /etc/passwd|sed 's/[^a-zA-Z]/ /g'`; do echo $w; done |sort |uniq -c|sort -nr |head
  2. 34 x
  3. 33 sbin
  4. 27 nologin
  5. 14 var
  6. 8 bin
  7. 7 lib
  8. 6 user
  9. 5 User
  10. 4 systemd
  11. 4 root
  12. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# sh add_title.sh 58.sh
  2. [root@centos-04 tmp]# vim 58.sh
  3. #!/bin/bash
  4. #这个脚本用来找出重复的单词
  5. #作者:SYJ
  6. #日期:2019-03-11
  7.  
  8. for w in `sed 's/[^a-zA-Z]/ /g' $1`
  9. do
  10. echo $w
  11. done |sort |uniq -c |sort -nr |head
  12.  
  13. [root@centos-04 tmp]# sh 58.sh /etc/passwd
  14. 34 x
  15. 33 sbin
  16. 27 nologin
  17. 14 var
  18. 8 bin
  19. 7 lib
  20. 6 user
  21. 5 User
  22. 4 systemd
  23. 4 root
  24. [root@centos-04 tmp]#

人员分组

首先需要有一个人员列表文件

  1. [root@centos-04 tmp]# vim member.txt
  2. aaaaaaa
  3. bbbb
  4. ccccc
  5. dddddddd
  6. eeeeeeeeeee
  7. ffffff
  8. gggggggg
  9. hhhhhhhhh  

需要安装bc命令,bc命令是linux下面的计算器

  1. [root@centos-04 tmp]# yum install -y bc

计算一个字符cksum的值(是一串数字)

  1. [root@centos-04 tmp]# echo adfasdf |cksum
  2. 3506188467 8
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# echo -e "1\n2\n3\n4\n5"|shuf shuf打乱列顺序)
  2. 3
  3. 2
  4. 5
  5. 4
  6. 1
  7. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 59.sh
  2. #!/bin/bash
  3. #这个脚本用来人员分组
  4. #作者:SYJ
  5. #日期:2019-03-11
  6.  
  7. #人员列表文件
  8. f=member.txt
  9. #小组数
  10. group_n=3
  11.  
  12. #人员总数
  13. member_n=`wc -l $f |awk '{print $1}'`
  14.  
  15. #根据姓名计算该用户所在小组的id
  16. get_n()
  17. {
  18. #根据姓名计算cksum值
  19. l=`echo $1 |cksum|awk '{print $1}'`
  20. #获取一个随机数
  21. n1=$RANDOM
  22. #cksum值和随机数相机,然后除以小组数取余,这样可以确保每次获取到的余数都不一样
  23. n2=$[$n1+$l]
  24. g_id=$[$n1%$group_n]
  25. #假如小组数为7,则余数范围0-6,如果余数为0,则小组为7
  26. if [ $g_id -eq 0 ]
  27. then
  28. g_id=$group_n
  29. fi
  30. echo $g_id
  31. }
  32.  
  33. for i in `seq 1 $group_n`
  34. do
  35. #n_$i.txt为临时文件,用来记录该小组内的成员
  36. #脚本之前执行过,则该文件会存在,本次执行脚本前应该删除掉这个临时文件
  37. [ -f n_$i.txt ] && rm -f n_$i.txt
  38. done
  39.  
  40. shuf $f |while read name
  41. do
  42. #计算用户所在小组的id
  43. g=`get_n $name`
  44. #将人员追加写入到他对应的小组里
  45. echo $name >> n_$g.txt
  46. done
  47.  
  48. #定义计算文件行数的函数
  49. nu(){
  50. wc -l $1 |awk '{print $1}'
  51. }
  52.  
  53. #获取组员人数最多的小组
  54. max(){
  55. ma=0;
  56. for i in `seq 1 $group_n|shuf`
  57. do
  58. n=`nu n_$i.txt`
  59. if [ $n -gt $ma ]
  60. then
  61. ma=$n
  62. fi
  63. done
  64. echo $ma
  65. }
  66.  
  67. #获取组员人数最少的小组
  68. min(){
  69. mi=$member_n
  70. for i in `seq 1 $group_n|shuf`
  71. do
  72. n=`nu n_$i.txt`
  73. if [ $n -lt $mi ]
  74. then
  75. mi=$n
  76. fi
  77. done
  78. echo $mi
  79. }
  80.  
  81. #定义四舍五入函数
  82. div(){
  83. n=`echo "scale=1;$1/$2"|bc`
  84. n1=`echo "scale=1;$n+0.5"|bc`
  85. echo $n1|cut -d. -f1
  86. }
  87.  
  88. #小组组员平均值(非四舍五入)
  89. ava_n=$[$member_n/$group_n]
  90. #小组组员平均值(四舍五入)
  91. ava_n1=`div $member_n $group_n`
  92.  
  93. if [ $ava_n -eq $ava_n1 ]
  94. then
  95. #定义初始最小值
  96. ini_min=1
  97. #以下while循环要做的事情,就是要把人数多的组里的人搞到人数少的组里去
  98. #此while循环的条件是,当人数最少的组成员数小于组员平均值
  99. while [ $ini_min -lt $ava_n1 ]
  100. do
  101. #找出人数最多的组
  102. m1=`max`
  103. #找出人数最少的组
  104. m2=`min`
  105. for i in `seq 1 $group_n|shuf`
  106. do
  107. n=`nu n_$i.txt`
  108. #找到人数最多的组对应的文件f1(可能有多个,这里取出现的第一个即可)
  109. if [ $n -eq $m1 ]
  110. then
  111. f1=n_$i.txt
  112. #找到人数最少的组对应的文件f2(可能有多个,这里取出现的第一个即可)
  113. elif [ $n -eq $m2 ]
  114. then
  115. f2=n_$i.txt
  116. fi
  117. done
  118. #取f1中最后一个人名
  119. name=`tail -n1 $f1`
  120. #将这个人名追加写入f2中
  121. echo $name >> $f2
  122. #在f1中删除刚刚取走的人名
  123. sed -i "/$name/d" $f1
  124. #把此时的最少组人员数赋值给ini_min
  125. ini_min=`min`
  126. done
  127. else
  128. #定义初始最大值
  129. ini_max=$member_n
  130. while [ $ini_max -gt $ava_n1 ]
  131. do
  132. #找出人数最多的组
  133. m1=`max`
  134. #找出人数最少的组
  135. m2=`min`
  136. for i in `seq 1 $group_n|shuf`
  137. do
  138. n=`nu n_$i.txt`
  139. #找到人数最多的组对应的文件f1(可能有多个,这里取出现的第一个即可)
  140. if [ $n -eq $m1 ]
  141. then
  142. f1=n_$i.txt
  143. #找到人数最少的组对应的文件f2(可能有多个,这里取出现的第一个即可)
  144. elif [ $n -eq $m2 ]
  145. then
  146. f2=n_$i.txt
  147. fi
  148. done
  149. #取f1中最后一个人名
  150. name=`tail -n1 $f1`
  151. #将这个人名追加写入f2中
  152. echo $name >> $f2
  153. #在f1中删除刚刚取走的人名
  154. sed -i "/$name/d" $f1
  155. #把此时的最少组人员数赋值给ini_min
  156. ini_max=`max`
  157. done
  158. fi
  159.  
  160. for i in `seq 1 $group_n`
  161. do
  162. echo -e "\033[34m$i 组成员有:\033[0m"
  163. cat n_$i.txt
  164. #把临时文件删除
  165. rm -f n_$i.txt
  166. echo
  167. done
  1. [root@centos-04 tmp]# sh 59.sh
  2. 1 组成员有:
  3. aaaaaaa
  4. eeeeeeeeeee
  5. ffffff
  6.  
  7. 2 组成员有:
  8. hhhhhhhhh
  9. bbbb
  10.  
  11. 3 组成员有:
  12. dddddddd
  13. gggggggg
  14. ccccc
  15.  
  16. [root@centos-04 tmp]#

比较两个数大小  

bc命令返回1代表成立,返回0代表不成立

  1. [root@centos-04 tmp]# x=10.1; y=11
  2. [root@centos-04 tmp]# echo "$x > $y"|bc
  3. 0
  4. [root@centos-04 tmp]# echo "$x < $y"|bc
  5. 1
  6. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 60.sh
  2.  
  3. if_number()
  4. {
  5.  
  6. if echo $1|grep -q '^-'
  7. then
  8. nu=`echo $1|sed 's/^-//'`
  9. else
  10. nu=$1
  11. fi
  12. n=`echo $nu|sed 's/[0-9.]//g'`
  13.  
  14. if [ -n "$n" ]
  15. then
  16. echo "$1不是合法数字。"
  17. exit
  18. fi
  19. if echo $1|grep -q '^\.'
  20. then
  21. echo "$1不是合法数字。"
  22. exit
  23. fi
  24. }
  25.  
  26. if_number $1
  27. if_number $2
  28.  
  29. n1=`echo "$1>$2"|bc`
  30. if [ $n1 -eq 1 ]
  31. then
  32. echo "$1 > $2"
  33. else
  34. if [ "$1" == "$2" ]
  35. then
  36. echo "$1=$2"
  37. else
  38. echo "$1 < $2"
  39. fi
  40. fi
  1. [root@centos-04 tmp]# sh 60.sh -100 -10
  2. -100 < -10
  3. [root@centos-04 tmp]# sh 60.sh 100 10
  4. 100 > 10
  5. [root@centos-04 tmp]#

  

  

  

  

  

  

  

Shell习题100例的更多相关文章

  1. Shell习题100例(2)

    找文件差异 grep -f 选项可以匹配到文件a在文件b中所有相关的行(取a中有b中有的) [root@centos-04 tmp]# vim b.txt vvvv root [root@centos ...

  2. linux shell习题训练

    shell习题训练 求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2 ...

  3. C语言程序设计100例之(14):丑数

    例14   丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...

  4. shell习题训练

    shell习题训练 求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2 ...

  5. Python语言100例

    Python版本:python 3.2.2 电脑系统:win7旗舰 实例来源:python菜鸟教程100例 #!/usr/bin/python # -*- coding: UTF-8 -*- impo ...

  6. C语言经典算法100例

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. ...

  7. 实用ExtJS教程100例-001:开天辟地的Hello World

    ExtJS功能繁多,要想彻底的了解确实很困难.作为初学者,如何能找到一条快速的通道呢?我觉得,如果你有Javascript的基础,那就不要惧怕ExtJS的复杂,从动手开始,遇到问题,解决问题,积累经验 ...

  8. C 语言经典100例

    C 语言经典100例 C 语言练习实例1 C 语言练习实例2 C 语言练习实例3 C 语言练习实例4 C 语言练习实例5 C 语言练习实例6 C 语言练习实例7 C 语言练习实例8 C 语言练习实例9 ...

  9. Xena L23网络测试仪Valkyrie使用技巧100例,目录

    Xena L23网络测试仪Valkyrie使用技巧100例,先写个目录 100例,会不会有点多,写不完... ^_^ 第1次编辑于2019-11-27 22:05:52, Evan YE. 编号 标题 ...

随机推荐

  1. VB.NET与C# 语法区别展示

    在学习VB.NET后发现,VB.NET与C#的语法主要的不同在两个部分,这两部分搞通了,那就游刃有余,迎刃而解了.现将其对比总结如下: 一.实体部分 (与VB相比,在C#和VB.NET中,实体的使用很 ...

  2. LigerUI之Grid使用详解(三)——字典数据展示

    一.问题概述 在开发web信息管理系统时,使用Web前端框架可以帮助我们快速搭建一组风格统一的界面效果,而且能够解决大多数浏览器兼容问题,提升开发效率.在关于LigerGrid的前两篇的内容里,给大家 ...

  3. 【Word】Word 2010 设置边框底纹,粘贴漂亮的代码

    参考资料: http://jingyan.baidu.com/article/48206aea1a3401216bd6b310.html http://wenku.baidu.com/link?url ...

  4. Style 的查找 FindResource

    1)根据名称查找 PrintPreview fe = new PrintPreview(new Summary()); string strResourceHeader = "headerS ...

  5. word图片自动编号与引用(转)

    http://blog.csdn.net/hunauchenym/article/details/5915616 用Word时,可能会遇到文中使用了大量的图片的情况,这时,若采用手动为图片编号的方法, ...

  6. Linux kernel模块管理相关详解

    Linux内核模块化设计 1. Linux内核设计:单内核.模块化(动态装载和卸载) (1) Linux:单内核设计,但充分借鉴了微内核体系的设计的优点:为内核引入了模块化机制:(2) 内核的组成部分 ...

  7. 菜鸟运维笔记:小记编译安装Nginx所遇到的坑

    转载请注明出处:http://blog.csdn.net/guodongxiaren/article/details/40950249 谢谢合作 前言 无论是CentOS,或是Debian/Ubunt ...

  8. swift语言实现单例模式

    Swift实现单例模式 单例模式在各个语言中都有实现,swift语言推出已经几天了.通过这几天的看文档.特奉上写的Swift的单例实现,供大家学习交流,欢迎指正. ---若转载请注明出处,本人Gith ...

  9. Eclipse中SVN修改的*星号没了,解决方法

    Eclipse中SVN修改的*星号没了,解决方法 打开Preference 第一步:去掉外加的 ">" 第二步:勾选Outgoing changes 这样做之后," ...

  10. 【IPC进程间通讯之二】管道Pipe

    IPC进程间通信+管道Pipe                IPC(Inter-Process Communication.进程间通信).         管道用于进程间共享数据,事实上质是共享内存 ...