• shell 使用摘记


    1.一直有点搞不清shell里面的判断,反引号,echo。看下面例子,

    文件sedTest.yml内容:

    server:
        url: http://testjkweb.tourongjia.com
    spring:
      aop:
    ddsdsd
          auto: true
          proxy-target-class: false
      redis:

    代码:

    match_flag=echo `grep -q "^spring" "/home/vobile/test/sedTest.yml"`
    
    echo `$match_flag|wc -l`
    
    if `grep -q "^spring" "/home/vobile/test/sedTest.yml"`; then
            echo "matching"
    fi
    
    
    if $match_flag; then
            echo "match"
    else
            echo "not match"
    fi
    
    if [ `grep -q "^spring" "/home/vobile/test/sedTest.yml"` ]; then
    
            echo "matched 1"
    fi
    
    
    if [ $match_flag ]; then
    
            echo "matched 2"
    fi

    结果:

    1
    matching
    
    match
    matched 2

    可以看到,if `grep -q "^spring" "/home/vobile/test/sedTest.yml"` 和if $match_flag的结果相同,都能匹配

    而 if [ `grep -q "^spring" "/home/vobile/test/sedTest.yml"` ] 和 if [ $match_flag ] 的结果不同, if [ `grep -q "^spring" "/home/vobile/test/sedTest.yml"` ]不能匹配,??

    3.if [ ! `grep "^$key" "$CONFIG_FILE_PATH/$path"` ];

    这种代码要小心,如果`grep "^$key" "$CONFIG_FILE_PATH/$path"`返回字符串中有空格则会得到不匹配的结果,正确的做法

          t=`grep "^$key" "$CONFIG_FILE_PATH/$CONFIG_FILE"`
                    echo $t"aaa"
                    if [ ! "$t" ]; then
                            echo  "$CONFIG_FILE_PATH/$CONFIG_FILE不完整,$key项缺失"
                            exit 2
                    fi

    就是字符串要用“”括起来

     

     4. /tmp/$$中字符串是david.test = ${david.test}

    for key in $(awk -F "=" '{print $2}' /tmp/$$) ; do
                    echo "key=$key"

    结果:${david.test}key,前面空格被忽略了
    但换另外一种写法

     change_value=`echo $line | awk -F= '{gsub(/^( |	)*|( |	)*$/,"",$2);print $2}'`
      echo "change_value=$change_value"

    结果:change_value= ${david.test},表明空格不被省略,看起来是反引号和$符在具体的应用中有所区别

    喜欢艺术的码农
  • 相关阅读:
    数组地址,数组首地址与数组首元素地址的区别
    memset,memcpy与strcpy
    OJ之大数与高精度题必备知识
    OJ之星期几算法(泽勒一致性)
    二分查找及其优化
    爱上vim之快捷键使用技巧与个性化配置
    shell之终极shell——zsh
    memset的一些坑
    OO终章
    hOmewOrk 第三单元 总结
  • 原文地址:https://www.cnblogs.com/zjhgx/p/7833526.html
Copyright © 2020-2023  润新知