1. 准备工具
添加到/etc/vimrc
bash
autocmd BufNewFile *.py,*.cc,*.sh,*.java,*.bash,Dockerfile,docker-compose.yml exec ":call SetTitle()"
func SetTitle()
if expand("%:e") =~ 'sh\|bash'
call setline(1,"#!/bin/bash")
call setline(2, "##############################################################")
call setline(3, "# File Name: ".expand("%"))
call setline(4, "# Version: V1.0")
call setline(5, "# Author: zbl")
call setline(6, "# Organization: www.zbl.com")
call setline(7, "# Description:")
call setline(8, "##############################################################")
call setline(9, "")
endif
if expand("%") == 'Dockerfile'
call setline(1, "#####################Dockerfile###############################")
call setline(2, "##############################################################")
call setline(3, "# File Name: ".expand("%"))
call setline(4, "# Version: V1.0")
call setline(5, "# Author: zbl")
call setline(6, "# Organization: www.zbl.com")
call setline(7, "# Description:")
call setline(8, "##############################################################")
call setline(9, "")
call setline(10, "FROM")
call setline(11, "LABEL maintaniner='zbl [email protected]' author=zbl")
call setline(12, "CMD []")
endif
if expand("%") == 'docker-compose.yml'
call setline(1, "#####################docker-compose###########################")
call setline(2, "##############################################################")
call setline(3, "# File Name: ".expand("%"))
call setline(4, "# Version: V1.0")
call setline(5, "# Author: zbl")
call setline(6, "# Organization: www.zbl.com")
call setline(7, "# Description:")
call setline(8, "##############################################################")
call setline(9, "")
call setline(10, "version: '3.3'")
call setline(11, "services:")
call setline(12, "volumes:")
endif
endfunc
-
vim
-
sublime/notepad(大一些脚本使用)
-
故障案例:windows书写的脚本上传到Linux运行,出现错误
bash
原因:windows下回车符号是\n\r,linux下回车符号就是\n,执行windows下写的内容报错
解决:dos2unix xxx.sh #windows格式转换linux格式
2. 脚本基本必会内容
2.1. #!/bin/bash
- 指定命令解释器
- 用于给系统运行或指定服务运行的脚本
- #! shabang符号
2.2. 运行脚本方法
|------------|-----------------------------------------------------|
| 运行脚本方法 | 应用场景 |
| bash/sh | 书写的脚本通过sh/bash运行 |
| 路径方法运行 | 服务软件运行脚本需要这样使用 |
| source 或 . | 运行脚本,加载带有变量,自定义函数库 /etc/profile使用 实现include功能,加载子脚本 |
3. 变量
- 普通变量
- 环境变量
- 特殊变量
3.1. 普通变量
- 命名规则
-
- 不能以数字开头
- 不要使用单独字母,最好看见变量名字就知道变量作用(memory_total)
- ${} 引用变量
3.2. 环境变量
- 全局变量(系统定义的)
|------|---------------------------------------------|
| 环境变量 | 说明 |
| LANG | 语言字符集,export LANG=en_US.UTF-8,localectl永久修改 |
| PS1 | 命令行格式 |
| PATH | 存放命令的路径 |
| UID | 当前的用户UID,判断中使用,检查当前用户是否为root |
- export创建或修改全局变量
3.3. 特殊变量
3.3.1. 常用必会的特殊变量
- 脚本中使用
|------|--------------------------------------|
| 特殊变量 | 说明 |
| n | 数字,脚本传参功能,1表示第一个参数,2表示第二个参数 |
| 0 | 脚本名字,有路径或无路径 |
| # | 脚本参数的个数,一般与判断结合做检查 |
| * | 取出脚本所有参数相当于替你写了1,2,3......,一般搭配循环 |
| @ | 取出脚本所有参数相当于替你写了1,2,3......,一般搭配循环 |
| ? | 上一个命令返回值是否正确,0表示正确,非0表示不正确,脚本判断使用 |
bash
[root@ky201 /study]# cat show.sh
#!/bin/bash
##############################################################
# File Name: show.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
cat<<EOF
脚本前三个参数:$1 $2 $3
脚本名字:$0
脚本参数个数:$#
脚本所有参数:$*
脚本所有参数:$@
EOF
[root@ky201 /study]# bash show.sh 1 2 3 4
脚本前三个参数:1 2 3
脚本名字:show.sh
脚本参数个数:4
脚本所有参数:1 2 3 4
脚本所有参数:1 2 3 4
- 面试题:$n,n大于9会有什么问题?
bash
[root@ky201 /study]# cat show.sh
#!/bin/bash
##############################################################
# File Name: show.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
cat<<EOF
脚本前三个参数:$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11
脚本名字:$0
脚本参数个数:$#
脚本所有参数:$*
脚本所有参数:$@
EOF
[root@ky201 /study]# bash show.sh {a..z}
脚本前三个参数:a b c d e f g h i a0 a1
脚本名字:show.sh
脚本参数个数:26
脚本所有参数:a b c d e f g h i j k l m n o p q r s t u v w x y z
脚本所有参数:a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@ky201 /study]#
[root@ky201 /study]# bash show.sh {a..z}
脚本前三个参数:a b c d e f g h i j k
脚本名字:show.sh
脚本参数个数:26
脚本所有参数:a b c d e f g h i j k l m n o p q r s t u v w x y z
脚本所有参数:a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@ky201 /study]#
4. 基础判断
4.1. if判断格式
bash
#单分支判断
if [ 条件 ];then
执行的命令
fi
if [ 条件 ]
then
执行的命令
fi
#双分支判断
if [ 条件 ];then
满足条件执行的命令
else
不满足条件执行的命令
fi
if [ 条件 ]
then
满足条件执行的命令
else
不满足条件执行的命令
fi
4.2. 条件:比较大小
|------|------------------|
| 比较大小 | 符号 |
| 等于 | -eq(equal) |
| 不等于 | -ne(not equal) |
| 大于 | -gt(great then) |
| 大于等于 | -ge(great equal) |
| 小于 | -lt(less than) |
| 小于等于 | -le(less equal) |
4.3. 案例
- 案例01:书写ping检查域名/ip的脚本,成功输出ip/域名可以访问,否则输出无法访问
bash
#!/bin/bash
##############################################################
# File Name: check_ip.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
#1.vars
ip=$1
#1.1 check
if [ $# -ne 1 ];then
echo "use $0 ip/url"
exit 1
#退出脚本
#返回值是1
fi
#2.ping
ping -c2 $ip >/dev/null 2>&1
#3.if
if [ $? -eq 0 ];then
echo "${ip} 可以访问"
else
echo "${ip} 不可以访问"
fi
- 案例02:检查指定用户是否存在,存在输出用户存在,不存在输出用户不存在。检查参数个数
bash
#!/bin/bash
##############################################################
# File Name: check_user.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
#1.vars
user=$1
#2.check
if [ $# -ne 1 ];then
echo "use $0 username"
exit 1
fi
#3.id user
id ${user}>/dev/null 2>&1
if [ $? -eq 0 ];then
echo "用户存在"
else
echo "用户不存在"
fi
案例03: 书写回收站脚本执行rm命令的时候调用mv命令移动指定文件或目录到/recycyle/目录下
- 编写脚本
bash
#!/bin/bash
##############################################################
# File Name: recycle.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
#1.vars
files="$*"
#2.check
if [ $# -eq 0 ];then
echo "use rm files dir"
exit 1
fi
#3.check root
if [ $UID -ne 0 ];then
echo "please use mv"
exit 2
fi
#4.创建临时目录
tmpdir=`mktemp -dp /recycle/`
#5.mv
mv ${files} ${tmpdir}
#6.输出结果
echo "文件已经移动到回收站:${tmpdir}"
- 使用别名
bash
#临时使用别名,然后测试脚本
alias rm='bash /server/scripts/recycle.sh'
#写入到/etc/profile文件中使其永久生效
vim /etc/profile
alias rm='bash /server/scripts/recycle.sh'
5. 基础循环
- for 循环
5.1. 基本格式
bash
for n in {1..10}
do
命令
done
for n in 1 2 3 3 4 5 6 7 8 9 10
do
echo $n
done
5.2. 案例
- 案例01:批量添加用户
bash
for name in zbl{01..10}
do
useradd $name
done
- 案例02:批量检查linux系统中可登录用户的uid,gid信息
- 步骤
bash
1.取出所有linux系统中可登录用户名
2.交给for循环
3.对用户进行检查,取出uid,gid
4.输出
- 编写脚本
bash
#!/bin/bash
##############################################################
# File Name: 02-userid.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
#1.vars
users=`grep '/bin/bash' /etc/passwd | awk -F ':' '{print $1}'`
#2.for
for name in $users
do
uid=`id $name | awk -F '[= ]' '{print $2}'`
gid=`id $name | awk -F '[= ]' '{print $4}'`
group=`id $name | awk -F '[= ]' '{print $6}'`
echo "用户名:$name,用户的uid:$uid,用户的gid:$gid,用户组信息:$group"
done
- 案例03:批量检查ip或url是否可以访问
bash
#!/bin/bash
##############################################################
# File Name: 03-ip.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################
#1.vars
ips="$*"
#2.check
if [ $# -eq 0 ];then
echo "use $0 ip/url"
exit 1
fi
#3.for
for ip in $ips
do
ping -c1 $ip >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "$ip 可以访问"
else
echo "$ip 不可以访问"
fi
done
