• shell下变量比较最佳实践


    https://stackoverflow.com/questions/13617843/unary-operator-expected

    If you know you're always going to use bash, it's much easier to always use the double bracket conditional compound command [[ ... ]], instead of the Posix-compatible  single bracket version [ ... ]. Inside a [[ ... ]] compound, word-splitting and pathname expansion are not applied to words, so you can rely on

    if [[ $aug1 == "and" ]];
    

    to compare the value of $aug1 with the string and.

    If you use [ ... ], you always need to remember to double quote variables like this:

    if [ "$aug1" = "and" ];
    

    If you don't quote the variable expansion and the variable is undefined or empty, it vanishes from the scene of the crime, leaving only

    if [ = "and" ]; 
    

    which is not a valid syntax. (It would also fail with a different error message if $aug1 included white space or shell metacharacters.)

    The modern [[ operator has lots of other nice features, including regular expression matching.

  • 相关阅读:
    view和activity的区别
    接口对象的实例化在接口回调中的使用
    GreenDao
    HAOI 2012 高速公路
    SDOI2010 地精部落
    hdu 1505 City Game
    uva 1506 Largest Rectangle in a Histogram
    2017 济南综合班 Day 2
    uva 12325 Zombie's Treasure Chest
    uva 11212 Editing a Book
  • 原文地址:https://www.cnblogs.com/codeking100/p/10235623.html
Copyright © 2020-2023  润新知