zoukankan      html  css  js  c++  java
  • : "${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

    #

     

     

  • 相关阅读:
    UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
    SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
    洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间合并(单点更新、区间查询)
    HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
    HDU 3183.A Magic Lamp-区间找最小值-RMQ(ST)
    HDU 1231.最大连续子序列-dp+位置标记
    牛客网 牛客练习赛43 F.Tachibana Kanade Loves Game-容斥(二进制枚举)+读入挂
    CodeForce-811B Vladik and Complicated Book(水题)
    POJ1426——Find The Multiple (简单搜索+取余)
    POJ——3278 Catch That Cow(BFS队列)
  • 原文地址:https://www.cnblogs.com/winditsway/p/14240622.html
Copyright © 2011-2022 走看看