关于debian挤压与debian wheezy的问题

时间:2014-09-22 04:35:44

标签: bash awk sed grep nawk

我的脚本的第一部分是在debian wheezy框中工作的:

    OUTPUT_DIR=/share/es-ops/Build_Farm_Reports/WorkSpace_Reports
    BASE=/export/ws
    TODAY=`date +"%m-%d-%y"`
    HOSTNAME=`hostname`
    WORKSPACES=( "bob_avail" "bob_used" "mel_avail" "mel_used" "sideshow-ws2_avail" "sideshow-ws2_used" )
    if ! [ -f $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == "sideshow" ]; then
    echo "$HOSTNAME" >  $OUTPUT_DIR/$HOSTNAME.csv # with a linebreak
    separator="," # defined empty for the first value
    for v in "${WORKSPACES[@]}"
    do
      echo -n "$separator$v" >> $OUTPUT_DIR/$HOSTNAME.csv # append, concatenated, the separator and the value to the file
      #separator="," # comma for the next values
    done
    echo >> $OUTPUT_DIR/$HOSTNAME.csv # add a linebreak (if you want it)
    WORKSPACES2=( "bob" "mel" "sideshow-ws2" )
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
        BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    elif [ $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == "sideshow" ]; then
    WORKSPACES2=( "bob" "mel" "sideshow-ws2" )
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
        BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    else
    :
    fi
每次cron在凌晨3点-8GMT熄灭时,

产生每日输出:

    sideshow
    ,bob_avail,bob_used,mel_avail,mel_used,sideshow-ws2_avail,sideshow-ws2_used
    09-20-14,470400,1032124,661826,1032124,43443,1032108
    09-20-15,470400,1032124,661826,1032124,43443,1032108
    09-20-16,470400,1032124,661826,1032124,43443,1032108

但出于某些原因,当我尝试在其他3个debian挤压盒上运行它时,我会在值之间得到三个逗号:

    case "$HOSTNAME" in
        simpsons) WORKSPACES=(bart_avail bart_used homer_avail home_used lisa_avail lisa_used \
                              marge_avail marge_used releases_avail releases_used rt-private_avail rt-private_used \
                              simpsons-ws0_avail simpsons-ws0_used simpsons-ws1_avail simpsons-ws1_used simpsons-ws2_avail \
                              simpsons-ws2_used vsimpsons-ws_avail vsimpsons-ws_used) ;;
        moes)     WORKSPACES=(barney_avail barney_used carl_avail carl_used lenny_avail lenny_used moes-ws2_avail moes-ws2_used) ;;
        flanders) WORKSPACES=(flanders-ws0_avail flanders-ws0_used flanders-ws1_avail flanders-ws1_used flanders-ws2_avail \
                              flanders-ws2_used maude_avail maude_used ned_avail ned_used rod_avail rod_used todd_avail \
                              todd_used to-delete_avail to-delete_used) ;;
    esac
    if ! [ -f $OUTPUT_DIR/$HOSTNAME.csv ]; then
    echo "$HOSTNAME" >  $OUTPUT_DIR/$HOSTNAME.csv # with a linebreak
    separator="," # defined empty for the first value
    for v in "${WORKSPACES[@]}"
    do
      echo -n "$separator$v" >> $OUTPUT_DIR/$HOSTNAME.csv # append, concatenated, the separator and the value to the file
      #separator="," # comma for the next values
    done
    echo >> $OUTPUT_DIR/$HOSTNAME.csv # add a linebreak (if you want it)
    case "$HOSTNAME" in
            simpsons) WORKSPACES2=(bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws) ;;
            moes)     WORKSPACES2=(barney carl lenny moes-ws2) ;;
            flanders) WORKSPACES2=(flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete) ;;
    esac
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
    BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    elif [ $OUTPUT_DIR/$HOSTNAME.csv ]; then
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
        BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    else
    :
    fi

看起来像这样:

    simpsons
    ,bart_avail,bart_used,homer_avail,home_used,lisa_avail,lisa_used,marge_avail,marge_used,releases_avail,releases_used,rt-private_avail,rt-private_used,simpsons-ws0_avail,simpsons-ws0_used,simpsons-ws1_avail,simpsons-ws1_used,simpsons-ws2_avail,simpsons-ws2_used,vsimpsons-ws_avail,vsimpsons-ws_used
    09-21-14,,,43417,1154259,,,2669,1195007,,,3427,1194249,,,2948,162602,,,128174,281377,,,967520,991870,,,85,168836,,,11995,1011937,,,780184,199511,,,14251,22408

你能帮我把3个逗号减少到1之间的值吗?

在这3个盒子(辛普森,模糊和佛兰德斯)上,获得正确利用和使用值的唯一方法是运行这样的awk:

df -m /export/ws/maude | awk '{if (NR!=1) {print $3, $2}}'

看起来像这样:

492 163306

否则,如果你这样运行:

df -m /export/ws/maude | awk '{print $3, $2}'

你明白了:

Used 1M-blocks

492 163306

1 个答案:

答案 0 :(得分:0)

我通过解决方法修复了三重逗号问题:

    OUTPUT_DIR=/share/es-ops/Build_Farm_Reports/WorkSpace_Reports
    BASE=/export/ws
    TODAY=`date +"%m-%d-%y"`
    HOSTNAME=`hostname`
    WORKSPACES=( "bob_avail" "bob_used" "mel_avail" "mel_used" "sideshow-ws2_avail" "sideshow-ws2_used" )
    if ! [ -f $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == "sideshow" ]; then
    echo "$HOSTNAME" >  $OUTPUT_DIR/$HOSTNAME.csv # with a linebreak
    separator="," # defined empty for the first value
    for v in "${WORKSPACES[@]}"
    do
      echo -n "$separator$v" >> $OUTPUT_DIR/$HOSTNAME.csv # append, concatenated, the separator and the value to the file
      #separator="," # comma for the next values
    done
    echo >> $OUTPUT_DIR/$HOSTNAME.csv # add a linebreak (if you want it)
    WORKSPACES2=( "bob" "mel" "sideshow-ws2" )
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
        BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    elif [ $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == "sideshow" ]; then
    WORKSPACES2=( "bob" "mel" "sideshow-ws2" )
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
        BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    else
    :
    fi
    case "$HOSTNAME" in
        simpsons) WORKSPACES=(bart_avail bart_used homer_avail home_used lisa_avail lisa_used marge_avail marge_used releases_avail releases_used rt-private_avail rt-private_used simpsons-ws0_ava
    il simpsons-ws0_used simpsons-ws1_avail simpsons-ws1_used simpsons-ws2_avail simpsons-ws2_used vsimpsons-ws_avail vsimpsons-ws_used) ;;
        moes)     WORKSPACES=(barney_avail barney_used carl_avail carl_used lenny_avail lenny_used moes-ws2_avail moes-ws2_used) ;;
        flanders) WORKSPACES=(flanders-ws0_avail flanders-ws0_used flanders-ws1_avail flanders-ws1_used flanders-ws2_avail flanders-ws2_used maude_avail maude_used ned_avail ned_used rod_avail ro
    d_used todd_avail todd_used to-delete_avail to-delete_used) ;;
    esac
    if ! [ -f $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == `hostname` ]; then
    echo "$HOSTNAME" >  $OUTPUT_DIR/$HOSTNAME.csv # with a linebreak
    separator="," # defined empty for the first value
    for v in "${WORKSPACES[@]}"
    do
      echo -n "$separator$v" >> $OUTPUT_DIR/$HOSTNAME.csv # append, concatenated, the separator and the value to the file
      #separator="," # comma for the next values
    done
    echo >> $OUTPUT_DIR/$HOSTNAME.csv # add a linebreak (if you want it)
    case "$HOSTNAME" in
            simpsons) WORKSPACES2=(bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws) ;;
            moes)     WORKSPACES2=(barney carl lenny moes-ws2) ;;
            flanders) WORKSPACES2=(flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete) ;;
    esac
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
    BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    sed -i s/,,,/,/g "$OUTPUT_DIR/$HOSTNAME.csv"
    elif [ $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == `hostname` ]; then
    case "$HOSTNAME" in
            simpsons) WORKSPACES2=(bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws) ;;
            moes)     WORKSPACES2=(barney carl lenny moes-ws2) ;;
            flanders) WORKSPACES2=(flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete) ;;
    esac
    df -m "${WORKSPACES2[@]/#//export/ws/}" | awk '
        BEGIN  { "date +'%m-%d-%y'" | getline date;
                 printf "%s",date }
        NR > 1 { printf ",%s,%s", $3, $2; }
        END    { printf "\n"}' >> "$OUTPUT_DIR/$HOSTNAME.csv"
    sed -i s/,,,/,/g "$OUTPUT_DIR/$HOSTNAME.csv"
    else
    :
    fi

我刚才说:

sed -i s/,,,/,/g "$OUTPUT_DIR/$HOSTNAME.csv"

我真的希望我能想到如何做首先消除三重逗号的awk部分,但至少我的脚本现在可以正常工作。