设为首页 收藏本站
查看: 820|回复: 0

linux 基础 shell脚本命令

[复制链接]

尚未签到

发表于 2018-8-27 08:49:40 | 显示全部楼层 |阅读模式
  #########shell脚本命令####
  1.diff
  diff      file file1             ####比较两个文件的不同
  -c                         ####显示周围的行
  -u                         ####按照一格式统一输出生成补丁
  -r                         ####比较两个文件的不同
  patch       file file.path       ####打补丁
  -b                         ####备份原文件
  ot@desktop28 mnt]# diff westos westos.new -c    ###显示周围行
  *** westos2016-11-22 04:04:37.782657053 -0500
  --- westos.new2016-11-22 04:05:02.900657053 -0500
  ***************
  *** 1 ****
  --- 1,4 ----
  + file
  westos
  + linux
  +
  [root@desktop28 mnt]# diff westos westos.new -u    ###显示详细情况
  --- westos2016-11-22 04:04:37.782657053 -0500
  +++ westos.new2016-11-22 04:05:02.900657053 -0500
  @@ -1 +1,4 @@
  +file
  westos
  +linux
  +
  [root@desktop28 mnt]# diff -u westos westos.new  > westos.path    ###生成补丁
  [root@desktop28 mnt]# ll
  total 12
  -rw-r--r--. 1 root root   7 Nov 22 04:04 westos
  -rw-r--r--. 1 root root  19 Nov 22 04:05 westos.new
  -rw-r--r--. 1 root root 135 Nov 22 04:07 westos.path
  [root@desktop28 mnt]# yum install patch -y    ###安装打补丁软件
  Loaded plugins: langpacks
  Package patch-2.7.1-8.el7.x86_64 already installed and latest version
  Nothing to do
  [root@desktop28 mnt]# patch westos westos.path    ###打补丁
  patching file westos
  [root@desktop28 mnt]# ll
  total 12
  -rw-r--r--. 1 root root  18 Nov 22 04:18 westos
  -rw-r--r--. 1 root root  18 Nov 22 04:15 westos.new
  -rw-r--r--. 1 root root 133 Nov 22 04:16 westos.path
  [root@desktop28 mnt]# vim westos
  [root@desktop28 mnt]# patch -b westos westos.new
  patch: **** Only garbage was found in the patch input.
  [root@desktop28 mnt]# patch -b westos westos.path
  patching file westos
  [root@desktop28 mnt]# ll
  total 16
  -rw-r--r--. 1 root root  18 Nov 22 04:19 westos
  -rw-r--r--. 1 root root  18 Nov 22 04:15 westos.new
  -rw-r--r--. 1 root root   7 Nov 22 04:18 westos.orig
  -rw-r--r--. 1 root root 133 Nov 22 04:16 westos.path
  2.grep
  grep 关键字符         文件|目录    ###在文件或目录中查找含有关键字的行
  grep -i            ####忽略大小写
  -n            ###显示关键字所在的行
  -c            ###显示过滤结果的行数
  -v            ###反向过滤
  -E “关键字1|关键字2”              ###过滤多个关键字
  -r  目录                            ###在目录中查找含有关键字的文件
  注意:^关键字                            ###以关键字开头
  关键字$                            ###以关键字结尾
  [root@localhost mnt]# grep root passwd    ###找出passwd文件中含有root的行
  root:x:0:0:root:/root:/bin/bash
  operator:x:11:0:operator:/root:/sbin/nologin
  test:root:test
  root:test:root
  test:ROOT:root
  [root@localhost mnt]# grep ^root passwd        ##找出passwd文件中以root开头的行
  root:x:0:0:root:/root:/bin/bash
  root:test:root
  [root@localhost mnt]# grep root$ passwd        ##找出passwd文件中以root结尾的行
  root:test:root
  test:ROOT:root
  [root@localhost mnt]# grep -i root passwd        ##忽略大小写
  root:x:0:0:root:/root:/bin/bash
  operator:x:11:0:operator:/root:/sbin/nologin
  test:root:test
  root:test:root
  test:ROOT:root
  [root@localhost mnt]# grep -n root passwd        ###找出关键字所在的行数
  1:root:x:0:0:root:/root:/bin/bash
  10:operator:x:11:0:operator:/root:/sbin/nologin
  15:test:root:test
  16:root:test:root
  17:test:ROOT:root
  [root@localhost mnt]# grep -v root$ passwd        ##找出不以root结尾的行
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
  adm:x:3:4:adm:/var/adm:/sbin/nologin
  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  sync:x:5:0:sync:/sbin:/bin/sync
  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  halt:x:7:0:halt:/sbin:/sbin/halt
  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  operator:x:11:0:operator:/root:/sbin/nologin
  games:x:12:100:games:/usr/games:/sbin/nologin
  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  nobody:x:99:99:Nobody:/:/sbin/nologin
  dbus:x:81:81:System message bus:/:/sbin/nologin
  test:root:test
  [root@localhost mnt]# grep -i root passwd | grep -E "^root|root$"        ##找出以root开头或结尾,不分大小写的行
  root:x:0:0:root:/root:/bin/bash
  root:test:root
  test:ROOT:root
  [root@localhost mnt]# grep -i root passwd | grep -E "^root|root$" -v      ##找出不以root开头或结尾的行
  operator:x:11:0:operator:/root:/sbin/nologin
  test:root:test
  [root@localhost mnt]# grep root -r /etc/ -n                                ###找出/etc/目录下含有root的文件并显示所在行数
  /etc/pki/ca-trust/extracted/README:6:root CA certificates.
  /etc/pki/ca-trust/extracted/java/README:11:root CA certificates.
  Binary file /etc/pki/ca-trust/extracted/java/cacerts matches
  /etc/pki/ca-trust/extracted/openssl/README:12:root CA certificates.
  /etc/pki/ca-trust/extracted/pem/README:15:root CA certificates.
  /etc/pki/tls/certs/make-dummy-cert:11:echo root@localhost.localdomain
  /etc/pki/tls/openssl.cnf:332:dir= ./demoCA# TSA root directory
  ###grep正则表达式###
  3.cut
  cut                                     ###截取字符
  cut -d 分隔符                         ###指定分隔符
  cut -c 1-4                            ###显示指定的字符
  cut -f 1,5                            ###显示指定的列
  [root@localhost mnt]# cat passwd
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
  adm:x:3:4:adm:/var/adm:/sbin/nologin
  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  sync:x:5:0:sync:/sbin:/bin/sync
  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  halt:x:7:0:halt:/sbin:/sbin/halt
  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  operator:x:11:0:operator:/root:/sbin/nologin
  games:x:12:100:games:/usr/games:/sbin/nologin
  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  nobody:x:99:99:Nobody:/:/sbin/nologin
  dbus:x:81:81:System message bus:/:/sbin/nologin
  test:root:test
  root:test:root
  test:ROOT:root
  [root@localhost mnt]# cut -d ":" -f 1,3 passwd        ##截取第一,三列
  root:0
  bin:1
  daemon:2
  adm:3
  lp:4
  sync:5
  shutdown:6
  halt:7
  mail:8
  operator:11
  games:12
  ftp:14
  nobody:99
  dbus:81
  test:test
  root:root
  test:root
  [root@localhost mnt]# cut -d ":" -f 1-3 passwd        ##截取1到3列
  root:x:0
  bin:x:1
  daemon:x:2
  adm:x:3
  lp:x:4
  sync:x:5
  shutdown:x:6
  halt:x:7
  mail:x:8
  operator:x:11
  games:x:12
  ftp:x:14
  nobody:x:99
  dbus:x:81
  test:root:test
  root:test:root
  test:ROOT:root
  [root@localhost mnt]# cut -c 2-5 passwd        ##截取第2到第5个字符的列
  oot:
  in:x
  aemo
  dm:x
  p:x:
  ync:
  hutd
  alt:
  ail:
  pera
  ames
  tp:x
  obod
  bus:
  est:
  oot:
  est:
  [root@localhost mnt]# cut -c 2,5 passwd        ##截取第2,5个字符所在的列
  o:
  ix
  ao
  dx
  p:
  y:
  hd
  a:
  a:
  pa
  as
  tx
  od
  b:
  e:
  o:
  e:
  例子:
  用命令, 只显示出eth0的ip.
  [root@localhost mnt]# ifconfig eth0
  eth0: flags=4163  mtu 1500
  inet 172.25.28.11  netmask 255.255.255.0  broadcast 172.25.28.255
  inet6 fe80::5054:ff:fe00:1c0b  prefixlen 64  scopeid 0x20
  ether 52:54:00:00:1c:0b  txqueuelen 1000  (Ethernet)
  RX packets 1899  bytes 165993 (162.1 KiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 1244  bytes 292701 (285.8 KiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  [root@localhost mnt]# ifconfig eth0 | grep inet |grep inet6 -v| cut -d " " -f 10
  172.25.28.11
  或
  [root@localhost mnt]# ifconfig eth0 | grep inet |grep inet6 -v| awk -F " " '{print $2}'
  172.25.28.11
  #####sort#######
  sort                        ####排序
  -n                   ####纯数字排序
  -u                    ####去冗余
  -|uniq -c            ####去冗余并统计冗余次数
  -t                    ####值定分隔符
  -k                    ####指定列
  [root@localhost mnt]# cat westos
  1
  1
  2
  2
  4
  5
  2
  65
  2
  12
  43
  [root@localhost mnt]# sort westos ·###排序
  1
  1
  12
  2
  2
  2
  2
  4
  43
  5
  65
  [root@localhost mnt]# sort -n westos     ###纯数字排序
  1
  1
  2
  2
  2
  2
  4
  5
  12
  43
  65
  [root@localhost mnt]# sort -nr westos     ###纯数字排倒序
  65
  43
  12
  5
  4
  2
  2
  2
  2
  1
  1
  [root@localhost mnt]# sort -nru westos     ###纯数字排倒序去冗余
  65
  43
  12
  5
  4
  2
  1
  [root@localhost mnt]# cat westos
  0:1
  3:1
  2:2
  5:2
  2:4
  7:5
  a:2
  e:65
  v:2
  4:12
  2:43
  [root@localhost mnt]# sort -n -t : -k 2 westos        ###第二列按纯数字顺序排列
  0:1
  3:1
  2:2
  5:2
  a:2
  v:2
  2:4
  7:5
  4:12
  2:43
  e:65
  ####uniq#####
  sort file |uniq -c            #####去冗余并统计冗余次数
  -d                     #####显示冗余行
  -u                    #####显示唯一行
  [root@localhost mnt]# sort -n westos |uniq -c        ###去冗余行并统计次数
  2 1
  4 2
  1 4
  1 5
  1 12
  1 43
  1 65
  [root@localhost mnt]# sort -n westos |uniq -u        ####显示唯一行
  4
  5
  12
  43
  65
  [root@localhost mnt]# sort -n westos |uniq -d        ####显示冗余行
  1
  2
  ####sed#####
  sed 's/原字符/替换字符/g' file                   ####替换字符
  sed -e '策略一' -e '策略二' file                 ####替换多种字符
  sed -i file                                      ####把转换后的内容输入到指定文件
  sed '3,5s/原字符/替换字符/g'                     ####3-5行替换
  sed xd                                           ####屏蔽指定行
  sed xp                                           ####复制指定行
  sed -n xp                                        ####值显示指定行
  [root@localhost mnt]# cat passwd
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
  adm:x:3:4:adm:/var/adm:/sbin/nologin
  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  sync:x:5:0:sync:/sbin:/bin/sync
  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  [root@localhost mnt]# sed 's/sbin/hello/g' passwd        ### 替换sbin为hello
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/hello/nologin
  daemon:x:2:2:daemon:/hello:/hello/nologin
  adm:x:3:4:adm:/var/adm:/hello/nologin
  lp:x:4:7:lp:/var/spool/lpd:/hello/nologin
  sync:x:5:0:sync:/hello:/bin/sync
  [root@localhost mnt]# cat passwd
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
  adm:x:3:4:adm:/var/adm:/sbin/nologin
  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  sync:x:5:0:sync:/sbin:/bin/sync
  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  [root@localhost mnt]# sed -e 's/sbin/hello/g' -e 's/nologin/westos/g'  passwd     ####替换sbin为hello,nologin为westos
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/hello/westos
  daemon:x:2:2:daemon:/hello:/hello/westos
  adm:x:3:4:adm:/var/adm:/hello/westos
  lp:x:4:7:lp:/var/spool/lpd:/hello/westos
  sync:x:5:0:sync:/hello:/bin/sync
  shutdown:x:6:0:shutdown:/hello:/hello/shutdown
  [root@localhost mnt]# cat passwd                                             ####(passd文件内容并没改变)
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
  adm:x:3:4:adm:/var/adm:/sbin/nologin
  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  sync:x:5:0:sync:/sbin:/bin/sync
  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  [root@localhost mnt]# sed -e 's/sbin/hello/g' -e 's/nologin/westos/g'  -i passwd         #####替换后保存内容到passwd
  [root@localhost mnt]# cat passwd
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/hello/westos
  daemon:x:2:2:daemon:/hello:/hello/westos
  adm:x:3:4:adm:/var/adm:/hello/westos
  lp:x:4:7:lp:/var/spool/lpd:/hello/westos
  sync:x:5:0:sync:/hello:/bin/sync
  shutdown:x:6:0:shutdown:/hello:/hello/shutdown
  ######awk#####
  awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析生成报告时,显得有为强大。
  使用方法:awk '{pattern + action}' {filename}
  [root@localhost mnt]# last -n 5
  root     pts/0        172.25.28.250    Wed Nov 23 05:32   still logged in
  root     pts/0        :0               Wed Nov 23 05:32 - 05:32  (00:00)
  root     :0           :0               Wed Nov 23 05:32   still logged in
  (unknown :0           :0               Wed Nov 23 05:31 - 05:32  (00:00)
  reboot   system boot  3.10.0-123.el7.x Wed Nov 23 05:31 - 07:29  (01:57)
  wtmp begins Thu Jul 10 18:18:02 2014
  [root@localhost mnt]# last -n 5 | awk '{print $1}'                ###值显示最近登陆的5个帐号($1表示第一个域,默认的域分隔符时空格键或tab键)
  root
  root
  root
  (unknown
  reboot
  [root@localhost mnt]# cat /etc/passwd | awk -F ':' '{print $1}'            ###-F指定分隔符为‘:’
  root
  bin
  daemon
  adm
  lp
  sync
  shutdown
  halt
  mail
  [root@localhost mnt]# cat /etc/passwd | awk -F ':' '{print $1 "\t" $7}'            ###显示/etc/passwd账户及对应的shell,中间用tab键隔开
  root/bin/bash
  bin/sbin/nologin
  daemon/sbin/nologin
  adm/sbin/nologin
  lp/sbin/nologin
  [root@localhost mnt]# cat /etc/passwd | awk -F ':' 'BEGIN {print "name shell"} {print $1 "," $7} END {print "blue,bin/nosh"}'
  name shell
  root,/bin/bash
  bin,/sbin/nologin
  daemon,/sbin/nologin
  adm,/sbin/nologin
  . . .
  student2,/bin/bash
  student3,/bin/bash
  blue,bin/nosh
  [root@localhost ~]# awk -F: '/root/' /etc/passwd             ###搜索/etc/passwd有root关键字的行
  root:x:0:0:root:/root:/bin/bash
  operator:x:11:0:operator:/root:/sbin/nologin
  [root@localhost ~]# awk -F: '/^root/' /etc/passwd             ###搜索开头是关键字root的行
  root:x:0:0:root:/root:/bin/bash
  [root@localhost ~]# awk -F: '/root$/' /etc/passwd


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-557064-1-1.html 上篇帖子: 如何在交互式shell脚本中创建对话框 下篇帖子: shell脚本wget crul监控某网站是否正常
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表