Shell编程变量
变量是用来传递数据的一种方式
简单来讲:用一个固定的字符串去表示一个不固定的内容,便于后期引用
变量名的规范
采用驼峰法书写
变量的名称不要跟系统中的命令或者变量有冲突
名称要求: 字母、数字、下划线组成 尽量使用字母开头,变量名称最好具有一定的含义
前面是变量名称 = 等号是赋值 后面的就是定义的值 等号两边不能存在空格。
1、用户自定义变量: 用户自己定义的变量
2、系统环境变量: 系统已经定义好的变量,变量记录的是操作系统的一些信息 全部是由大写字母组成的
全局变量 和 局部变量
3、位置参数变量: 向脚本中进行传递参数,变量名不能自己定义,变量的作用也是固定的 $1 $2
4、预定义变量: bash已经定义好的变量,变量名不能自己定义,变量的作用也是固定的,值不是自己设置的 $@ $* $? $0
1. 用户自定义变量,只在当前环境生效
[root@shell ~]
[root@shell ~]
-bash: qiudao: command not found
[root@shell ~]
[root@shell ~]
-bash: Name-1=qls: command not found
[root@shell ~]
[root@shell ~]
-bash: 1Name=qls: command not found
[root@shell ~]
qls qiudao
[root@shell ~]
qls qiudao
[root@shell ~]
[root@shell ~]
[root@shell ~]
qls qiudao_age
[root@shell ~]
[root@shell ~]
10%
[root@shell ~]
Name='qls qiudao'
[root@shell ~]
[root@shell ~]
root
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
单双引号和不加引号及反引号的区别
反引号,先执行反引号里面的命令,将执行的结果交给外面的命令 里面必须是命令
[root@shell ~]
[root@shell ~]
shell
[root@shell ~]
[root@shell ~]
shell
单引号 强引用 单引号里面是什么,得到的也是什么
[root@shell ~]
[root@shell ~]
$(hostname)
双引号 弱引用 里面是什么内容,也会得到什么内容 但是他会解析变量 解析特殊字符
[root@shell ~]
[root@shell ~]
shell
不加引号 当变量的值出现空格时,不会将值看做是一个整体,不支持通配符
[root@shell ~]
-bash: qiudao: command not found
[root@shell ~]
[root@shell ~]
1 2 3 4 5 6 7 8 9 10
[root@shell ~]
{1..10}
[root@shell ~]
[root@shell ~]
hj
[root@shell ~]
hj money is 0000000000
[root@shell ~]
hj money is 0000000000
[root@shell ~]
$Name money is $10000000000
[root@shell ~]
hj money is $10000000000
[root@shell ~]
hj money is $10000000000
[root@shell ~]
hj money is $10000000000
系统环境变量 全局生效的
如何自定义环境变量 ,在当前环境下和子shell环境下生效
[root@shell ~]
[root@shell ~]
qls
[root@shell ~]
[root@shell ~]
echo $Name
[root@shell ~]
[root@shell ~]
[root@shell ~]
qls
[root@shell ~]
qls
使用export命令只能在当前环境下和当前环境下的子shell环境生效
只有将变量写入到环境变量配置文件中,才会全局生效 当前用户和所有用户生效
[root@shell ~]
root
[root@shell ~]
0
[root@shell ~]
/root
[root@shell ~]
shell
[root@shell ~]
/root
[root@shell ~]
[root@shell /opt]
/opt
[root@shell /opt]
/bin/bash
位置变量
[root@shell ~]
echo $1 $2 $4
[root@shell ~]
[root@shell ~]
1 2
[root@shell ~]
1 2 4
$1 $2 $3 .. $N ${10} 位置变量出现两位数的时候,需要使用花括号括起来
预定义的变量
$0 $* $@ $# $? $$
[root@shell /service/scripts/day01]
echo "当前Shell的脚本文件名为:$0"
echo "当前Shell运行的PID号为:$$"
echo "当前Shell的第一个位置变量为:$1"
echo "当前Shell的第二个位置变量为:$2"
echo "当前Shell的第三个位置变量为:$3"
echo "当前传递的所有位置参数为:$*"
echo "当前传递的所有位置参数为:$@"
echo "当前总共传递的位置参数的个数为:$#"
echo "上条命令的执行结果为:$?"
[root@shell /service/scripts/day01]
当前Shell的脚本文件名为:/service/scripts/day01/test.sh
当前Shell运行的PID号为:8932
当前Shell的第一个位置变量为:linux
当前Shell的第二个位置变量为:nginx
当前Shell的第三个位置变量为:php
当前传递的所有位置参数为:linux nginx php
当前传递的所有位置参数为:linux nginx php
当前总共传递的位置参数的个数为:3
上条命令的执行结果为:0
[root@shell /service/scripts/day01]
test() {
echo "未加引号,二者是相同的"
echo $*
echo $@
echo "加入引号对比"
for i in "$*"
do
echo $i
done
echo '###################'
for j in "$@"
do
echo $j
done
}
test linux nginx mysql
[root@shell /service/scripts/day01]
未加引号,二者是相同的
linux nginx mysql
linux nginx mysql
加入引号对比
linux nginx mysql
linux
nginx
mysql
没有加入引号之前,两者是相同的
加入引号之后呢
$* 把参数作为一个字符串整体进行输出返回
$@ 把每个参数作为一个一个字符串返回
[root@shell /service/scripts/day01]
2020-04-20
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
total 8
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls1.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls2.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls3.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls4.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls5.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls6.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls7.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls8.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls9.txt
-rw-r--r-- 1 root root 280 2020-04-20 16:11 test1.sh
-rw-r--r-- 1 root root 451 2020-04-20 16:03 test.sh
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
total 12
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls1.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls2.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls3.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls4.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls5.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls6.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls7.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls8.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls9.txt
-rw-r--r-- 1 root root 177 2020-04-20 16:19 qls.tar.gz
-rw-r--r-- 1 root root 280 2020-04-20 16:11 test1.sh
-rw-r--r-- 1 root root 451 2020-04-20 16:03 test.sh
[root@shell /service/scripts/day01]
qls1.txt
qls2.txt
qls3.txt
qls4.txt
qls5.txt
qls6.txt
qls7.txt
qls8.txt
qls9.txt
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
total 12
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls1.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls2.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls3.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls4.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls5.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls6.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls7.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls8.txt
-rw-r--r-- 1 root root 0 2020-04-20 16:19 qls9.txt
-rw-r--r-- 1 root root 180 2020-04-20 16:21 qls.tar.gz
-rw-r--r-- 1 root root 280 2020-04-20 16:11 test1.sh
-rw-r--r-- 1 root root 451 2020-04-20 16:03 test.sh
[root@shell /service/scripts/day01]
[root@shell /service/scripts/day01]
./qls1.txt ./qls2.txt ./qls3.txt ./qls4.txt ./qls5.txt ./qls6.txt ./qls7.txt ./qls8.txt ./qls9.txt