Shell编程变量

Shell编程变量

  • 变量是什么
变量是用来传递数据的一种方式
简单来讲:用一个固定的字符串去表示一个不固定的内容,便于后期引用
变量名的规范

采用驼峰法书写

变量的名称不要跟系统中的命令或者变量有冲突

名称要求: 字母、数字、下划线组成 尽量使用字母开头,变量名称最好具有一定的含义

前面是变量名称 = 等号是赋值 后面的就是定义的值 等号两边不能存在空格。
  • 如何定义变量有哪几种方式
1、用户自定义变量: 用户自己定义的变量

2、系统环境变量: 系统已经定义好的变量,变量记录的是操作系统的一些信息 全部是由大写字母组成的

全局变量 和 局部变量

3、位置参数变量: 向脚本中进行传递参数,变量名不能自己定义,变量的作用也是固定的 $1 $2

4、预定义变量: bash已经定义好的变量,变量名不能自己定义,变量的作用也是固定的,值不是自己设置的 $@ $* $? $0
  • 定义变量——实践

1. 用户自定义变量,只在当前环境生效  

#定义变量  
[root@shell ~]# Name=qls
[root@shell ~]# Name=qls qiudao		#变量值出现空格时,使用引号引用起来
-bash: qiudao: command not found
[root@shell ~]# Name='qls qiudao'
[root@shell ~]# Name-1=qls			#变量名不能出现短横岗
-bash: Name-1=qls: command not found
[root@shell ~]# Name1=qls
[root@shell ~]# 1Name=qls			#不能以数字开头
-bash: 1Name=qls: command not found


#引用变量
[root@shell ~]# echo $Name
qls qiudao
[root@shell ~]# echo ${Name}
qls qiudao
[root@shell ~]# echo $Nameage

[root@shell ~]# echo $Name_age

[root@shell ~]# echo ${Name}_age
qls qiudao_age
[root@shell ~]# Num=10
[root@shell ~]# echo $Num%
10%


#查看所有的变量

#显示局部变量 
[root@shell ~]# set | grep Name
Name='qls qiudao'

#显示全局变量和进程正在运行中的变量     显示系统的环境变量 
[root@shell ~]# env | grep Name
[root@shell ~]# echo $USER
root

#取消变量

[root@shell ~]# Name=qls
[root@shell ~]# 
[root@shell ~]# unset  Name
[root@shell ~]# echo $Name


单双引号和不加引号及反引号的区别

反引号,先执行反引号里面的命令,将执行的结果交给外面的命令  里面必须是命令
[root@shell ~]# Name=$(hostname)
[root@shell ~]# echo $Name
shell
[root@shell ~]# Name=`hostname`
[root@shell ~]# echo $Name
shell

单引号  强引用  单引号里面是什么,得到的也是什么  
[root@shell ~]# Name='$(hostname)'
[root@shell ~]# echo $Name
$(hostname)

双引号  弱引用  里面是什么内容,也会得到什么内容  但是他会解析变量  解析特殊字符
[root@shell ~]# Name="$(hostname)"
[root@shell ~]# echo $Name
shell


不加引号   当变量的值出现空格时,不会将值看做是一个整体,不支持通配符 

[root@shell ~]# Name=qls qiudao
-bash: qiudao: command not found

[root@shell ~]# Name={1..10}
[root@shell ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@shell ~]# echo $Name
{1..10}


[root@shell ~]# Name=hj
[root@shell ~]# echo $Name
hj
[root@shell ~]# echo $Name money is $10000000000
hj money is 0000000000
[root@shell ~]# echo "$Name money is $10000000000"
hj money is 0000000000
[root@shell ~]# echo '$Name money is $10000000000'
$Name money is $10000000000
[root@shell ~]# echo $Name money is '$10000000000'
hj money is $10000000000
[root@shell ~]# echo "$Name money is \$10000000000"
hj money is $10000000000
[root@shell ~]# echo $Name money is \$10000000000
hj money is $10000000000


系统环境变量  全局生效的 

如何自定义环境变量 ,在当前环境下和子shell环境下生效

[root@shell ~]# Name=qls
[root@shell ~]# echo $Name
qls
[root@shell ~]# vim test.sh 
[root@shell ~]# cat test.sh
#!/bin/bash
echo $Name
[root@shell ~]# sh test.sh

[root@shell ~]# export Name=qls
[root@shell ~]# echo $Name
qls
[root@shell ~]# sh test.sh 
qls

使用export命令只能在当前环境下和当前环境下的子shell环境生效
只有将变量写入到环境变量配置文件中,才会全局生效  当前用户和所有用户生效 


[root@shell ~]# echo $USER
root
[root@shell ~]# echo $UID
0
[root@shell ~]# echo $HOME
/root
[root@shell ~]# echo $HOSTNAME
shell
[root@shell ~]# echo $PWD
/root
[root@shell ~]# cd /opt/
[root@shell /opt]# echo $PWD
/opt
[root@shell /opt]# echo $SHELL
/bin/bash

位置变量   

[root@shell ~]# cat test.sh
#!/bin/bash
echo $1 $2 $4
[root@shell ~]# sh test.sh

[root@shell ~]# sh test.sh 1 2 3 
1 2
[root@shell ~]# sh test.sh 1 2 3 4
1 2 4

$1  $2  $3 ..  $N    ${10}   位置变量出现两位数的时候,需要使用花括号括起来  


预定义的变量  

$0   $*   $@  $#  $?  $$ 

[root@shell /service/scripts/day01]# cat test.sh
#!/bin/bash
echo "当前Shell的脚本文件名为:$0"
echo "当前Shell运行的PID号为:$$"
echo "当前Shell的第一个位置变量为:$1"
echo "当前Shell的第二个位置变量为:$2"
echo "当前Shell的第三个位置变量为:$3"
echo "当前传递的所有位置参数为:$*"
echo "当前传递的所有位置参数为:$@"
echo "当前总共传递的位置参数的个数为:$#"
echo "上条命令的执行结果为:$?"
[root@shell /service/scripts/day01]# sh /service/scripts/day01/test.sh  linux  nginx php
当前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]# cat test1.sh
#!/bin/bash
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]# sh test1.sh
未加引号,二者是相同的
linux nginx mysql
linux nginx mysql
加入引号对比
linux nginx mysql
###################
linux
nginx
mysql


没有加入引号之前,两者是相同的

加入引号之后呢

$*  把参数作为一个字符串整体进行输出返回

$@  把每个参数作为一个一个字符串返回


[root@shell /service/scripts/day01]# echo $(date +%F)
2020-04-20

[root@shell /service/scripts/day01]# touch  qls{1..9}.txt
[root@shell /service/scripts/day01]# ll
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]# tar czf  qls.tar.gz  *.txt
[root@shell /service/scripts/day01]# ll
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]# tar cvzf  qls.tar.gz  *.txt
qls1.txt
qls2.txt
qls3.txt
qls4.txt
qls5.txt
qls6.txt
qls7.txt
qls8.txt
qls9.txt
[root@shell /service/scripts/day01]# File=$(tar czf qls.tar.gz  $(find  ./ -name "*.txt"))
[root@shell /service/scripts/day01]# 
[root@shell /service/scripts/day01]# 
[root@shell /service/scripts/day01]# echo $File

[root@shell /service/scripts/day01]# tar czf qls.tar.gz  $(find  ./ -name "*.txt")
[root@shell /service/scripts/day01]# ll
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]# File=$(tar czvf qls.tar.gz  $(find  ./ -name "*.txt"))
[root@shell /service/scripts/day01]# echo $File
./qls1.txt ./qls2.txt ./qls3.txt ./qls4.txt ./qls5.txt ./qls6.txt ./qls7.txt ./qls8.txt ./qls9.txt



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值