• shell脚本的变量


    一、系统预定义变量

    1.常用系统变量 $HOME、$PWD、$SHELL、$USER

    查看系统变量的值

    1. [root@hadoop scripts]# echo $HOME
    2. /root

    查看全局环境变量 命令env

    1. [root@hadoop scripts]# env
    2. SPARK_HOME=/usr/soft/spark
    3. HOSTNAME=hadoop
    4. SELINUX_ROLE_REQUESTED=
    5. TERM=xterm
    6. SHELL=/bin/bash
    7. HADOOP_HOME=/usr/soft/hadoop
    8. HISTSIZE=1000

    查看系统全局变量 命令printenv

    [root@hadoop scripts]# printenv
    

    查看局部变量用printenv命令 直接打名称

    1. [root@hadoop scripts]# printenv USER
    2. root

     命令env和printenv只是打印当前环境变量信息的命令,

    显示当前 Shell 中所有变量:set

    [root@hadoop scripts]# set
    

    二、自定义变量

    基本语法

    (1)定义变量:变量名=变量值,注意,=号前后不能有空格

    (2)撤销变量:unset 变量名

    (3)声明静态变量:readonly 变量,注意:不能 unset

    变量定义规则

    (1)变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建 议大写。 (2)等号两侧不能有空格

    (3)在 bash 中,变量默认类型都是字符串类型,无法直接进行数值运算。

    (4)变量的值如果有空格,需要使用双引号或单引号括起来。

    变量赋值 

    1. [root@hadoop scripts]# a=2
    2. [root@hadoop scripts]# echo $a
    3. 2
    4. [root@hadoop scripts]# echo $my_var
    5. [root@hadoop scripts]# my_var=hello
    6. [root@hadoop scripts]# echo $my_var
    7. hello

    赋值后更改 

    1. [root@hadoop scripts]# my_var=Hello
    2. [root@hadoop scripts]# echo $my_var
    3. Hello

    注意

    以下错误的原因是,等号前后空格,系统会识别成命令

    1. [root@hadoop scripts]# my_var = Hello
    2. -bash: my_var: command not found

     如果要空格,用引号

    1. [root@hadoop scripts]# my_var="hello, world"
    2. [root@hadoop scripts]# echo $my_var
    3. hello, world

    双引号和单引号都可以

    1. [root@hadoop scripts]# my_var='hello, world'
    2. [root@hadoop scripts]# echo $my_var
    3. hello, world

     用以下发生判断局部变量,判断结果是局部变量

    1. [root@hadoop scripts]# set | grep my_var
    2. my_var='hello, world'
    3. [root@hadoop scripts]# bash
    4. [root@hadoop scripts]# ps -f
    5. UID PID PPID C STIME TTY TIME CMD
    6. root 20191 20187 0 16:53 pts/0 00:00:00 -bash
    7. root 20504 20191 0 18:18 pts/0 00:00:00 bash
    8. root 20513 20504 0 18:18 pts/0 00:00:00 ps -f
    9. [root@hadoop scripts]# echo $my_var
    10. [root@hadoop scripts]# exit
    11. exit

     提升成全局变量

    1. [root@hadoop scripts]# export my_var #做导出
    2. [root@hadoop scripts]# echo $my_var
    3. hello, world
    4. [root@hadoop scripts]# ps -f #查看父子继承关系
    5. UID PID PPID C STIME TTY TIME CMD
    6. root 20191 20187 0 16:53 pts/0 00:00:00 -bash
    7. root 20525 20191 0 18:22 pts/0 00:00:00 ps -f
    8. [root@hadoop scripts]# bash #进入子shell
    9. [root@hadoop scripts]# ps -f #查看父子继承关系
    10. UID PID PPID C STIME TTY TIME CMD
    11. root 20191 20187 0 16:53 pts/0 00:00:00 -bash
    12. root 20526 20191 0 18:22 pts/0 00:00:00 bash
    13. root 20537 20526 0 18:23 pts/0 00:00:00 ps -f
    14. [root@hadoop scripts]# echo $my_var #在子shell中查看
    15. hello, world

    在全局变量下的子进程中,进行修改,不影响父shell的结果

    1. [root@hadoop scripts]# my_var="hello, Linux"
    2. [root@hadoop scripts]# echo $my_var
    3. hello, Linux
    4. [root@hadoop scripts]# exit
    5. exit
    6. [root@hadoop scripts]# ps -f
    7. UID PID PPID C STIME TTY TIME CMD
    8. root 20191 20187 0 16:53 pts/0 00:00:00 -bash
    9. root 20548 20191 0 18:28 pts/0 00:00:00 ps -f
    10. [root@hadoop scripts]# echo $my_var
    11. hello, world

    在全局变量下的子进程中,在修改全局,依然还是hello, world

    1. [root@hadoop scripts]# bash
    2. [root@hadoop scripts]# ps -f
    3. UID PID PPID C STIME TTY TIME CMD
    4. root 20191 20187 0 16:53 pts/0 00:00:00 -bash
    5. root 20560 20191 0 18:31 pts/0 00:00:00 bash
    6. root 20569 20560 0 18:32 pts/0 00:00:00 ps -f
    7. [root@hadoop scripts]# my_var="hello, Linux"
    8. [root@hadoop scripts]# export my_var
    9. [root@hadoop scripts]# echo $my_var
    10. hello, Linux
    11. [root@hadoop scripts]# exit
    12. exit
    13. [root@hadoop scripts]# echo $my_var
    14. hello, world

     基于脚本的方式实现局部变量提升成全局

    1. [root@hadoop scripts]# echo $new_var
    2. [root@hadoop scripts]# new_var="hello, linux"
    3. [root@hadoop scripts]# echo $new_var
    4. hello, linux
    5. [root@hadoop scripts]# vim hello2.sh
    6. [root@hadoop scripts]# cat hello2.sh
    7. #!/bin/bash
    8. echo "hello, world"
    9. echo $my_var
    10. echo $new_var
    11. [root@hadoop scripts]# source hello2.sh
    12. hello, world
    13. hello, world
    14. hello, linux
    15. [root@hadoop scripts]# sh hello2.sh
    16. hello, world
    17. hello, world
    18. [root@hadoop scripts]# export new_var
    19. [root@hadoop scripts]# sh hello2.sh
    20. hello, world
    21. hello, world
    22. hello, linux

    默认定义的类型是字符串,数值计算运算符的表达了

    1. [root@hadoop scripts]# echo $a
    2. 2
    3. [root@hadoop scripts]# a=1+5
    4. [root@hadoop scripts]# echo $a
    5. 1+5

     数值运算

    1. [root@hadoop scripts]# a=$((1+5))
    2. [root@hadoop scripts]# echo $a
    3. 6
    4. [root@hadoop scripts]# a=$[5+9]
    5. [root@hadoop scripts]# echo $a
    6. 14

    只读变量readonly

    1. [root@hadoop scripts]# readonly b=5
    2. [root@hadoop scripts]# b=10
    3. -bash: b: readonly variable
    4. [root@hadoop scripts]# echo $b
    5. 5

     撤销命令unset

    1. [root@hadoop scripts]# unset a
    2. [root@hadoop scripts]# unset b #相当于常量,不能撤销
    3. -bash: unset: b: cannot unset: readonly variable

    三、特殊变量

    1.$n

    $n (功能描述:n 为数字,$0 代表该脚本名称,$1-$9 代表第一到第九个参数,十以 上的参数,十以上的参数需要用大括号包含,如${10})

    1. [root@hadoop scripts]# vim hello2.sh
    2. #!/bin/bash
    3. echo "hello, world"
    4. echo "hello, $1"
    5. [root@hadoop scripts]# . hello2.sh
    6. hello, world
    7. hello,
    8. [root@hadoop scripts]# . hello2.sh xiaoming
    9. hello, world
    10. hello, xiaoming
    1. [root@hadoop scripts]# vim parameter.sh
    2. #!/bin/bash
    3. echo '===============$n================'
    4. echo scripts name: $0
    5. echo 1st parament: $1
    6. echo 2nd parament: $2
    7. [root@hadoop scripts]# chmod +x parameter.sh
    8. [root@hadoop scripts]# . parameter.sh abc def
    9. ===============$n================
    10. scripts name: -bash
    11. 1st parament: abc
    12. 2nd parament: def
    13. [root@hadoop scripts]# ./parameter.sh abc def
    14. ===============$n================
    15. scripts name: ./parameter.sh
    16. 1st parament: abc
    17. 2nd parament: def

    2.$#

    $# (功能描述:获取所有输入参数个数,常用于循环,判断参数的个数是否正确以及 加强脚本的健壮性)。

    参数个数

    1. [root@hadoop scripts]# vim parameter.sh
    2. #!/bin/bash
    3. echo '===============$n================'
    4. echo scripts name: $0
    5. echo 1st parament: $1
    6. echo 2nd parament: $2
    7. echo '===============$#================'
    8. echo parameter numbers: $#
    9. [root@hadoop scripts]# ./parameter.sh
    10. ===============$n================
    11. scripts name: ./parameter.sh
    12. 1st parament:
    13. 2nd parament:
    14. ===============$#================
    15. parameter numbers: 0
    16. [root@hadoop scripts]# ./parameter.sh abc def
    17. ===============$n================
    18. scripts name: ./parameter.sh
    19. 1st parament: abc
    20. 2nd parament: def
    21. ===============$#================
    22. parameter numbers: 2

    3.$*、$@

    $* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)

    $@ (功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)

    1. [root@hadoop scripts]# vim parameter.sh
    2. #!/bin/bash
    3. echo '===============$n================'
    4. echo scripts name: $0
    5. echo 1st parament: $1
    6. echo 2nd parament: $2
    7. echo '===============$#================'
    8. echo parameter numbers: $#
    9. echo '===============$*================'
    10. echo $*
    11. echo '===============$@================'
    12. echo $@
    13. [root@hadoop scripts]# ./parameter.sh abc def
    14. ===============$n================
    15. scripts name: ./parameter.sh
    16. 1st parament: abc
    17. 2nd parament: def
    18. ===============$#================
    19. parameter numbers: 2
    20. ===============$*================
    21. abc def
    22. ===============$@================
    23. abc def

    4.$?

    $? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为 0,证明上一 个命令正确执行;如果这个变量的值为非 0(具体是哪个数,由命令自己来决定),则证明 上一个命令执行不正确了。)127表示未找到命令,1表示一般错误

    1. [root@hadoop scripts]# echo $?
    2. 0
    3. [root@hadoop scripts]# parsmeter.sh
    4. -bash: parsmeter.sh: command not found
    5. [root@hadoop scripts]# echo $? #表示未找到命令
    6. 127

  • 相关阅读:
    详解JVM类加载
    Java中的String类真的不可变吗?java面试常见问题
    当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
    数据血缘分析-Python代码的智能解析
    封神工具:腾讯云服务器价格计算器_好工具分享
    科研工作学习中常用的录制动图软件——screenToGif
    英伟达再放AI芯片“大招” H200 GPU是人工智能技术的里程碑
    SIP系统组成格式
    罗茨气体流量计的结构设计
    x86: perf_events内核初始化
  • 原文地址:https://blog.csdn.net/m0_55834564/article/details/126414665