zoukankan      html  css  js  c++  java
  • shell使用技巧

    凡是传递给server端的文本数据,如果过长必须要截取,例如截取前480个字符

    echo ${lines:0:480}

    若有下载文件步骤,须判断文件是否下载成功

    length1=${pkg1_file[@]}

    if [ $length1 -eq 0 ]; then

         echo "下发安装包失败"

         exit 1

    fi

    若有解压安装包步骤,需判断是否解压成功

    su - oracle -c "cd $pkg_path; unzip -o $pkg1_file"

    if [ $? -ne 0 ]; then

         echo "解压安装包出错"

        echo "中断操作…"

        exit 1

    fi

    shell输出内容拼装成json格式时,需要去除每次插入的字符串后面的换行

    echo ',{"code":"add_results","alert": 1,"value":"'$error'","result": "ok"}'| tr -d [" "]>>$reports

    如果需要保存error数据里的换行:

    echo ',{"code":"add_results","alert": 1,"value":"'| tr -d [" "]>>$reports

    echo $error>>$reports

    echo '","result": "ok"}'| tr -d [" "]>>$reports

    若需要将shell以json样式输出的文本转json格式字符串供python或server端使用,需要进行转换

    import os

    import os

    import json

    import sys

    import socket

    if not os.path.exists(reports) or reports in ('',None) :

        print '巡检报告不存在'

        sys.exit(1)

    with open(reports,'r') as f:

        lines=f.read().strip(',').strip(' ').replace(' ','\n').replace(' ','\t')

    name=socket.gethostname()

    results=[]

    results.append(name)

    wlines=[i for i in lines.split('},')]

    for line in wlines[:-1]:

        results.append(json.loads(line+'}'))

    if len(wlines)==1:

        #print wlines[0]

        results.append(json.loads(wlines[0]))

    else:

        results.append(json.loads(wlines[-1]))

    results=json.dumps(results)

    #print results

    由于server端下发的数值全部为字符串,因此进行与基值的数据比较时,可以先转换成字符串,本例中基值为db_use_base_value

    typeset C=$(echo ${open_status} "$db_use_base_value" | awk '{print $1-$2}')

    types=`echo ${C:0:1}`

    if [ "$types" != "-" ]

    then

    error=$error$table_name"---"$open_status"%,"

    fi

    let "line_num=$line_num+1"

    done

    fi

    除法取两位小数

    use_size=$((total_size-free_size))

    use_percent=$(echo "scale=2; $use_size*100/$total_size" | bc)

    用find查找文件时排除某些特定目录,下方示例排除了/etc/init.d

    find /etc -path ‘/etc/init.d’ -a -prune -o -name *.conf -print

    用yum安装依赖包以后进行相应校验,例如

    #rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH}) ' binutils
    binutils-2.20.51.0.2-5.47.el6_9.1 (x86_64)

    su $os_gtp_user -c "cat > ~/startup.sh << "EOF"
    su $os_gtp_user -c "cd $dir_name;source setp;gtp;gtp -m"
    EOF"

  • 相关阅读:
    POJ 3253 Fence Repair STL 优先队列
    P1196 [NOI2002]银河英雄传说 题解
    UVA1316 Supermarket 题解
    P1955 [NOI2015]程序自动分析 题解
    P3807 【模板】卢卡斯定理 题解
    P2480 [SDOI2010]古代猪文 题解
    题解 P4778 【Counting swaps】
    P1313 计算系数 题解
    P3810 【模板】三维偏序(陌上花开)题解
    P1072 Hankson 的趣味题 题解
  • 原文地址:https://www.cnblogs.com/slqt/p/6774659.html
Copyright © 2011-2022 走看看