1 什么是shell
shell 也是操作系统中的一个软件,它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口,系统中的命令用 shell 去解释 shell 接收系统回应的输出并显示其到屏幕中
2. shell脚本存在的意义
脚本是一种解释型语言
用 shell 脚本保存执行动作
用脚本判定命令的执行条件
用脚本来实现动作的批量执行
3.脚本的创建
一般创建脚本时,需要注明创建的信息——创建者、版本(手动填写)、邮箱、时间、描述(手动填写)
!/bin/bash ## 幻数
文件不以. sh 结尾时,编辑语言没有语法高亮
[root@shell mnt]# vim /etc/vimrc
64 let &guicursor = &guicursor . ",a:blinkon0"
65 set nu
66
67
68
69
70 map <F4> ms:call WESTOS()<cr>'s 按F4添加以下内容
71 func WESTOS ()
72 call append(0,"############################")
73 call append(1,"# Author: bo".(" #")) 作者
74 call append(2,"# Version: ".(" #")) 版本
75 call append(3,"# Mail: ".(" #")) 邮箱
76 call append(4,"# Date: ".strftime("%Y-%m-%d").(" #")) 日期
77 call append(5,"# Description: ".(" #")) 简介描述
78 call append(6,"# ".(" #"))
79 call append(7,"############################")
80 call append(8," ")
81 call append(9,"#!/bin/bash")
82 endfunc
[root@shell mnt]# vim ceshi.sh
[root@shell mnt]# cat ceshi.sh
############################
# Author: bo #
# Version: #
# Mail: #
# Date: 2018-06-14 #
# Description: #
# #
############################
#!/bin/bash
此脚本的作用是,当你进入vim模式按下F4键会自动填写如下内容
############################
# Author: bo #
# Version: #
# Mail: #
# Date: 2018-06-14 #
# Description: #
# #
############################
#!/bin/bash
我们想让当我们新建一个以.sh结尾的脚本时,自动填写以上内容,不用按F4
[root@shell mnt]# vim /etc/vimrc
69"map <F4> ms:call WESTOS()<cr>'s ##注释掉
70 autocmd BufNewFile *.sh exec ":call WESTOS()" 当创建新的以.sh结尾的shell,自动添加以下内容
71 func WESTOS ()
72 call append(0,"############################")
73 call append(1,"# Author: bo".(" #")) 作者
74 call append(2,"# Version: ".(" #")) 版本
75 call append(3,"# Mail: ".(" #")) 邮箱
76 call append(4,"# Date: ".strftime("%Y-%m-%d").(" #")) 日期
77 call append(5,"# Description: ".(" #")) 简介描述
78 call append(6,"# ".(" #"))
79 call append(7,"############################")
80 call append(8," ")
81 call append(9,"#!/bin/bash")
82 endfunc
调试脚本
创建一个脚本
[root@shell mnt]# vim date.sh
[root@shell mnt]# cat date.sh
############################
# Author: bo #
# Version: #
# Mail: #
# Date: 2018-06-14 #
# Description: #
# #
############################
#!/bin/bash
date
[root@shell mnt]#
[root@shell mnt]# sh -x date.sh 调试脚本date.sh
+ date
Thu 14 Jun 09:25:53 EDT 2018
[root@shell mnt]# date.sh
bash: date.sh: command not found...
[root@shell mnt]# /mnt/date.sh 无法运行,没有可执行权限
-bash: /mnt/date.sh: Permission denied
[root@shell mnt]# sh /mnt/date.sh sh 可以执行
Thu 14 Jun 09:27:06 EDT 2018
[root@shell mnt]# chmod +x /mnt/date.sh 给脚本可执行权限
[root@shell mnt]# /mnt/date.sh 可以执行
Thu 14 Jun 09:27:30 EDT 2018
[root@shell mnt]#
练习:
创建一个脚本——建立用户并添加密码,用户存在时不建立,不存在时建立
[root@shell mnt]# vim user_create.sh
[root@shell mnt]# cat user_create.sh
############################
# Author: bo #
# Version: #
# Mail: #
# Date: 2018-06-14 #
# Description: #
# #
############################
#!/bin/bash
[ "$#" -eq "2" ]||{
echo "please intput username and password after script!!"
exit 1
}
Check_User=`getent passwd $1`
[ -n "$Check_User" ]&&{
echo $1 is exist!!
exit 1
}
useradd $1
echo $2 | passwd --stdin $1
[root@shell mnt]# sh user_create.sh
please intput username and password after script!!
[root@shell mnt]# id hehe
id: hehe: no such user
[root@shell mnt]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@shell mnt]# sh user_create.sh hehe hehe
Changing password for user hehe.
passwd: all authentication tokens updated successfully.
[root@shell mnt]# sh user_create.sh student hehe
student is exist!!
[root@shell mnt]#