shell脚本基础知识
一、shell脚本
1、shell脚本是什么
将需要执行的命令保存到一个文件中,按照顺序执行,它不需要编译,它是解释型的
2、shell脚本能干什么
- 自动化完成软件的安装部署,如安装部署LAMP架构服务
- 自动化完成系统的管理,如批量添加用户
- 自动化完成备份,如数据库定时备份
- 自动化的分析处理,如网站访问量
3、shell脚本使用场景
在需要完成大量复杂、重复性的工作时,不需要在命令行重复执行命令,直接运行shell脚本即可,大大的节省了时间提高了效率
4、如何学习shell脚本
- 熟悉掌握各种linux命令
- 掌握脚本的标准格式
- 掌握脚本的基本语法
5、shell脚本的基本写法
-
第一部分:声明shell脚本的解释器
如:
#!/bin/bash
#!/bin/env bash
-
第二部分:注释说明(
用#开头
)如:#本脚本实现的功能
#更新时间、用法等 -
第三部分:具体代码内容
[root@server myscripts]# vim first.sh
#!/bin/bash
# 这是我人生中的第一个脚本!
# Name:first.sh
echo "hello world"
6、shell脚本的执行方法
写好的脚本给脚本赋予可执行
的权限(建议!)
[root@server myscripts]# chmod +x first.sh
[root@server myscripts]# ./first.sh //相对路径
hello world
[root@server ~]# /opt/myscripts/first.sh //绝对路径
hello world
没有可执行权限也可以执行:
[root@server myscripts]# bash first.sh
hello world
[root@server myscripts]# sh first.sh
hello world
[root@server myscripts]# . first.sh
hello world
[root@server myscripts]# source first.sh
hello world
7、shell脚本的调试
1、bash -x
显示脚本执行过程
[root@server myscripts]# bash -x first.sh
+ echo 'hello world'
hello world
2、bash -n
检查脚本语法
[root@server myscripts]# vim first.sh
#!/bin/bash
# 这是我人生中的第一个脚本!
# Name:first.sh
if
echo "hello world"
[root@server myscripts]# bash -n first.sh
first.sh:行8: 语法错误: 未预期的文件结尾
8、编写一个shell脚本
把你想要在系统中执行的命令按顺序写入一个文件,指定用bash执行,这就是一个最简单的shell脚本
[root@server myscripts]# vim new.sh
#!/bin/bash
rm -rf /tmp/*
mkdir /tmp/dir{1..3}
cp /etc/hosts /tmp/dir1
echo "任务已于$(date +'%F %T')完成"
[root@server myscripts]# ./new.sh
任务已于2020-07-26 19:26:47完成
[root@server tmp]# ls
dir1 dir2 dir3
二、shell的变量
变量是用来临时保存数据的,并且该数据时可以变化的,任何一个语言都离不开变量,如果某个内容需要多次使用并且会重复出现,这样就可以使用变量了,如果需要修改直接修改变量就可以了
1、变量的定义
变量名=变量值
变量名:临时存放数据的地方
变量值:临时的可变化的数据
例1:定义一个变量A,把hello赋值给A,然后再把world赋值给A,最后取消定义变量A
[root@server myscripts]# A=hello
[root@server myscripts]#
[root@server myscripts]# echo $A
hello
[root@server myscripts]# echo ${A}
hello
[root@server myscripts]# A=world
[root@server myscripts]# echo $A
world
[root@server myscripts]# unset A
[root@server myscripts]# echo $A
2、变量名定义的规则
- 1、区分大小写
[root@server myscripts]# A=hello
[root@server myscripts]# a=world
[root@server myscripts]# echo $A
hello
[root@server myscripts]# echo $a
world
- 2、不能包含特殊符号
[root@server myscripts]# ?A=hello
bash: ?A=hello: 未找到命令...
[root@server myscripts]# *A=hello
bash: *A=hello: 未找到命令...
- 3、不能以数字开头,但可以包含数字
[root@server myscripts]# 1A=hello
bash: 1A=hello: 未找到命令...
[root@server myscripts]# 2A=hello
bash: 2A=hello: 未找到命令...
- 4、如果变量的值中间有空格需要用
单、双引号
引起来
[root@server myscripts]# A=hello world
bash: world: 未找到命令...
[root@server myscripts]# A="hello world"
[root@server myscripts]#
[root@server myscripts]# echo $A
hello world
[root@server myscripts]# A='hello world'
[root@server myscripts]# echo $A
hello world
- 5、等号两边不可以有空格
[root@server myscripts]# A = hello
bash: A: 未找到命令...
[root@server myscripts]# A =hello
bash: A: 未找到命令...
[root@server myscripts]# A= hello
bash: hello: 未找到命令...
3、变量定义的方式
- 1、直接赋值,可以是字符串,注意
花括号
的用法
[root@server myscripts]# A=123456
[root@server myscripts]# echo $A
123456
[root@server myscripts]# echo ${A}
123456
[root@server myscripts]# echo ${A:2:3} //从第三个开始取值,取3个,2相当于索引
345
- 2、把某个命令的结果赋值给变量,要把命令用
反撇号``引起来
或者使用$()
[root@server myscripts]# A=`hostname`
[root@server myscripts]# echo $A
server
[root@server myscripts]# A=$(hostname)
[root@server myscripts]# echo $A
server
- 3、交互式定义变量(
read
) - -p 提示用户的信息
- -n 定义字符数
- -s 不显示用户输入的内容
- -t 定义超时时间,超过多长时间没输自动退出
直接定义变量name,光标不动等待用户输入,输入shell,变量的值就会赋值给name
[root@server myscripts]# read name
shell
[root@server myscripts]# echo $name
shell
提示用户输入什么内容
[root@server myscripts]# read -p "input your name:" name
input your name:shell
[root@server myscripts]# echo $name
shell
隐藏输入的内容
[root@server myscripts]# read -s -p "input your password:" pass
input your password:[root@server myscripts]#
[root@server myscripts]# echo $pass
123456
限制只能输入5个字符,刚输到第五个字符就会退出
[root@server myscripts]# read -n 5 -p "input your name:" name
input your name:shell[root@server myscripts]#
限制3秒输入内容,如果没有输入3秒后会退出
[root@server myscripts]# read -t 3 -p "input your name:" name
input your name:[root@server myscripts]#
- 4、从文件读取内容赋值给变量
[root@server myscripts]# echo 192.168.100.100 > ip.txt
[root@server myscripts]# cat ip.txt
192.168.100.100
[root@server myscripts]# read -p "input your ip:" IP < ip.txt
[root@server myscripts]# echo $IP
192.168.100.100
- 5、定义有类型的变量(
declare
)- -i 定义一个整数变量
- -r 定义只读变量
- -x 定义系统环境变量
给A定义为一个整型变量,值为123,再次给他赋予一个字符串的值,虽然没有报错可是它并不理你
[root@server myscripts]# declare -i A=123
[root@server myscripts]# echo $A
123
[root@server myscripts]# A=hello
[root@server myscripts]# echo $A
0
定义一个只读变量B,无法重新赋值,也没法删除,需要exit退出当前shell才行
[root@server myscripts]# declare -r B=123
[root@server myscripts]# echo $B
123
[root@server myscripts]# B=111
-bash: B: 只读变量
[root@server myscripts]# unset B
-bash: unset: B: 无法反设定: 只读 variable
定义一个系统的环境变量用declare -x
,不加任何声明的变量只是临时变量,需要export
才能变成环境变量
[root@server myscripts]# declare -x name=shengjie
[root@server myscripts]# env | grep name
name=shengjie
[root@server myscripts]# name=kgc
[root@server myscripts]# env | grep name
name=kgc
[root@server myscripts]# AA=12
[root@server myscripts]# env | grep AA
[root@server myscripts]# export AA
[root@server myscripts]#
[root@server myscripts]# env | grep AA
AA=12
三、shell脚本中的运算
shell当中的算术运算,只支持整数的运算
运算内容:加(+)、减(-)、乘(*)、除(/)、取余(%)
运算符号:(())
和$[]
运算命令:expr
和let
[root@server myscripts]# echo $((1+1))
2
[root@server myscripts]# echo $((5-2))
3
[root@server myscripts]# echo $[10*10]
100
[root@server myscripts]# echo $[10%8]
2
[root@server myscripts]# echo $[10/8]
1
[root@server myscripts]# echo $[10/12]
0
[root@server myscripts]# echo $[10%12]
10
[root@server myscripts]# expr 1 + 1
2
[root@server myscripts]# expr 1+1
1+1
[root@server myscripts]# expr 2 * 2
expr: 语法错误
[root@server myscripts]# expr 2 \* 2 //乘法*需要转义
4
[root@server myscripts]# n=1;let n=n+1;echo $n
2
[root@server myscripts]# let n+=2 //n=n+2
[root@server myscripts]#
[root@server myscripts]# echo $n
4
[root@server myscripts]# let n=n**2 //求幂,4的2次方
[root@server myscripts]# echo $n
16
如果要进行其他非整数的运算,可以使用系统自带的bc
命令
[root@server myscripts]# echo $[1+1.5]
-bash: 1+1.5: 语法错误: 无效的算术运算符 (错误符号是 ".5")
[root@server myscripts]# echo 1+1.5|bc
2.5
[root@server myscripts]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1.5+2.5 //这里输入想要计算的公式
4.0