1.什么是shell
脚本中命令的解释器
2.shell脚本的意义
1.记录命令执行的过程和执行逻辑,以便以后重复执行
2.脚本可以批量处理主机
3.脚本可以定时处理主机
3.如何创建shell脚本
#!/bin/bash ##幻数
![](https://img-blog.csdnimg.cn/img_convert/c245ea2de5dfbbbd5efbcfc53b501328.png)
vim自动添加脚本首部
"map <F4> ms:call WESTOSTITLE()<cr>'s
autocmd BufNewFile *.sh,*.script call WESTOSTITLE()
func WESTOSTITLE()
call append(0,"###############################################")
call append(1,"# Author: lee")
call append(2,"# Version: ")
call append(3,"# Create_Time: ".strftime("%Y/%m/%d"))
call append(4,"# Mail: lee@westos.org")
call append(5,"# Info: ")
call append(6,"# ")
call append(7,"################################################")
call append(8,"")
call append(9,"#!/bin/bash")
endfunc
![](https://img-blog.csdnimg.cn/img_convert/dc4106afb2899cb8fefaad57b0c84876.png)
![](https://img-blog.csdnimg.cn/img_convert/c3ac314f78f101c899e8b14546b1851e.png)
按下F4后出现我们需要添加的内容
![](https://img-blog.csdnimg.cn/img_convert/e500371883f01177dd705b77eb17dfcb.png)
此时只会针对新文件添加头部
4.如何执行shell脚本
1).手动在环境中开启指定解释器
sh script.sh
2).直接在当前环境中运行shell中的指令不开启新的shell
source script.sh
. script.sh
3).开启脚本中指定的shell并使用此shell环境运行脚本中的指令
chmod +x script.sh
/xxx/xxx/script.sh
./script.sh
5.如何对脚本进行调试
sh -x /mnt/westos.sh
+ ##运行指令
不带+ ##命令运行的输出
![](https://img-blog.csdnimg.cn/img_convert/fb65eeb4fb0dd7ccaaa8c5c68102293d.png)
脚本练习:
1
ip_show.sh 网卡 显示当前的IP
![](https://img-blog.csdnimg.cn/img_convert/181b419878b525773a476479bd483ab8.png)
![](https://img-blog.csdnimg.cn/img_convert/8d4496af01465a8479edfa11a2674023.png)
2
host_messages.sh 显示当前主机的名称,ip登陆当前主机的用户
hostname: xxxxx
ipaddress: xxxx.xxxx.xxx.xxx
username: root
![](https://img-blog.csdnimg.cn/img_convert/7848142d9eae9a48b40b02d4925fb911.png)
![](https://img-blog.csdnimg.cn/img_convert/20203fc41c2efcccc6d0d4705c897528.png)
3.
clear_log.sh 执行次脚本后可以清空日志
![](https://img-blog.csdnimg.cn/img_convert/4d4707c8e8a3a7d250a155452e7874c6.png)
![](https://img-blog.csdnimg.cn/img_convert/60606c0346322c9c5b56a8a9acfacba5.png)