1.shell脚本的意义
##脚本中命令的解释器
#1 记录命令执行过程和执行逻辑,以便以后重复执行
#2 脚本可以批量处理主机
#3 脚本可以定时处理主机
2.如何创建shell脚本
#1
vim 文件名 ----> 文件名以.sh结尾
写入:
#2
#! /bin/bash ##幻数
作用: 定义shell脚本最先执行的程序和调用的程序
3.利用vim实现脚本规范化输入
/etc/vimrc shell脚本主配置文件 ----> 对所有用户生效
~/.vimrc shell脚本家目录 ---->只对自己生效
set nu ts=2 sw=2
"map <F4> ms:call SHELLTATLE()<cr>'s
autocmd BufNewFile *.sh call SHELLTATLE()
func SHELLTATLE()
call append(0,"############")
call append(1,"Author" )
call append(2," Create_Time: ".strftime("%Y-%m-%d"))
call append(3,"############")
call append(4,"#! /bin/bash")
endfunc
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 必须在所写shell目录中执行
5.shell脚本的调试
sh -x /mnt/westos.sh ##调试脚本
+ ##运行指令
不带+ ##命令运行的输出