diff --git a/Shell-MD/shell变量.md b/Shell-MD/shell变量.md index cc12e33..55fa872 100644 --- a/Shell-MD/shell变量.md +++ b/Shell-MD/shell变量.md @@ -1,410 +1,410 @@ -

Shell变量

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

Shell变量

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