zoukankan      html  css  js  c++  java
  • shell中的变量

    一、系统变量

    常用系统变量

    $HOME
    $PWD
    $SHELL
    $USER

     查看变量值

    [root@slave2 testshell]# echo $HOME
    /root
    [root@slave2 testshell]# echo $PWD
    /root/testshell
    [root@slave2 testshell]# echo $USER
    root

     显示当前Shell中所有变量

    [root@slave2 testshell]# set
    BASH=/bin/bash
    BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
    BASH_ALIASES=()
    BASH_ARGC=()
    BASH_ARGV=()
    BASH_CMDS=()
    BASH_LINENO=()
    BASH_SOURCE=()
    BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
    BASH_VERSION='4.2.46(2)-release'
    CLASSPATH=.::/usr/local/src/jdk1.8.0_171/lib
    COLUMNS=119
    DIRSTACK=()
    EUID=0
    GROUPS=()
    HADOOP_HOME=/usr/local/src/hadoop-2.6.1
    HBASE_HOME=/usr/local/src/hbase-0.98.6-hadoop2
    HISTCONTROL=ignoredups
    HISTFILE=/root/.bash_history
    HISTFILESIZE=1000
    HISTSIZE=1000
    HOME=/root
    HOSTNAME=slave2
    HOSTTYPE=x86_64
    ID=0
    IFS=$' 	
    '
    JAVA_HOME=/usr/local/src/jdk1.8.0_171
    KAFKA_HOME=/usr/local/src/kafka_2.11-0.10.2.1
    LANG=en_US.UTF-8
    LESSOPEN='||/usr/bin/lesspipe.sh %s'
    LINES=61
    LOGNAME=root

    二、自定义变量

     1. 基本语法

    • 定义变量:变量=值(=两边没有空格)
      [root@slave2 testshell]# W=10
      [root@slave2 testshell]# echo $W
      10
    • 撤销变量:unset 变量
      [root@slave2 testshell]# W=10
      [root@slave2 testshell]# echo $W
      10
      [root@slave2 testshell]# unset W
      [root@slave2 testshell]# echo $W
      
      [root@slave2 testshell]#
    • 声明静态变量:readonly 变量,注意不能unset
      [root@slave2 testshell]# readonly B=2
      [root@slave2 testshell]# echo $B
      2
      [root@slave2 testshell]# unset B
      -bash: unset: B: cannot unset: readonly variable

     2.变量定义规则

    • 变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写
    • 等号两边不能有空格
    • 在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
      [root@slave2 testshell]# C=1+1
      [root@slave2 testshell]# echo C
      C
      [root@slave2 testshell]# echo $C
      1+1
    • 变量的值如果有空格,需要使用“”或‘’括起来
      [root@slave2 testshell]# D=banzhang love mm
      -bash: love: command not found
      [root@slave2 testshell]# D='banzhang love mm'
      [root@slave2 testshell]# echo $D             
      banzhang love mm
    • 可把变量提升为全局环境变量,可供其它shell程序使用
      [root@slave2 testshell]# D='banzhang love mm'
      [root@slave2 testshell]# echo $D             
      banzhang love mm
      [root@slave2 testshell]# vim ../helloworld.sh 
      #!/bin/bash
      echo 'hello world!'
      echo $D
      ~                                                                                                                      
      ~                                                                                                                      
      "../helloworld.sh" 3L, 40C written                                                                 
      [root@slave2 testshell]# ../helloworld.sh 
      hello world!
      
      [root@slave2 testshell]# export D
      [root@slave2 testshell]# ../helloworld.sh 
      hello world!
      banzhang love mm

    三、特殊变量

    3.1 特殊变量:$n

    • 基本语法

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

    •  案例实操
      [root@slave2 testshell]# touch parameter.sh
      [root@slave2 testshell]# vim parameter.sh 
      #!/bin/bash
      echo "$0  $1  $2"
      [root@slave2 testshell]# chmod 777 parameter.sh
      [root@slave2 testshell]# ./parameter.sh   
      ./parameter.sh    
      [root@slave2 testshell]# bash parameter.sh 
      parameter.sh    
      [root@slave2 testshell]# bash parameter.sh banzhang
      parameter.sh  banzhang  
      [root@slave2 testshell]# bash parameter.sh banzhang love
      parameter.sh  banzhang  love

    3.2 特殊变量:$#

    • 基本语法
      $#:功能描述:获取所有输入参数个数,常用于循环
    • 案例实操
      [root@slave2 testshell]# vim parameter.sh 
      #!/bin/bash
      echo "$0  $1  $2"
      echo $#
      [root@slave2 testshell]# bash parameter.sh banzhang love
      parameter.sh  banzhang  love
      2

    3.3 特殊变量:$*、$@

    • 基本语法

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

        $@:这个变量也代表命令行中所有的参数,不过它是把每个参数区分对待

    • 案例实操
      [root@slave2 testshell]# vim parameter.sh               
      #!/bin/bash
      echo "$0  $1  $2"
      echo $#
      echo $*
      echo $@
      [root@slave2 testshell]# bash parameter.sh banzhang love
      parameter.sh  banzhang  love
      2
      banzhang love
      banzhang love
      [root@slave2 testshell]# bash parameter.sh banzhang love mm
      parameter.sh  banzhang  love
      3
      banzhang love mm
      banzhang love mm

    3.4 特殊变量:$?

    • 基本语法
      $?:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。
    • 案例实操
      [root@slave2 testshell]# bash parameter.sh banzhang love mm
      parameter.sh  banzhang  love
      3
      banzhang love mm
      banzhang love mm
      [root@slave2 testshell]# echo $?
      0
      [root@slave2 testshell]# a= 8
      -bash: 8: command not found
      [root@slave2 testshell]# echo $?
      127
  • 相关阅读:
    SSH学习-struts2整合spring报错'Could not open ServletContext resource [/WEB-INF/applicationContext.xml]'
    YAML学习
    配置composer代理
    Windows下配置PHPUnit(pear已弃用,使用phpunit.phar)
    算法-第四版-练习1.3.9解答
    算法-第四版-练习1.3.10解答
    算法-第四版-练习1.3.11解答
    算法-第四版-练习1.3.12解答
    算法-第四版-练习1.3.13解答
    算法-第四版-练习1.3.14解答
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10409117.html
Copyright © 2011-2022 走看看