zoukankan      html  css  js  c++  java
  • 变量 $ ${} ${!}

    1、变量的引用,当param为数组时,$param 和"$param"区别

    $param将数组作为n个参数处理,而"$param"将数组作为一个整体处理,举例:

    [xiluhua@vm-xiluhua][~/shell_script]$ cat compare1.sh 
    #=======================================================================================
    #/bin/bash
    #auth: xiluhua
    #date: 20160717
    #desc: 
    #=======================================================================================
    v_list="a b c "
    echo "=== demo1 ==="
    echo "echo $v_list:"
    for char in $v_list
    do
       echo $char
    done
     
    echo "=== demo2 ==="
    echo "echo "$v_list":"
    for char in "v_list"
    do 
            echo $char
    done

    2、基本参数扩展:

    [xiluhua@vm-xiluhua][~]$ echo $temp
    100
    [xiluhua@vm-xiluhua][~]$ echo $temp1
    结果:echo nothing
    这时必须使用${}
    [xiluhua@vm-xiluhua][~]$ echo ${temp}1
    1001

    3、间接参数扩展:

    [xiluhua@vm-xiluhua][~]$ temp=100
    [xiluhua@vm-xiluhua][~]$ param=temp
    [xiluhua@vm-xiluhua][~]$ echo $param
    temp
    [xiluhua@vm-xiluhua][~]$ echo ${!param}
    100

    Bash4.0特性

    4、大小写修改:

    ${PARAMETER^}    将参数值的第一个字符改为大写
    ${PARAMETER^^}    将参数值的所有字符改为大写
    ${PARAMETER,}    将参数值的第一个字符改为小写
    ${PARAMETER,,}    将参数值的所有字符改为小写
    [xiluhua@vm-xiluhua][~/shell_script]$ touch test1.123 test2.123 test3.123
     
    [xiluhua@vm-xiluhua][~/shell_script]$ ll -t *.123
    -rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 test1.123
    -rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 test2.123
    -rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 test3.123  
    
    
    
    [xiluhua@vm-xiluhua][~/shell_script]$ cat 2.sh
    #=======================================================================================
    #/bin/bash
    #auth: xiluhua
    #date: 20160626
    #desc: test
    #=======================================================================================
    for file in *.123; do
            mv "$file" "${file^^}"
    done
    [xiluhua@vm-xiluhua][~/shell_script]$ 2.sh
     
    [xiluhua@vm-xiluhua][~/shell_script]$ ll -t *.123
    -rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 TEST1.123
    -rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 TEST2.123
    -rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 TEST3.123
     

    5、变量名扩展:

    [xiluhua@vm-xiluhua][~/shell_script]$ temp1=1
     
    [xiluhua@vm-xiluhua][~/shell_script]$ temp2=2
     
    [xiluhua@vm-xiluhua][~/shell_script]$ temp3=3
    
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${!temp*}
    temp1 temp2 temp3
    
    或
    
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${!temp@}
    temp1 temp2 temp3

    6、字符串移除:(举例说明)

    [xiluhua@vm-xiluhua][~/shell_script]$ pride="IRON MAN is the pride of the PEOPLE'S REPUBLIC OF CHINA"
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride#* }  #移除第一个空格前的内容
    MAN is the pride of the PEOPLE'S REPUBLIC OF CHINA
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride##* }  #移除最后一个空格前的内容
    CHINA
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride% *}  #移除最后一个空格后的内容
    IRON MAN is the pride of the PEOPLE'S REPUBLIC OF
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride%% *}  #移除最后一个空格前的内容
    IRON

    最常用的业务场景是提取文件名的一部分:
    [xiluhua@vm-xiluhua][~/shell_script]$ filename=test1.123
    
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename##*.}  #得到文件后缀
    123
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename%.*}  #得到文件名
    test1
    
    [xiluhua@vm-xiluhua][~/shell_script]$ filename=/home/xiluhua/test1.123
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename%/*}  #得到文件目录
    /home/xiluhua
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename##*/}  #得到文件名称
    test1.123
    
    
     

    7、字符串搜索与替换

    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride}    #原文
    IRON MAN is the pride of the PEOPLE'S REPUBLIC OF CHINA
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride/the/a}  #the替换成a,仅替换第一处
    IRON MAN is a pride of the PEOPLE'S REPUBLIC OF CHINA
    您在 /var/spool/mail/xiluhua 中有新邮件
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride//the/a}  #他和替换成a,替换所有
    IRON MAN is a pride of a PEOPLE'S REPUBLIC OF CHINA
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride/the}  #删除第一个the
    IRON MAN is pride of the PEOPLE'S REPUBLIC OF CHINA
    
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride//the}  #删除所有the
    IRON MAN is pride of PEOPLE'S REPUBLIC OF CHINA

    8、字符串长度

    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${#pride}
    55

    9、子字符串扩展

    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:5}  #截取第5个字符后的字符串
    MAN is the pride of the PEOPLE'S REPUBLIC OF CHINA
    
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:28:55}  #截取从28-55的字符
    PEOPLE'S REPUBLIC OF CHINA  

    10、指定默认值

    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:-"the pride of the PEOPLE'S REPUBLIC OF CHINA"}
    IRON MAN is the pride of the PEOPLE'S REPUBLIC OF CHINA
     
    [xiluhua@vm-xiluhua][~/shell_script]$ unset pride
     
    [xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:-"the pride of the PEOPLE'S REPUBLIC OF CHINA"}
    the pride of the PEOPLE'S REPUBLIC OF CHINA

     11、替换特殊字符

    PID=`netstat -tpln|grep 8002`;PID=${PID#*LISTEN};PID=`echo $PID | sed -s "s//java//g"`
  • 相关阅读:
    HTTP 错误 500.19 配置文件错误 ( 0x8007000d,0x80070032)
    system.web下的HttpModules节点和system.webServer下的modules节点的配置区别
    索引超出了数组界限(Microsoft.SqlServer.Smo)
    VS 附加进程调试 Web项目
    VS 调试 无法启动IIS Express Web 服务器(进程不存在)
    java基础面试题
    给dubbo接口添加白名单——dubbo Filter的使用
    mysql行转列转换
    Spring透过ApplicationListener来触发contextrefreshedevent事件
    spring mvc之请求过程源码分析
  • 原文地址:https://www.cnblogs.com/xiluhua/p/5677252.html
Copyright © 2011-2022 走看看