目录
1.Shell编程
- Linux运维工程师在进行服务器集群管理时,需要编写Shell程序来进行服务器管理。
- 对于JavaEE和Python程序员来说,工作的需要,你的老大会要求你编写一些Shell脚本进行程序或者是服务器的维护,比如编写一个定时备份数据库的脚本。
- 对于大数据程序员来说,需要编写Shell程序来管理集群。
1.1 shell脚本的执行方式
- 脚本以#!/bin/bash开头
- 脚本需要有可执行权限
- 一般以.sh命名,当然后缀名不限制
编写一个shell脚本,输出“ hello world!”
[root@hadoop ~]# cd /home/shell
[root@hadoop shell]# vim myshell.sh (见下图)
[root@hadoop shell]# ll
总用量 4
-rw-r--r--. 1 root root 32 3月 30 00:05 myshell.sh
[root@hadoop shell]# /home/shell/myshell.sh
-bash: /home/shell/myshell.sh: 权限不够
# sh+.sh不用授权能执行shell脚本,但是不推荐
[root@hadoop shell]# sh /home/shell/myshell.sh
hello world!
# 授权
[root@hadoop shell]# chmod 744 myshell.sh
[root@hadoop shell]# ll
总用量 4
-rwxr--r--. 1 root root 32 3月 30 00:05 myshell.sh
# 执行
[root@hadoop shell]# /home/shell/myshell.sh
hello world!
1.2 shell变量
Linux Shell中的变量分为:系统变量和用户自定义变量。
1.2.1 系统变量
- 系统变量:
$HOME、$PWD、$SHELL、$USER
等; - 显示当前shell中所有变量:set
输出系统变量
[root@hadoop shell]# vim myshell.sh
[root@hadoop shell]# /home/shell/myshell.sh
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
USER=root
1.2.2 自定义变量
- 定义变量:变量=值
- 撤销变量:unset 变量
- 声明静态变量:readonly变量,注意:不能unset
- 变量名称可以由字母、数字和下划线组成,但是不能以数字开头。
- 等号两侧不能有空格
- 变量名称一般习惯为大写
[root@hadoop shell]# vim myshell.sh
[root@hadoop shell]# /home/shell/myshell.sh
A=100
A=
# 声明静态的变量不能unset
[root@hadoop shell]# vim myshell.sh
[root@hadoop shell]# /home/shell/myshell.sh
A=100
/home/shell/myshell.sh: line 9: unset: A: cannot unset: readonly variable
A=100
1.2.3 将命令的返回值赋给变量
[root@hadoop shell]# vim myshell.sh
[root@hadoop shell]# /home/shell/myshell.sh
总用量 68 -rwxr-xr-x. 1 tom police 0 2月 21 23:19 abc.txt -rw-r--r--. 1 tom root 0 2月 21 19:27 apple.txt -rw-r--r--. 1 root police 1796 2月 17 21:43 a.txt -rw-r--r--. 1 root root 53 2月 18 11:03 hello.txt -rw-r--r--. 1 root root 142 2月 17 21:46 mycal -rw-r--r--. 1 root root 5685 2月 18 14:41 myhome.tar.gz -rw-r--r--. 1 root root 6510 2月 18 11:29 mypackage.zip -rwxr--r--. 1 root root 26 3月 3 19:39 mytask1.sh -rwxr--r--. 1 root root 49 3月 3 19:54 mytask2.sh -rwxr--r--. 1 root root 75 3月 3 22:22 mytask3.rh drwxr-xr-x. 3 root root 4096 3月 4 00:24 newdisk drwxr-xr-x. 2 root root 4096 3月 30 01:34 shell drwxr-xr-x. 3 root root 4096 3月 3 19:57 test drwx------. 4 tom police 4096 2月 22 01:07 tom -rw-r--r--. 1 root root 440 2月 25 01:07 to.txt drwx------. 4 tom police 4096 2月 15 18:07 xm drwx------. 5 zwj wudang 4096 3月 8 19:45 zwj
date=2022年 03月 30日 星期三 01:34:31 CST
1.3 设置环境变量
1.3.1 基本语法
- export 变量名=变量值 (功能描述:将shell变量输出为环境变量)
- source 配置文件(功能描述:让修改后的配置信息立即生效)
- echo $变量名 (功能描述:查询环境变量的值)
案例:
- 在/etc/profile文件中自定义TOMCAT_HOME环境变量
[root@hadoop shell]# vim /etc/profile
2. 查看环境变量TOMCAT_HOME的值
3. 在另外一个shell程序中使用 TOMCAT_HOME
[root@hadoop shell]# vim myshell.sh
多行注释
[root@hadoop shell]# ./myshell.sh
tomcathome=/opt/tomcat
1.3.2 位置参数变量
案例:编写一个shell脚本 positionPara.sh , 在脚本中获取到命令行的各个参数信息。
[root@hadoop shell]# vim positionPara.sh
[root@hadoop shell]# chmod 544 positionPara.sh
[root@hadoop shell]# ls
myshell.sh positionPara.sh
[root@hadoop shell]# ./positionPara.sh 30 60
./positionPara.sh 30 60
30 60
30 60
count=2
1.3.3 预定义变量
就是shell设计者事先已经定义好的变量,可以直接在shell脚本中使用
- $$ (功能描述:当前进程的进程号(PID))
- $! (功能描述:后台运行的最后一个进程的进程号(PID))
- $? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。)
案例:在一个prevar.sh脚本中简单使用一下预定义变量
[root@hadoop shell]# vim prevar.sh
[root@hadoop shell]# ls
myshell.sh positionPara.sh prevar.sh
[root@hadoop shell]# chmod 744 prevar.sh
[root@hadoop shell]# ls
myshell.sh positionPara.sh prevar.sh
./prevar.sh
1.4 运算符
[root@hadoop shell]# vim demo.sh
[root@hadoop shell]# chmod 777 demo.sh
[root@hadoop shell]# ./demo.sh 10 18
result1=20
result2=20
result3=20
sum=28
1.5 条件判断
基本语法
[ condition ](注意condition前后要有空格)
#非空返回true,可使用$?验证(0为true,>1为false)
[root@hadoop shell]# vim judge.sh
[root@hadoop shell]# chmod 777 judge.sh
[root@hadoop shell]# ./judge.sh
re1=yes
大于
[root@hadoop shell]# touch aaa.txt
[root@hadoop shell]# ./judge.sh
re1=yes
大于
存在
1.6 流程控制
删除shell文件
1.6.1 if判断
请编写一个shell程序,如果输入的参数(用$1表示),大于等于60,则输出 “及格了”,如果小于60,则输出 “不及格”。
[root@hadoop shell]# vim testif.sh
[root@hadoop shell]# ./testif.sh 66
及格了
[root@hadoop shell]# ./testif.sh 59
不及格
1.6.2 case语句
当命令行参数是 1 时,输出 “周一”, 是2 时,就输出"周二", 其它情况输出 “other”
[root@hadoop shell]# vim testcase.sh
[root@hadoop shell]# chmod 744 testcase.sh
[root@hadoop shell]# ./testcase.sh 1
周一
[root@hadoop shell]# ./testcase.sh 2
周二
[root@hadoop shell]# ./testcase.sh 3
other
1.6.3 for循环
基本语法1
for 变量 in 值1 值2 值3…
do
程序
done
打印命令行输入的参数[这里可以看出$* 和 $@ 的区别]。
[root@hadoop shell]# vim testfor.sh
[root@hadoop shell]# chmod 744 testfor.sh
[root@hadoop shell]# ./testfor.sh 10 20 30
the num is 10 20 30
============================
the num is 10 20 30
the num is 10 20 30
the num is 10 20 30
基本语法2
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
从1加到100的值输出显示。
[root@hadoop shell]# vim testfor2.sh
[root@hadoop shell]# chmod 744 testfor2.sh
[root@hadoop shell]# ./testfor2.sh
sum=5050
1.6.4 while循环
从命令行输入一个数n,统计从 1+…+ n 的值是多少?
[root@hadoop shell]# vim testwhile.sh
[root@hadoop shell]# chmod 744 testwhile.sh
[root@hadoop shell]# ./testwhile.sh 100
sum=5050
1.7 read读取控制台输入
read(选项)(参数) :
-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了。。
变量:指定读取值的变量名
应用实例
root@hadoop shell]# vim testread.sh
[root@hadoop shell]# chmod 744 testread.sh
[root@hadoop shell]# ./testread.sh
10s内不输入就自动跳走。
1.8 函数
1.8.1 系统函数
请返回 /home/aaa/test.txt 的 “test.txt” 部分
请返回 /home/aaa/test.txt 的 /home/aaa
1.8.2 自定义函数
计算输入两个参数的和(read), getSum
root@hadoop shell]# vim testfun.sh
[root@hadoop shell]# chmod 744 testfun.sh
[root@hadoop shell]# ./testfun.sh
1.9 Shell变成综合案例
分析:
1、备份目录有可能存在,有可能不存在
2、时间命名防止重复
3、定期清除
代码实现:
[root@hadoop shell]# cd /usr/sbin/
[root@hadoop shell]# vim mysql_db_backup.sh
[root@hadoop shell]# chmod u+x mysql_db_backup.sh
[root@hadoop shell]# ./mysql_db_backup.sh
加入定时:
crontab -e