• : "${LOG_FILE:=/var/log/xxx.log}"


    : "${LOG_FILE:=/var/log/factory_install.log}"
    

      Shell Parameter Expansioin

    ${parameter:=word}

    If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

    #

    ppparameter is unset
    #!/bin/sh
    set +x
    a=${ppparameter:=wwwword}
    echo ${a}

    cor@debian:
    /tmp$ . 1.sh wwwword

    #

    ppparameter='Peter',  is not null
    cor@debian:/tmp$ cat 1.sh 
    
    #!/bin/sh
    set -x
    
    ppparameter="Peter"
    a=${ppparameter:=wwwword}
    set +x
    echo "a = "${a}
    cor@debian:/tmp$ . 1.sh 
    ++ ppparameter=Peter
    ++ a=Peter
    ++ set +x
    a = Peter

    #

    What is the purpose of the : (colon) GNU Bash builtin? linkage from stackoverflow, seems a good one, but not fully understand for me now.

    let's try design a experiment to help us to understand how it works

    #try with auth.log

    cor@debian:/var/log$ ls
    alternatives.log        alternatives.log.5.gz  auth.log.1     daemon.log.1     debug.4.gz      dpkg.log.4.gz   gdm3           lastlog            syslog       unattended-upgrades  Xorg.0.log
    alternatives.log.1      alternatives.log.6.gz  auth.log.2.gz  daemon.log.2.gz  dpkg.log        dpkg.log.5.gz   hp             messages           syslog.1     user.log             Xorg.0.log.old
    alternatives.log.10.gz  alternatives.log.7.gz  auth.log.3.gz  daemon.log.3.gz  dpkg.log.1      dpkg.log.6.gz   installer      messages.1         syslog.2.gz  user.log.1           Xorg.1.log
    alternatives.log.11.gz  alternatives.log.8.gz  auth.log.4.gz  daemon.log.4.gz  dpkg.log.10.gz  dpkg.log.7.gz   kern.log       messages.2.gz      syslog.3.gz  user.log.2.gz        Xorg.1.log.old
    alternatives.log.12.gz  alternatives.log.9.gz  btmp           debug            dpkg.log.11.gz  dpkg.log.8.gz   kern.log.1     messages.3.gz      syslog.4.gz  user.log.3.gz        Xorg.2.log
    alternatives.log.2.gz   apache2                btmp.1         debug.1          dpkg.log.12.gz  dpkg.log.9.gz   kern.log.2.gz  messages.4.gz      syslog.5.gz  user.log.4.gz        Xorg.2.log.old
    alternatives.log.3.gz   apt                    cups           debug.2.gz       dpkg.log.2.gz   faillog         kern.log.3.gz  sddm.log           syslog.6.gz  wtmp
    alternatives.log.4.gz   auth.log 

     #

    cor@debian:/tmp$ cat 1.sh 
    #!/bin/sh
    set -x
    ppparameter="Peter"
    a=${ppparameter:=wwwword}
    set +x
    : "${LOG_FILE1:=/var/log/factory_install.log}"
    : "${LOG_FILE2:=/var/log/auth.log}"
    
    echo "a = "${a}
    echo "LOG_FILE1 = " "${LOG_FILE1}"
    echo "LOG_FILE2 = " "${LOG_FILE2}"
     
    cor@debian:/tmp$ . 1.sh ++ ppparameter=Peter ++ a=Peter ++ set +x a = Peter LOG_FILE1 = /var/log/factory_install.log LOG_FILE2 = /var/log/auth.log

      

    cor@debian:/tmp$ ls /var/log/factory_install.log -l
    ls: cannot access '/var/log/factory_install.log': No such file or directory
    cor@debian:/tmp$ ls /var/log/auth.log -l
    -rw-r----- 1 root adm 82132 Jan 15 15:17 /var/log/auth.log

    From the experiments above: it seems there is no difference.

    remove the ':'

    cor@debian:/tmp$ . 1.sh 
    ++ ppparameter=Peter
    ++ a=Peter
    ++ set +x
    bash: /var/log/factory_install.log: No such file or directory
    bash: /var/log/auth.log: Permission denied
    a = Peter
    LOG_FILE1 =  /var/log/factory_install.log
    LOG_FILE2 =  /var/log/auth.log
    cor@debian:/tmp$ cat 1.sh 
    #!/bin/sh
    set -x
    ppparameter="Peter"
    a=${ppparameter:=wwwword}
    set +x
    "${LOG_FILE1:=/var/log/factory_install.log}"
    "${LOG_FILE2:=/var/log/auth.log}"
    
    echo "a = "${a}
    echo "LOG_FILE1 = " "${LOG_FILE1}"
    echo "LOG_FILE2 = " "${LOG_FILE2}"
    

      

    What does a leading colon (:) mean in a script?

    "Another place you might see it is with conditional variable setting. For example, say we want to set xx to "foo" but only if it isn't already set. We can use use '${xx:="foo"}' as a shorthand way to avoid "if" or "case" blocks, but that has a side effect:

    ${xx:="foo"}
    -bash: foo: command not found
     
    

    You could redirect errout to stop that complaint, but what if 'foo' were a real command? It would be executed, and that might not be what you want. You can avoid that by the leading ":" again:

    : ${xx:="foo"}
     
    

    So that's why those leading colons are often found in shell scripts."

    #

    Introduction to 'if'

    [ -z STRING ] True of the length if "STRING" is zero.

    长度为零 条件成立

    # STRING is  zero:

    localhost /tmp/shell # cat 1.sh 
    DA=""
    #DA="aaaaa"
    if [ -z "${DA}" ];then
    echo "ttttttT"
    else
    echo "Ssssss"
    fi
    localhost /tmp/shell # . 1.sh 
    ttttttT
    

      

      # STRING is not zero:

    localhost /tmp/shell # cat 1.sh 
    #DA=""
    DA="aaaaa"
    if [ -z "${DA}" ];then
    echo "ttttttT"
    else
    echo "Ssssss"
    fi
    localhost /tmp/shell # . 1.sh 
    Ssssss
    

      

    [ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.

    #条件成立如果 长度大于零

    localhost /tmp/shell # cat 2.sh 
    #DA=""
    DA="aaaaa"
    if [ -n "${DA}" ];then
    echo "ttttttT"
    else
    echo "Ssssss"
    fi
    localhost /tmp/shell # . 2.sh 
    ttttttT
    

      

    # 等于零

    localhost /tmp/shell # cat 2.sh 
    DA=""
    #DA="aaaaa"
    if [ -n "${DA}" ];then
    echo "ttttttT"
    else
    echo "Ssssss"
    fi
    localhost /tmp/shell # . 2.sh 
    Ssssss
    

      

    -x FILE ] True if FILE exists and is executable.

    #

    #

    -e FILE ] True if FILE exists.

    if [[ -e /dev/nvram ]];

     #

     

    -r FILE ] True if FILE exists and is readable.

    #

    #

    -b FILE ] True if FILE exists and is a block-special file.

    #if [ -b "${src_media}" ]; then

     

    -s FILE ] True if FILE exists and has a size greater than zero.

    # if [ -s "${tmp_dir}/firmware" ]; then

    #

     

     

  • 相关阅读:
    git设置用户名和邮箱
    D②商品列表:毫秒转换成时间;分页添加背景;$$搜索商品关键字 $$$清空
    C②参数管理:修改删除;空格分隔的字符串渲染成标签;动态编辑标签;删除标签
    有用的抓包工具Ethereal
    PHP与XML联手进行网站编程
    配置Apache 2.2+PHP 5.2.9支持OCI通过Oracle9i Client连接Oracle
    UTF8编码主页调用JS显示乱码问题解决办法
    WIndows 环境下安装php环境(php5.2.9+apache2.2安装过程)
    PHP5 在处理 XML 方面的改进
    marquee上下滚动停顿效果
  • 原文地址:https://www.cnblogs.com/winditsway/p/14240622.html
Copyright © 2020-2023  润新知