一、脚本概述
1.Shell脚本是一种解释型语言
2.用Shell脚本保存执行命令
3.用脚本判定命令的执行条件
4.用脚本实现命令的批量执行
二、脚步基础知识
1.脚本编写
#!/bin/bash
echo hello westos
~
~
~
2.脚本运行
sh hello.sh ## 用sh运行脚本
chmod +x hello.sh ## 给”hello.sh”添加执行权限
/mnt/hello.sh ## 用脚本指定的shell打开
[root@desktop51 mnt]# vim hello.sh
[root@desktop51 mnt]# cat hello.sh
#!/bin/bash
echo hello westos
[root@desktop51 mnt]# sh hello.sh
hello westos
[root@desktop51 mnt]# chmod +x hello.sh
[root@desktop51 mnt]# /mnt/hello.sh
hello westos
3.脚本调试
[root@desktop51 mnt]# sh -x hello.sh
+ echo hello westos ## 命令行
hello westos ## 运行结果
4.自动添加脚本信息说明 ##配置文件/etc/vimrc
autocmd BufNewFile *.sh exec ":call WESTOS( )"
##新建的以.sh结尾的文件自动添加函数WESTOS
"map <F4> ms:call WESTOS()<CR>'s
##按F4自动添加函数WESTOS,“ 表示注释
67 autocmd BufNewFile *.sh exec ":call WESTOS( )"
68
69 "map <F4> ms:call WESTOS()<CR>'s
70 function WESTOS()
71 call append(0,"#####################################")
72 call append(1,"# Autor : lixuan #")
73 call append(2,"# Mail : [email protected] #")
74 call append(3,"# Version : 1.0 #")
75 call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
76 call append(5,"# Description : #")
77 call append(6,"# #")
78 call append(7,"# #")
79 call append(8,"#####################################")
80 call append(9,"")
建立新的脚本
[root@desktop251 mnt]# vim westos.sh
[root@desktop251 mnt]# cat westos.sh
#####################################
# Autor : lixuan #
# Mail : [email protected] #
# Version : 1.0 #
# Create_Time : 2018-05-12 #
# Description : #
# #
# #
#####################################
#!bin/bash
三、脚本练习
练习1:显示当前主机ip地址
[root@desktop251 mnt]# vim ip_show.sh
[root@desktop251 mnt]# /mnt/ip_show.sh
172.25.254.251
[root@desktop251 mnt]# cat ip_show.sh
#####################################
# Autor : lixuan #
# Mail : [email protected] #
# Version : 1.0 #
# Create_Time : 2018-05-11 #
# Description : #
# #
# #
#####################################
#!bin/bash
ifconfig eth0 | awk -F " " '/inet /{print $2}'
练习2:显示当前主机中能登陆系统的用户
[root@desktop251 mnt]# vim user_show.sh
[root@desktop251 mnt]# chmod +x user_show.sh
[root@desktop251 mnt]# /mnt/user_show.sh
root
student
[root@desktop251 mnt]# cat user_show.sh
#####################################
# Autor : lixuan #
# Mail : [email protected] #
# Version : 1.0 #
# Create_Time : 2018-05-12 #
# Description : #
# #
# #
#####################################
#!bin/bash
awk -F : '/bash$/{print $1}' /etc/passwd
练习3:执行命令后可以清空日志
[root@desktop251 mnt]# vim clean_log.sh
[root@desktop251 mnt]# chmod +x clean_log.sh
[root@desktop251 mnt]# /mnt/clean_log.sh
[root@desktop251 mnt]# cat /var/log/message
[root@desktop251 mnt]# cat clean_log.sh
#####################################
# Autor : lixuan #
# Mail : [email protected] #
# Version : 1.0 #
# Create_Time : 2018-05-12 #
# Description : #
# #
# #
#####################################
#!bin/bash
>/var/log/message
或者:
[root@desktop251 mnt]# vim clean_log.sh
[root@desktop251 mnt]# chmod +x clean_log.sh
[root@desktop251 mnt]# /mnt/clean_log.sh
[root@desktop251 mnt]# sh -x clean_log.sh
+ echo ' '
[root@desktop251 mnt]# cat /mnt/clean_log.sh
#####################################
# Autor : lixuan #
# Mail : [email protected] #
# Version : 1.0 #
# Create_Time : 2018-05-12 #
# Description : #
# #
# #
#####################################
#!bin/bash
echo " ">/var/log/message
四、基本脚本命令
diff
##比较文件差异
1.对比两个文件的不同
[root@desktop251 mnt]# vim westos1
[root@desktop251 mnt]# cat westos
123
[root@desktop251 mnt]# cat westos1
123
456
[root@desktop251 mnt]# diff westos westos1
1a2
> 456
2.生成补丁
[root@desktop251 mnt]# diff westos westos1 -u > westos.path
[root@desktop251 mnt]# cat westos.path
--- westos 2018-05-12 03:19:17.547374795 -0400
+++ westos1 2018-05-12 03:18:28.843374795 -0400
@@ -1 +1,2 @@
123
+456
patch
1.安装补丁软件 patch.x86_64
[root@desktop151 ~]# yum install patch
patchutils.x86_64 patch.x86_64
[root@desktop151 ~]# yum install patch.x86_64
Loaded plugins: langpacks
file:///mnt/rhel7.0/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/rhel7.0/repodata/repomd.xml"
Trying other mirror.
Resolving Dependencies
--> Running transaction check
---> Package patch.x86_64 0:2.7.1-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
patch x86_64 2.7.1-8.el7 rhel_dvd 110 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 110 k
Installed size: 210 k
Is this ok [y/d/N]: y
Downloading packages:
Error downloading packages:
patch-2.7.1-8.el7.x86_64: [Errno 256] No more mirrors to try.
2.打补丁