1、Shell脚本概述
●Shell的作用
●Shell编程规范
●重定向与管道
2、Shell脚本变量
●自定义变量
●特殊变量
1.1 Shell脚本的概念
●将要执行的命令按顺序保存到一个文本文件
●给该文件可执行权限
●可结合各种Shell控制语句以完成更复杂的操作
1.2 Shell脚本应用场景
●重复性操作
●交互性任务
●批量事务处理
●服务运行状态监控
●定时任务执行
1.3 Shell脚本的作用——命令解释器,"翻译官"
1.4Shell脚本编写格式
(1)使用vim编辑器
(2)每行一条linux命令,按照顺序编写
(3)使脚本具有可执行性chomd +x test.sh
vim test.sh
#!/bin/bash 第一行声明注释器
#注释内容(可多行注释)
命令 第三行执行命令
保存退出
chmod +x test.sh 第一种方法添加权限
./test.sh 查看到添加的内容
(1)bash test.sh (2) sh test.sh 第二种方法执行(没有权限的解析运行用程序来情况)
(1)source test.sh(2) . test.sh 第三种方法执行(只有这两个在当前shell环境运行)
示例
[root@localhost ~]# vim test.bash
#!/bin/bash
#打出hello word字符
echo "HELLO WORLD"
[root@localhost ~]# ./test.bash
-bash: ./test.bash: 权限不够
[root@localhost ~]# chmod +x test.bash 需要添加权限
[root@localhost ~]# ./test.bash 查看内容
HELLO WORLD
[root@localhost ~]# chmod -x test.bash
[root@localhost ~]# source test.bash 不需要添加权限直接执行查看
HELLO WORLD
[root@localhost ~]# . test.bash 不需要添加权限直接执行查看
HELLO WORLD
bash -x test.sh 逐行调试
bash -n test.sh 显示哪一行错误
[root@localhost ~]# bash -x test.bash
+ echo 'HELLO WORLD'
HELLO WORLD
[root@localhost ~]# bash -n test.bash
[root@localhost ~]#
一键安装启动http
[root@localhost ~]# vim test.bash
#!/bin/bash
#一键启动apache服务
yum -y install httpd
systemctl start httpd
netstat -antp | grep httpd
1.5、重定向与管道
交互式硬件设备
标准输入:从该设备接收用户输入的数据
标准输出:通过该设备向用户输出数据
标准错误:通过该设备报告执行出错信息
重定向操作
示例输出符号>,2>
[root@localhost ~]# echo 111 > 1.txt >覆盖内容
[root@localhost ~]# cat 1.txt
111
[root@localhost ~]# echo 111 >> 1.txt >>
[root@localhost ~]# cat 1.txt
111
111
[root@localhost ~]# echo 111 1> 1.txt 1可以省略与>用法一样
[root@localhost ~]# cat 1.txt
111
[root@localhost ~]# ls xxx
ls: 无法访问xxx: 没有那个文件或目录
[root@localhost ~]# ls xxx > 2.txt
ls: 无法访问xxx: 没有那个文件或目录
[root@localhost ~]# ls xxx 2> 2.txt 用2>结果不会输出到屏幕
[root@localhost ~]#
[root@localhost ~]# ls xxx 2> 2.txt
[root@localhost ~]# cat 2.txt
ls: 无法访问xxx: 没有那个文件或目录
[root@localhost ~]# ls xxx 2>> 2.txt
[root@localhost ~]# cat 2.txt 可以看到追加的结果
ls: 无法访问xxx: 没有那个文件或目录
ls: 无法访问xxx: 没有那个文件或目录
[root@localhost ~]#
[root@localhost ~]# ls xxx file.sh 2> 3.txt >4.txt 正确的file.sh与错误的xxx输出到不同的文件
[root@localhost ~]#
[root@localhost ~]# ls xxx file.sh &> 5.txt 正确的file.sh与错误的xxx输出到相同的文件
[root@localhost ~]#
执行命令 &> /dev/null 不想看见执行命令后的结果则丢入垃圾桶
示例输入符号<
2.1自定义变量
a=httpd 变量名=变量值 变量名:字母,数字,下划线组成,不能以数字开头,区分大小写,不能是特殊字符
unset a 取消赋值
set 查看所有变量
declare -i a=10 declare -i 只能赋值整数,若非整数则为0
示例
[root@localhost ~]# a=httpd
[root@localhost ~]# echo $a
httpd
[root@localhost ~]# unset a
[root@localhost ~]# echo $a
[root@localhost ~]# declare -i a=10
[root@localhost ~]# echo $a
10
[root@localhost ~]# echo ${a}rmb 大括号用法
10rmb
[root@localhost ~]# abc=5.5
[root@localhost ~]# dd="echo $abc"
[root@localhost ~]#
[root@localhost ~]# echo $abc
5.5
[root@localhost ~]# echo $dd
echo 5.5
[root@localhost ~]#
[root@localhost ~]# a=hi hello
bash: hello: 未找到命令...
[root@localhost ~]# a="hi hello" 变量值如果是断开的必须要加""字符
[root@localhost ~]# echo $a
hi hello
[root@localhost ~]# echo \$a
$a
[root@localhost ~]# echo "$a"
hi hello
[root@localhost ~]# echo '$a'
$a
转移符号\用法
[root@localhost ~]# a=hi\ world
[root@localhost ~]# echo $a
hi world
反撇号用法
[root@localhost ~]# echo `date`
2021年 06月 02日 星期三 03:19:55 CST
[root@localhost ~]# a=`date`
[root@localhost ~]# echo $a
2021年 06月 02日 星期三 03:21:37 CST
小括号用法
[root@localhost ~]# a=(date)
[root@localhost ~]# echo $a
2021年 06月 02日 星期三 03:21:37 CST
只读readonly用法
[root@localhost ~]# a=xkl
[root@localhost ~]# readonly a 设置a变成只读不可以修改也不能unset,只能退出重新加入才无效
2.2交互式变量read
[root@localhost ~]# read name read+变量名
xkl 输入变量值
[root@localhost ~]# echo $name
xkl
[root@localhost ~]# read -p "输入你的姓名" name
输入你的姓名xkl
[root@localhost ~]# echo $name
xkl
read带选项用法
[root@localhost ~]# read -n 5 -p "请输入你的名字:" name 5代表最多输入5个字符
请输入你的名字:xukan[root@localhost ~]#
[root@localhost ~]# read -s -p "请输入你的名字:" name -s代表私密输入不会显示
请输入你的名字:[root@localhost ~]#
示例
#!/bin/bash
#这是一个创建用户的脚本
#作者:xkl
#日期:2021-06-08
read -p "输入用户名:" user
read -p "输入密码:" pass
useradd $user
echo "$pass" | passwd --stdin $user
id $user
2.3环境变量
环境变量全大写,环境变量是系统定义好的,我们只需正确调用即可
环境变量又称之为全局变量,与局部变量不同,可一直沿用
env 查看所有的环境变量
echo $SHLVL 查看到了第几层bash
[root@localhost ~]#bb=20
[root@localhost ~]#export bb 临时把bb=20从局部变量变成环境变量
另一种方法
[root@localhost ~]#export bb=20 临时把bb=20从局部变量变成环境变量
局部变量永久变成环境变量
[root@localhost ~]# aa=10
[root@localhost ~]# vim /etc/profile
在配置文件最后加入export aa=10
[root@localhost ~]#source /etc/profile
echo -n 不换行
echo -e带转义字符
\c不换行
\n换行加回