更新 'Shell-MD/shell变量.md'

This commit is contained in:
diandian 2023-04-17 16:10:31 +08:00
parent 698824b1d1
commit 7cc2c0e47e

View File

@ -1,410 +1,410 @@
<h1><center>Shell变量</center></h1> <h1><center>Shell变量</center></h1>
作者:行癫(盗版必究) 作者:行癫(盗版必究)
------ ------
## 一:变量概述 ## 一:变量概述
#### 1.什么是变量 #### 1.什么是变量
变量来源于数学,是计算机语言中能储存计算结果或能表示值的抽象概念 变量来源于数学,是计算机语言中能储存计算结果或能表示值的抽象概念
变量可以通过变量名访问,在指令式语言中,变量通常是可变的;在某些条件下也是不可变的 变量可以通过变量名访问,在指令式语言中,变量通常是可变的;在某些条件下也是不可变的
#### 2.变量的规则 #### 2.变量的规则
命名只能使用英文字母,数字和下划线,首个字符不能以数字开头 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头
中间不能有空格,可以使用下划线 中间不能有空格,可以使用下划线
不能使用标点符号 不能使用标点符号
不能使用bash里的关键字 不能使用bash里的关键字
## 二:变量分类 ## 二:变量分类
#### 1.自定义变量 #### 1.自定义变量
定义变量:变量名=变量值 例如xingdian=123 定义变量:变量名=变量值 例如xingdian=123
引用变量:$变量名 或 ${变量名} 引用变量:$变量名 或 ${变量名}
查看变量echo $变量名 查看变量echo $变量名
取消变量unset 变量名 取消变量unset 变量名
作用范围仅在当前shell中有效 作用范围仅在当前shell中有效
#### 2.环境变量 #### 2.环境变量
定义环境变量: 定义环境变量:
方法一 export back_dir2=/home/backup 方法一 export back_dir2=/home/backup
方法二 export back_dir1 将自定义变量转换成环境变量 方法二 export back_dir1 将自定义变量转换成环境变量
引用环境变量:$变量名 或 ${变量名} 引用环境变量:$变量名 或 ${变量名}
查看环境变量echo $变量名 查看环境变量echo $变量名
取消环境变量unset 变量名 取消环境变量unset 变量名
变量作用范围在当前shell和子shell有效 变量作用范围在当前shell和子shell有效
注意: 注意:
环境变量拥有可继承性export之后就拥有继承性 环境变量拥有可继承性export之后就拥有继承性
永久生效:写到环境变量脚本,/etc/profile ~/.baserc ~/.bash_profile /etc/bashrc 永久生效:写到环境变量脚本,/etc/profile ~/.baserc ~/.bash_profile /etc/bashrc
案例: 案例:
```shell ```shell
[root@xingdiancloud ~]# vim /etc/profile [root@xingdiancloud ~]# vim /etc/profile
JAVA_HOME=/usr/local/java JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH export JAVA_HOME PATH
``` ```
```shell ```shell
[root@xingdiancloud ~]# vim ~/.bash_profile (只显示部分) [root@xingdiancloud ~]# vim ~/.bash_profile (只显示部分)
PATH=$PATH:$HOME/bin:/usr/local/mycat/bin PATH=$PATH:$HOME/bin:/usr/local/mycat/bin
``` ```
/etc/profile /etc/profile
这是系统最主要的shell设置文件也是用户登陆时系统最先检查的文件有关重要的环境变量都定义在此其中包括PATH,USER,LOGNAME,MAIL,HOSTNAME,HISTSIZE,INPUTRC等。而在文件的最后它会检查并执行/etc/profile.d/*.sh的脚本 这是系统最主要的shell设置文件也是用户登陆时系统最先检查的文件有关重要的环境变量都定义在此其中包括PATH,USER,LOGNAME,MAIL,HOSTNAME,HISTSIZE,INPUTRC等。而在文件的最后它会检查并执行/etc/profile.d/*.sh的脚本
~/.bash_profile ~/.bash_profile
这个文件是每位用户的bash环境设置文件它存在与于用户的主目录中当系统执行/etc/profile 后就会接着读取此文件内的设置值。在此文件中会定义USERNAME,BASH_ENV和PATH等环境变量但是此处PATH除了包含系统的$PATH变量外加入用户的“bin”目录路径 这个文件是每位用户的bash环境设置文件它存在与于用户的主目录中当系统执行/etc/profile 后就会接着读取此文件内的设置值。在此文件中会定义USERNAME,BASH_ENV和PATH等环境变量但是此处PATH除了包含系统的$PATH变量外加入用户的“bin”目录路径
~/.bashrc ~/.bashrc
接下来系统会检查~.bashrc文件这个文件和前两个文件/etc/profile 和~.bash_profile最大的不同是每次执行bash时~.bashrc都会被再次读取也就是变量会再次地设置而/etc/profile,~./bash_profile只有在登陆时才读取。就是因为要经常的读取所以~/.bashrc文件只定义一些终端机设置以及shell提示符号等功能而不是定义环境变量 接下来系统会检查~.bashrc文件这个文件和前两个文件/etc/profile 和~.bash_profile最大的不同是每次执行bash时~.bashrc都会被再次读取也就是变量会再次地设置而/etc/profile,~./bash_profile只有在登陆时才读取。就是因为要经常的读取所以~/.bashrc文件只定义一些终端机设置以及shell提示符号等功能而不是定义环境变量
~/.bash_login ~/.bash_login
如果~/.bash_profile文件不存在则系统会转而读取~/.bash_login这个文件内容。这是用户的登陆文件在每次用户登陆系统时bash都会读此内容所以通常都会将登陆后必须执行的命令放在这个文件中 如果~/.bash_profile文件不存在则系统会转而读取~/.bash_login这个文件内容。这是用户的登陆文件在每次用户登陆系统时bash都会读此内容所以通常都会将登陆后必须执行的命令放在这个文件中
.profile .profile
如果~./bash_profile ~./bash_login两个文件都不存在则会使用这个文件的设置内容其实它的功能与~/.bash_profile相同 如果~./bash_profile ~./bash_login两个文件都不存在则会使用这个文件的设置内容其实它的功能与~/.bash_profile相同
.bash_logout .bash_logout
如果想在注销shell前执行一些工作都可以在此文件中设置 如果想在注销shell前执行一些工作都可以在此文件中设置
```shell ```shell
[root@xingdiancloud ~]# vi ~.bash_logout [root@xingdiancloud ~]# vi ~.bash_logout
clear clear
仅执行一个clear命令在你注销的时候 仅执行一个clear命令在你注销的时候
``` ```
~/.bash_history ~/.bash_history
这个文件会记录用户先前使用的历史命令 这个文件会记录用户先前使用的历史命令
注意: 注意:
在/etc/profile.d建立独立的环境变量配置文件 在/etc/profile.d建立独立的环境变量配置文件
常用环境变量USER UID HOME HOSTNAME PWD PATH 常用环境变量USER UID HOME HOSTNAME PWD PATH
PATH这个变量存放的是所有命令所在的路径 修改PATH=$PATH:+目录 PATH这个变量存放的是所有命令所在的路径 修改PATH=$PATH:+目录
#### 3.位置变量 #### 3.位置变量
$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
案例: 案例:
```shell ```shell
[root@xingdiancloud sh]# cat xingdian.sh [root@xingdiancloud sh]# cat xingdian.sh
#!/bin/bash #!/bin/bash
echo "hello $1" echo "hello $1"
[root@xingdiancloud sh]# bash xingdian.sh xingdian [root@xingdiancloud sh]# bash xingdian.sh xingdian
hello xingdian hello xingdian
``` ```
4.预定义变量 4.预定义变量
```shell ```shell
$0 脚本名 $0 脚本名
$* 所有的参数 $* 所有的参数
$@ 所有的参数 $@ 所有的参数
$# 参数的个数 $# 参数的个数
$$ 当前进程的PID $$ 当前进程的PID
$! 上一个后台进程的PID $! 上一个后台进程的PID
$? 上一个命令的返回值 0表示成功 $? 上一个命令的返回值 0表示成功
``` ```
案例: 案例:
```shell ```shell
[root@xingdiancloud sh]# cat test.sh [root@xingdiancloud sh]# cat test.sh
#!/bin/bash #!/bin/bash
echo "第2个位置参数是$2" echo "第2个位置参数是$2"
echo "第1个位置参数是$1" echo "第1个位置参数是$1"
echo "第4个位置参数是$4" echo "第4个位置参数是$4"
echo "所有参数是: $*" echo "所有参数是: $*"
echo "所有参数是: $@" echo "所有参数是: $@"
echo "参数的个数是: $#" echo "参数的个数是: $#"
echo "当前进程的PID是: $$" echo "当前进程的PID是: $$"
echo '$1='$1 echo '$1='$1
echo '$2='$2 echo '$2='$2
echo '$3='$3 echo '$3='$3
echo '$*='$* echo '$*='$*
echo '$@='$@ echo '$@='$@
echo '$#='$# echo '$#='$#
echo '$$='$$ echo '$$='$$
``` ```
## 三:变量赋值 ## 三:变量赋值
#### 1.显示赋值 #### 1.显示赋值
变量名=变量值 变量名=变量值
示例: 示例:
```shell ```shell
[root@xingdiancloud ~]# ip1=192.168.1.251 [root@xingdiancloud ~]# ip1=192.168.1.251
[root@xingdiancloud ~]# school="BeiJing 1000phone" [root@xingdiancloud ~]# school="BeiJing 1000phone"
[root@xingdiancloud ~]# today1=`date +%F` [root@xingdiancloud ~]# today1=`date +%F`
[root@xingdiancloud ~]# today2=$(date +%F) [root@xingdiancloud ~]# today2=$(date +%F)
``` ```
#### 2.键盘读入 #### 2.键盘读入
```shell ```shell
read 变量名 read 变量名
read -p "提示信息: " 变量名 read -p "提示信息: " 变量名
read -t 5 -p "提示信息: " 变量名 -t 后面跟秒数,定义输入字符的等待时间 read -t 5 -p "提示信息: " 变量名 -t 后面跟秒数,定义输入字符的等待时间
read -n 2 变量名 -n 后跟一个数字,定义输入文本的长度,很实用。 read -n 2 变量名 -n 后跟一个数字,定义输入文本的长度,很实用。
``` ```
案例1 案例1
```shell ```shell
[root@xingdiancloud ~]# vim first.sh [root@xingdiancloud ~]# vim first.sh
back_dir1=/var/backup back_dir1=/var/backup
read -p "请输入你的备份目录: " back_dir2 read -p "请输入你的备份目录: " back_dir2
echo $back_dir1 echo $back_dir1
echo $back_dir2 echo $back_dir2
[root@xingdiancloud ~]# sh first.sh [root@xingdiancloud ~]# sh first.sh
``` ```
案例2 案例2
```shell ```shell
[root@xingdiancloud ~]# vim ping2.sh [root@xingdiancloud ~]# vim ping2.sh
#!/bin/bash #!/bin/bash
read -p "Input IP: " ip read -p "Input IP: " ip
ping -c2 $ip &>/dev/null ping -c2 $ip &>/dev/null
if [ $? = 0 ];then if [ $? = 0 ];then
echo "host $ip is ok" echo "host $ip is ok"
else else
echo "host $ip is fail" echo "host $ip is fail"
fi fi
[root@xingdiancloud ~]# chmod a+x ping2.sh [root@xingdiancloud ~]# chmod a+x ping2.sh
[root@xingdiancloud ~]# ./ping.sh [root@xingdiancloud ~]# ./ping.sh
``` ```
注意:定义或引用变量时注意事项 注意:定义或引用变量时注意事项
" " 弱引用 可以实现变量和命令的替换 " " 弱引用 可以实现变量和命令的替换
' ' 强引用 不完成变量替换 ' ' 强引用 不完成变量替换
反引 命令替换 等价于 $() 反引号中的shell命令会被先执行 反引 命令替换 等价于 $() 反引号中的shell命令会被先执行
```shell ```shell
[root@xingdiancloud ~]# school=1000phone [root@xingdiancloud ~]# school=1000phone
[root@xingdiancloud ~]# echo "${school} is good" [root@xingdiancloud ~]# echo "${school} is good"
1000phone is good 1000phone is good
[root@xingdiancloud ~]# echo '${school} is good' [root@xingdiancloud ~]# echo '${school} is good'
${school} is good ${school} is good
[root@xingdiancloud ~]# touch `date +%F`_file1.txt [root@xingdiancloud ~]# touch `date +%F`_file1.txt
[root@xingdiancloud ~]# touch $(date +%F)_file2.txt [root@xingdiancloud ~]# touch $(date +%F)_file2.txt
[root@xingdiancloud ~]# disk_free3="df -Ph |grep '/$' |awk '{print $4}'" 错误 [root@xingdiancloud ~]# disk_free3="df -Ph |grep '/$' |awk '{print $4}'" 错误
[root@xingdiancloud ~]# disk_free4=$(df -Ph |grep '/$' |awk '{print $4}') [root@xingdiancloud ~]# disk_free4=$(df -Ph |grep '/$' |awk '{print $4}')
[root@xingdiancloud ~]# disk_free5=`df -Ph |grep '/$' |awk '{print $4} [root@xingdiancloud ~]# disk_free5=`df -Ph |grep '/$' |awk '{print $4}
``` ```
## 四:变量运算 ## 四:变量运算
#### 1.整数运算 #### 1.整数运算
方法一expr 方法一expr
```shell ```shell
[root@xingdiancloud ~]# expr 1 + 2 [root@xingdiancloud ~]# expr 1 + 2
[root@xingdiancloud ~]# expr $num1 + $num2 + - \* / % [root@xingdiancloud ~]# expr $num1 + $num2 + - \* / %
``` ```
方法二:$(()) 方法二:$(())
```shell ```shell
[root@xingdiancloud ~]# echo $(($num1+$num2)) + - * / % [root@xingdiancloud ~]# echo $(($num1+$num2)) + - * / %
[root@xingdiancloud ~]# echo $((num1+num2)) [root@xingdiancloud ~]# echo $((num1+num2))
[root@xingdiancloud ~]# echo $((5-3*2)) [root@xingdiancloud ~]# echo $((5-3*2))
[root@xingdiancloud ~]# echo $(((5-3)*2)) [root@xingdiancloud ~]# echo $(((5-3)*2))
[root@xingdiancloud ~]# echo $((2**3)) [root@xingdiancloud ~]# echo $((2**3))
[root@xingdiancloud ~]# sum=$((1+2)); echo $sum [root@xingdiancloud ~]# sum=$((1+2)); echo $sum
``` ```
方法三:$[] 方法三:$[]
```shell ```shell
[root@xingdiancloud ~]# echo $[5+2] + - * / % [root@xingdiancloud ~]# echo $[5+2] + - * / %
[root@xingdiancloud ~]# echo $[5**2] [root@xingdiancloud ~]# echo $[5**2]
``` ```
方法四let 方法四let
``` ```
[root@xingdiancloud ~]# let sum=2+3; echo $sum [root@xingdiancloud ~]# let sum=2+3; echo $sum
[root@xingdiancloud ~]# let i++; echo $i [root@xingdiancloud ~]# let i++; echo $i
``` ```
#### 2.小数运算 #### 2.小数运算
使用bc做小数运算scale指定小数点位数 使用bc做小数运算scale指定小数点位数
加法运算(scale参数无效) 加法运算(scale参数无效)
```shell ```shell
[root@xingdiancloud ~]# echo "5.999 + 5.001"|bc [root@xingdiancloud ~]# echo "5.999 + 5.001"|bc
6.000 6.000
[root@xingdiancloud ~]# echo "5.111+ 5.1114"|bc [root@xingdiancloud ~]# echo "5.111+ 5.1114"|bc
10.2224 10.2224
``` ```
减法运算(scale参数无效) 减法运算(scale参数无效)
```shell ```shell
[root@xingdiancloud ~]# echo "2.22 - 1.11"|bc [root@xingdiancloud ~]# echo "2.22 - 1.11"|bc
1.11 1.11
``` ```
乘法运算 乘法运算
```shell ```shell
[root@xingdiancloud ~]# echo "5.12 * 5.6000"|bc [root@xingdiancloud ~]# echo "5.12 * 5.6000"|bc
28.6720 28.6720
``` ```
注意乘积小数点位数默认以乘数中小数点位数最多的为准不指定scale参数 注意乘积小数点位数默认以乘数中小数点位数最多的为准不指定scale参数
除法运算 除法运算
```shell ```shell
[root@xingdiancloud ~]# echo "scale=2;9.898 / 1.11"|bc [root@xingdiancloud ~]# echo "scale=2;9.898 / 1.11"|bc
8.91 8.91
[root@xingdiancloud ~]# echo "9.898 / 1.11"|bc [root@xingdiancloud ~]# echo "9.898 / 1.11"|bc
8 8
``` ```
## 五:扩展 ## 五:扩展
#### 1.内容的删除 #### 1.内容的删除
案例一 案例一
```shell ```shell
[root@xingdian ~]# url=www.sina.com.cn [root@xingdian ~]# url=www.sina.com.cn
[root@xingdian ~]# echo ${#url} 获取变量值的长度 [root@xingdian ~]# echo ${#url} 获取变量值的长度
15 15
[root@xingdian ~]# echo ${url} 标准查看 [root@xingdian ~]# echo ${url} 标准查看
www.sina.com.cn www.sina.com.cn
[root@xingdian ~]# echo ${url#*.} 从前往后,最短匹配 [root@xingdian ~]# echo ${url#*.} 从前往后,最短匹配
sina.com.cn sina.com.cn
[root@xingdian ~]# echo ${url##*.} 从前往后,最长匹配 贪婪匹配 [root@xingdian ~]# echo ${url##*.} 从前往后,最长匹配 贪婪匹配
cn cn
[root@xingdian ~]# url=www.sina.com.cn [root@xingdian ~]# url=www.sina.com.cn
[root@xingdian ~]# echo ${url#a.} [root@xingdian ~]# echo ${url#a.}
www.sina.com.cn www.sina.com.cn
[root@xingdian ~]# echo ${url#*sina.} [root@xingdian ~]# echo ${url#*sina.}
com.cn com.cn
``` ```
案例二 案例二
```shell ```shell
[root@xingdian ~]# url=www.sina.com.cn [root@xingdian ~]# url=www.sina.com.cn
[root@xingdian ~]# echo ${url} [root@xingdian ~]# echo ${url}
www.sina.com.cn www.sina.com.cn
[root@xingdian ~]# echo ${url%.*} 从后往前,最短匹配 [root@xingdian ~]# echo ${url%.*} 从后往前,最短匹配
www.sina.com www.sina.com
[root@xingdian ~]# echo ${url%%.*} 从后往前,最长匹配 贪婪匹配 [root@xingdian ~]# echo ${url%%.*} 从后往前,最长匹配 贪婪匹配
www www
[root@xingdian ~]# echo $HOSTNAME [root@xingdian ~]# echo $HOSTNAME
xingdian.1000phone.com xingdian.1000phone.com
[root@xingdian ~]# echo ${HOSTNAME%%.*} [root@xingdian ~]# echo ${HOSTNAME%%.*}
xingdian xingdian
``` ```
#### 2.索引及切片 #### 2.索引及切片
```shell ```shell
[root@xingdian ~]# echo ${url:0:5} [root@xingdian ~]# echo ${url:0:5}
0:从头开始 0:从头开始
5:到第五个 5:到第五个
[root@xingdian ~]# echo ${url:5:5} [root@xingdian ~]# echo ${url:5:5}
[root@xingdian ~]# echo ${url:5} [root@xingdian ~]# echo ${url:5}
``` ```
#### 3.变量内容替换 #### 3.变量内容替换
```shell ```shell
[root@xingdian ~]# url=www.sina.com.cn [root@xingdian ~]# url=www.sina.com.cn
[root@xingdian ~]# echo ${url/sina/baidu} [root@xingdian ~]# echo ${url/sina/baidu}
www.baidu.com.cn www.baidu.com.cn
[root@xingdian ~]# url=www.sina.com.cn [root@xingdian ~]# url=www.sina.com.cn
[root@xingdian ~]# echo ${url/n/N} [root@xingdian ~]# echo ${url/n/N}
www.siNa.com.cn www.siNa.com.cn
[root@xingdian ~]# echo ${url//n/N} 贪婪匹配 [root@xingdian ~]# echo ${url//n/N} 贪婪匹配
www.siNa.com.cN www.siNa.com.cN
``` ```
#### 4.自增运算 #### 4.自增运算
对变量的值的影响 对变量的值的影响
```shell ```shell
[root@xingdian ~]# i=1 [root@xingdian ~]# i=1
[root@xingdian ~]# let i++ [root@xingdian ~]# let i++
[root@xingdian ~]# echo $i [root@xingdian ~]# echo $i
2 2
[root@xingdian ~]# j=1 [root@xingdian ~]# j=1
[root@xingdian ~]# let ++j [root@xingdian ~]# let ++j
[root@xingdian ~]# echo $j [root@xingdian ~]# echo $j
2 2
``` ```
对表达式的值的影响 对表达式的值的影响
```shell ```shell
[root@xingdian ~]# unset i [root@xingdian ~]# unset i
[root@xingdian ~]# unset j [root@xingdian ~]# unset j
[root@xingdian ~]# [root@xingdian ~]#
[root@xingdian ~]# i=1 [root@xingdian ~]# i=1
[root@xingdian ~]# j=1 [root@xingdian ~]# j=1
[root@xingdian ~]# [root@xingdian ~]#
[root@xingdian ~]# let x=i++ 先赋值,再运算 [root@xingdian ~]# let x=i++ 先赋值,再运算
[root@xingdian ~]# let y=++j 先运算,再赋值 [root@xingdian ~]# let y=++j 先运算,再赋值
[root@xingdian ~]# [root@xingdian ~]#
[root@xingdian ~]# echo $i [root@xingdian ~]# echo $i
2 2
[root@xingdian ~]# echo $j [root@xingdian ~]# echo $j
2 2
[root@xingdian ~]# [root@xingdian ~]#
[root@xingdian ~]# echo $x [root@xingdian ~]# echo $x
1 1
[root@xingdian ~]# echo $y [root@xingdian ~]# echo $y
2 2
``` ```