vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

如何在awk脚本中使用shell变量 #91

Open vieyahn2017 opened 7 months ago

vieyahn2017 commented 7 months ago
echo "$result" | grep " sd" | awk -v nlun="$lun" '{print nlun" "$2" "$3}'
vieyahn2017 commented 7 months ago

https://blog.csdn.net/weixin_35401836/article/details/116624892

vieyahn2017 commented 7 months ago
#!/bin/bash

tmpfile=/root/multipath_tmp
[ -e "$tmpfile" ] && rm -rf $tmpfile

function explain_one_foreach () {
        for lun in $(cat "$WWIDS" | grep -v ^# | sed s#/##g);
        do
                result=$(multipath -ll "$lun" 2> /dev/null)
                echo "$result" | grep " sd" | awk -v nlun="$lun" '{print nlun" "$3" "$2" "$5" "$6" "$7}' >> $tmpfile
        done
}

function explain_all () {
        results=$(multipath -ll 2> /dev/null)
        lun=""
        echo "$results" | while read line;
        do
                if [ "${line:0:2}" == "36" ]; then
                        lun=$(echo $line | awk '{print $1}')
                fi
                if [ $(echo $line | grep -c " sd") -eq 1 ]; then
                        echo "$line" | awk -v nlun="$lun" '{print nlun" "$3" "$2" "$5" "$6" "$7}'
                fi
        done
}

WWIDS=/etc/multipath/wwids
if [ ! -f /etc/multipath/wwids ]; then
    echo "/etc/multipath/wwids files not existed"
    if [ $(multipath -ll  2>&1 | grep -c "DM multipath kernel driver not loaded") -eq 1 ]; then
        echo "DM multipath kernel driver not loaded"
        exit 1
    fi
fi

explain_all >> $tmpfile
exit 0