Shell编程
shell初步入门
1)什么是shell
shell是命令解释器,负责翻译我们输入的命令
交互式:输入命令后,shell负责解析执行我们输入的命令,并把结果输出在屏幕上的过程
非交互式:读取存储在文本中的shell命令,读取到文本结尾,shell结束
2)什么是shell脚本
把可执行的命令统一放入到一个文本中,称为shell脚本 包含了 判断语句 循环语句 数组等
3)脚本语言的种类
编译型 c c++
解释型 python ruby
脚本型 python shell javascript
其他语言: PHP html go
问: 编译型语法是最快的吗 看优化
面试题: Linux中默认的shell解释器是 bash
shell和py的区别
shell处理底层的能力较强 所有的服务都是shell编写 一键优化 一键安装 一键统计(awk)
py主要作用可以写界面 自动化管理平台CMDB 功能需求
4)书写脚本的规范
a.脚本存放在固定目录 统一管理
b.脚本使用.sh结尾 让我们能识别是shell脚本 有语法检测
c.脚本命名 见名知其意
d.脚本开头使用解释器
e.脚本内的注释最好不用英文(可以用)
f.脚本内的成对符号一次性书写完毕 语法写完再写内容
5)写第一个脚本
使用vim编辑test.sh 在屏幕上输出 Hello World!
[root@shell ~]
echo "Hello World"
[root@shell ~]
Hello World
父shell和子shell的区别
子shell可以继承父shell的所有变量, /etc/profile变量 所有子shell都可以使用;
使用bash和sh执行脚本,都是在子shell中运行的
执行脚本常用的三种方式
1.使用sh或bash运行脚本(开启了一个子shell运行里面的脚本)
[root@shell ~]
Hello World
[root@shell ~]
Hello World
2.使用全路径的方式运行脚本(前提是要给脚本文件执行权限)
[root@shell ~]
-bash: /root/test.sh: Permission denied
[root@shell ~]
[root@shell ~]
-rwxr-xr-x 1 root root 31 Oct 15 17:03 test.sh
[root@shell ~]
Hello World
3.使用 . 或source在父进程中执行脚本
[root@shell ~]
Hello World
[root@shell ~]
Hello World
[root@shell ~]
/root
[root@shell ~]
Hello World!
/bin/bash
查看执行脚本的过程
sh -x 脚本名
Shell常用的基础变量
查看变量
查看所有的变量,包括环境变量和普通变量
命令:set
只查看全局变量
命令:env
取消定义的变量
命令:unset 变量名
调用变量
命令:${变量名}
$变量名
显示变量值的长度:
命令:${#变量名}
1.什么是变量?
使用一个固定的值代表一个不固定的值
变量的名=变量的值
2.变量的分类
环境变量(全局变量) 所有shell都生效
普通变量(局部变量) 只针对当前的shell生效 自定义的变量
3.按生命周期划分变量
临时生效 只对在当前shell中生效 关闭则失效 使用export直接定义即可
不加export 只对当前的shell生效
加export 对当前窗口的父shell和子shell生效
永久生效 对当前系统所有shell生效 写入/etc/profile
4.环境变量配置文件执行的顺序
1. /etc/profile
读取家目录下所有的环境变量文件
2. .bash_profile
3. .bashrc
4. /etc/bashrc
5.定义环境变量
环境变量的名称定义: 字母数字下划线开头的组合 见名知其意 等号两端不能有空格 不允许数字开头
oldboy_age=18
OLDBOY_AGE=25
oldboy_Age=30
Oldboy_Age=35
变量值的定义: 数字 字符串 和命令
字符串定义:变量值中间不允许有空格 如果有需要加引号
6.Shell脚本中特殊位置变量
$0 ==> 显示执行脚本的名称;如果执行脚本包含了路径,那么就包括脚本的路径
$n ==> 获取当前执行脚本的第n个参数值,n=1..9, 如果n大于9,则用大括号括起来,例:${10}(参数写在命令行)
$
$? ==> 检查上一个命令是否执行成功,(0为成功;非0为失败)
$$ ==> 获取当前执行脚本的进程号(pid)
$! ==> 获取上一个在后台运行脚本的进程号
$_ ==> 获取当前命令行的最后一个参数 类似于esc .
$* ==> 获取脚本传参的所有参数 在循环体中 不加双引号和$@相同 加双引号 把所有的参数当做一个参数
$@ ==> 获取脚本传参的所有参数 在循环体中 不加双引号和$*相同 加双引号 把所有的参数当做独立的
$0 ==> 显示执行脚本的名称;如果执行脚本包含了路径,那么就包括脚本的路径
[root@shell ~]
echo "Hello World"
echo $0
[root@shell ~]
Hello World
test.sh
$n ==> 获取当前执行脚本的第n个参数值,n=1..9, 如果n大于9,则用大括号括起来,例:${10}
参数写在命令行,把命令行中的参数传递到脚本中
[root@shell ~]
echo "Hello World"
echo $1
[root@shell ~]
Hello World
[root@shell ~]
Hello World
hehe
[root@shell ~]
echo "Hello World"
echo $1 $3
[root@shell ~]
Hello World
hehe gg
$
[root@shell ~]
echo "Hello World"
echo $1 $3 ${12}
echo $
[root@shell ~]
Hello World
a c l
26
$$ ==> 获取当前执行脚本的进程号(pid)
[root@shell ~]
echo "Hello World"
echo 当前执行脚本的进程号是:$$
[root@shell ~]
Hello World
当前执行脚本的进程号是:7821
脚本传参的三种方式
直接传参
[root@shell ~]# cat test.sh
#!/bin/bash
echo name=$1
echo age=$2
[root@shell ~]# sh test.sh gaoxinyv 25
name=gaoxinyv
age=25
赋值传参
[root@shell ~]
name=$1
age=$2
echo $name $age
[root@shell ~]
gaoxinyv 25
read传参
read语法:
命令:read 参数 变量名 回车
变量值
-p 用户提示
-t 时间限制
[root@shell ~]
gaoxinyv
[root@shell ~]
gaoxinyv
[root@shell ~]
高新宇 25
[root@shell ~]
高新宇
[root@shell ~]
25
[root@shell ~]
请输入你的姓名:gaoxinyv
[root@shell ~]
gaoxinyv
[root@shell ~]
read -p "请输入你的姓名:" name
echo "你输入的姓名是:$name"
[root@shell ~]
请输入你的姓名:高新宇
你输入的姓名是:高新宇
[root@shell ~]
read -p "请输入你的姓名和年龄:" name age
echo "你输入的姓名是:$name 年龄是: $age"
[root@shell ~]
请输入你的姓名和年龄:高新宇 25
你输入的姓名是:高新宇 年龄是: 25
变量子串的删除及替换
[root@shell ~]
[root@shell ~]
I am gaoxinyv
[root@shell ~]
am
[root@shell ~]
am
这个功能类似于 cut 命令 -c 参数的功能
[root@shell ~]
am
变量子串的删除
%:从后往前匹配
[root@shell ~]
www.baidu.com
[root@shell ~]
baidu.com
[root@shell ~]
baidu.com
[root@shell ~]
com
[root@shell ~]
www.baidu
[root@shell ~]
www.baidu
[root@shell ~]
www
变量子串的替换
[root@shell ~]
www.baidu.com
[root@shell ~]
WWW.baidu.com
[root@shell ~]
Www.baidu.com
[root@shell ~]
WWW.baidu.com
[root@shell ~]
www.sina.com
Shell数值运算
expr
expr 只能做整数运算,不支持小数运算
命令格式: expr 整数1 空格 运算符 空格 整数2
[root@shell ~]
3
[root@shell ~]
-1
[root@shell ~]
expr: syntax error
[root@shell ~]
2
[root@shell ~]
0
[root@shell ~]
2
[root@shell ~]
8
[root@shell ~]
12
$[ ]
$[ ] 只支持整数运算,不支持小数运算
命令格式:$[整数 运算符号 整数]
[root@shell ~]
3
[root@shell ~]
-bash: 1+2.1: syntax error: invalid arithmetic operator (error token is ".1")
[root@shell ~]
-1
[root@shell ~]
2
[root@shell ~]
0
[root@shell ~]
2
[root@shell ~]
100*10=1000
[root@shell ~]
[root@shell ~]
[root@shell ~]
100*10
[root@shell ~]
100*10=1000
$(( ))
$(( )) 只支持整数运算,不支持小数运算 执行效率最高
命令格式:$((整数 运算符号 整数))
[root@shell ~]
3
[root@shell ~]
-bash: 1+1.5: syntax error: invalid arithmetic operator (error token is ".5")
[root@shell ~]
-1
[root@shell ~]
2
[root@shell ~]
0
[root@shell ~]
2
let
let 只支持整数运算,不支持小数运算
语法格式:let 变量名=整数+整数
输出结果:echo $变量名
[root@shell ~]
[root@shell ~]
3
[root@shell ~]
-bash: let: num=1+2.5: syntax error: invalid arithmetic operator (error token is ".5")
[root@shell ~]
[root@shell ~]
-1
[root@shell ~]
[root@shell ~]
2
[root@shell ~]
[root@shell ~]
0
[root@shell ~]
[root@shell ~]
1 1
[root@shell ~]
[root@shell ~]
2 2
[root@shell ~]
[root@shell ~]
3 3
bc
bc 支持整数和小数运算 需要安装 yum -y install bc
语法格式:echo 数字 运算符号 数字|bc
[root@shell ~]# echo 1+2|bc
3
[root@shell ~]# echo 1-2|bc
-1
[root@shell ~]# echo 1*2|bc
2
[root@shell ~]# echo 1*1.5|bc
1.5
[root@shell ~]# echo 1/2|bc
0
[root@shell ~]# echo 4/2|bc
2
[root@shell ~]# echo 4/1.5|bc
2
其他运算
awk python 支持整数和小数运算
awk语法: awk 'BEGIN{print 10*10}'
[root@shell ~]
100
[root@shell ~]
0
[root@shell ~]
1
[root@shell ~]
20
[root@shell ~]
10000000000
[root@shell ~]
100
[root@shell ~]
10000000000
[root@shell ~]
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 10*10
100
>>> 10-10
0
>>> 10-1.5
8.5
>>> 10/10
1
案例
案例1:编写计算器
直接传参的方式
[root@shell ~]
[ $
echo $1+$2=$(($1+$2))
echo $1-$2=$(($1-$2))
echo $1*$2=$(($1*$2))
echo $1/$2=$(($1/$2))
[root@shell ~]
10+5=15
10-5=5
10*5=50
10/5=2
[root@shell ~]
请输入两个数字!
[root@shell ~]
请输入两个数字!
赋值传参的方式
[root@shell ~]
[ $
num1=$1
num2=$2
echo $num1+$num2=