shell 脚本1

shell脚本
编辑shell脚本一般以.sh结尾
[root@foundation77 ~]# cd /ost
[root@foundation77 ost]# ls
[root@foundation77 ost]# vim westos.sh  编辑一个shell脚本
 1 #!/bin/bash   (幻数    shell脚本开头必须要有)

 2 echo hello westos     保存退出 


查看脚本有两种方式
1.[root@foundation77 ost]# sh westos.sh 
hello westos


2.[root@foundation77 ost]# chmod +x westos.sh   添加一个可执行权限
[root@foundation77 ost]# /mnt/westos.sh   
hello westos
以上两种方式都可以看到shell脚本执行的结果
如果要看他的命令及结果,也有两种方式
1.[root@foundation77 ost]# sh -x /mnt/westos.sh 
+ echo hello westos     (执行的命令)
hello westos            (执行的结果)


2.[root@foundation77 ost]# vim westos.sh
 1 #!/bin/bash -x    (最后加入-x)
 2 echo hello westos    保存退出
[root@foundation77 ost]# /mnt/westos.sh
+ echo hello westos
hello westos                     (此时可以查看到命令及结果)


一般来说,脚本需要有作者,说明及创建时间等,如果每次编写则太过麻烦,可以写在系统文件中自动读取  有两种编辑方式
1.(第一种方法)
[root@foundation77 ost]# vim /etc/vimrc  编辑此文件
 67 map <F4> ms:call WESTOS()<cr>'s    (编辑文件按F4自动执行WESTOS命令)
 68 function WESTOS()                   (68及以下为命令内容)
 69         call append(0,"#####################################")
 70         call append(1,"# Author :      lee                 #")
 71         call append(2,"# Mail :        lee@westos.com      #")
 72         call append(3,"# Version :     1.0                 #")
 73         call append(4,"# Create_Time :   ".strftime("%Y-%m-%d")."        #")
 74         call append(5,"# Description :                     #")
 75         call append(6,"#                                   #")
 76         call append(7,"#                                   #")
 77         call append(8,"#####################################")
 78         call append(9,"")
 79         call append(10,"#!/bin/bash")
 80 endfunction

编辑完成后保存退出




[root@foundation77 ost]# vim westos.sh    编辑此文件
  1 #####################################
  2 # Author :      lee                 #
  3 # Mail :        lee@westos.com      #
  4 # Version :     1.0                 #
  5 # Create_Time :   2018-05-09        #
  6 # Description :                     #
  7 #                                   #
  8 #                                   #
  9 #####################################
 10 
 11 #!/bin/bash
 12 #!/bin/bash -x
 13 echo hello westos               按F4后会出现刚才编辑的表头

2.第二种方法
[root@foundation77 ost]# vim /etc/vimrc
 67 autocmd BufNewFile *.sh exec ":call WESTOS()"    (当编辑一个新的以.sh结尾的shell时,会出现WESTOS表头)
 68 "map <F4> ms:call WESTOS()<cr>'s           (使用"注释掉本行命令)
 69 function WESTOS()
 70         call append(0,"#####################################")
 71         call append(1,"# Author :      lee                 #")
 72         call append(2,"# Mail :        lee@westos.com      #")
 73         call append(3,"# Version :     1.0                 #")
 74         call append(4,"# Create_Time :   ".strftime("%Y-%m-%d")."        #")
 75         call append(5,"# Description :                     #")
 76         call append(6,"#                                   #")
 77         call append(7,"#                                   #")
 78         call append(8,"#####################################")
 79         call append(9,"")
 80         call append(10,"#!/bin/bash")
 81 endfunction                                         保存退出
[root@foundation77 ost]# vim westos.sh
  1 #####################################
  2 # Author :      lee                 #
  3 # Mail :        lee@westos.com      #
  4 # Version :     1.0                 #
  5 # Create_Time :   2018-05-09        #
  6 # Description :                     #
  7 #                                   #
  8 #                                   #
  9 #####################################
 10 
 11 #!/bin/bash
此时不需要按F4  会自动弹出表头    (必须编辑新的以.sh结尾的shell才可以,编辑以前存在的则不会出现)


执行ip_show.sh  显示当前主机的ip地址
[root@foundation77 ost]# vim ip_show.sh
12 ifconfig br0 | awk -F " " '/inet\>/{print $2}'  加入此行   保存退出
[root@foundation77 ost]# chmod +x ip_show.sh 
[root@foundation77 ost]# sh ip_show.sh 
172.25.22.77



执行user_show.sh   显示当前主机中能登陆系统的用户
[root@foundation77 ost]# vim user_show.sh
12 awk -F : '/bash$/{print $1}' /etc/passwd       加入此行  保存退出
[root@foundation77 ost]# chmod +x user_show.sh 
[root@foundation77 ost]# /mnt/user_show.sh 
root
kiosk



shell中的命令
[root@foundation77 ost]# vim westos
[root@foundation77 ost]# cat westos
123
[root@foundation77 ost]# vim westos1
[root@foundation77 ost]# cat westos1
123

456




编辑两个文件,查看他们的不同,使用diff
[root@foundation77 ost]# diff westos westos1
1a2                    (此行表示第一个文件的第二行中添加下面第二个文件中多出来的内容)
> 456                  (第二个文件中多出来的内容)
diff在比较文件过程中的结果读取方式
[num1,num2][a|c|d][num3,num4]
num1,num2  表示在第一个文件中的行数
a表示添加-----add       c表示更改-------change      d表示删除-----delete
<表示第一个文件中的内容,>表示第二个文件中的内容
num3,num4表示在第二个文件中的行数
比较出不同可以用来做补丁
[root@foundation77 ost]# diff -u westos westos1 >file.path   把两个不同导入到file.path文件
下载patch服务
[root@foundation77 ost]# yum install patch -y
[root@foundation77 ost]# patch westos file.path   添加补丁
patching file westos
[root@foundation77 ost]# cat westos
12
[root@foundation77 ost]# cat westos1
12                                      此时两个文件一样
也可以备份原文件再添加补丁
[root@foundation77 ost]# patch -b westos file.path 
patching file westos                                    备份原westos文件
[root@foundation77 ost]# ls
file.path  westos  westos1  westos.orig (备份文件)



cut
cut命令用于字符截取
cut -d    指定分隔符    cut -f 1,7|1-7   指定截取的列   cut -c 1,4|1-4  指定截取的字符位置
[root@foundation77 ost]# vim user_show.sh
grep bash /etc/passwd |cut -d : -f 1    指定分隔符为“:”   截取的列为第一列
[root@desktop22 mnt]# chmod +x user_show.sh 
[root@foundation77 ost]# /ost/user_show.sh 
root

kiosk          此时可以看到登陆系统的用户



[root@foundation77 ost]# vim ip_show.sh 
12 ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10  保存退出
[root@foundation77 ost]# chmod +x ip_show.sh
[root@foundation77 ost]# /ost/ip_show.sh 
172.25.22.10                                    此时可以看到ip


sort   多用于字符排列
sort -n 纯数字排列
sort -r 倒叙
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
[root@foundation77 ost]# ll
total 32
-rw-r--r--. 1 root root 119 Jun 14 10:44 file.path
-rwxr-xr-x. 1 root root 401 Jun 14 09:58 ip_show.sh
-rw-r--r--. 1 root root 411 Jun 14 11:16 ips_show.sh
-rwxr-xr-x. 1 root root 396 Jun 14 11:00 user_show.sh
-rwxr-xr-x. 1 root root 392 Jun 14 11:02 usr_show.sh
-rw-r--r--. 1 root root   4 Jun 14 10:15 westos
-rw-r--r--. 1 root root   8 Jun 14 10:15 westos1
-rw-r--r--. 1 root root 376 Jun 14 09:33 westos.sh


当前目录中的文件,按从大到小排序
[root@foundation77 ost]# ls -l /mnt | sort -t " " -k 5 -rn
-rw-r--r--. 1 root root 266 May 16 08:32 useradd.sh
-rwxr-xr-x. 1 root root 223 Apr 21 15:43 vm_create.sh
-rw-r--r--. 1 root root  18 May 16 08:18 userfile
-rw-r--r--. 1 root root  12 May 16 08:18 passwdfile
total 16


先列出文件做为后面的输出项,-t指定分隔符为“空格” 然后指定要排序的列为第五列,然后倒序排出  


uniq  对重复字符做相应的处理     (搭配sort使用)
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数    
[root@foundation77 ost]# cat westos3
1
2
4
8
6
5
9
6
7
3
4
7
8
5
11


[root@foundation77 ost]# sort -n westos3 | uniq -d   显示重复的行排列
4
5
6
7
8


[root@foundation77 ost]# sort -n westos3 | uniq -c 每行显示一次并统计重复次数
      1 1
      1 2
      1 3
      2 4
      2 5
      2 6
      2 7
      2 8
      1 9
      1 11


[root@foundation77 ost]# sort -n westos3 | uniq -u   显示唯一的行并排序
1
2
3
9
11




&&和||
&&用来执行条件成立后执行的命令  ||用来执行条件不成立后执行的命令
[root@foundation77 ost]# vim check_ip.sh
12 ping -c1 -w1 $1 &>/dev/null && echo $1 is up || echo $1 is down  
ping通则显示up    否则显示down
[root@foundation77 ost]# chmod +x check_ip.sh
[root@foundation77 ost]# /ost/check_ip.sh 172.25.254.250
172.25.254.250 is up
[root@foundation77 ost]# /ost/check_ip.sh 172.25.22.40
172.25.254.40 is down

$1:表示输入的所有命令



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值