printf输出从一行转换为参数数量

时间:2012-08-17 20:37:12

标签: bash printf

这是我的代码的一部分:

FCWWN=`fcinfo hba-port | grep HBA | awk '{print $4}'`

for i in ${FCWWN}
do
    FCREMOTE=`fcinfo remote-port -p ${i} | grep Remote | nawk '{print $4}'`
    PRODUCT=`/usr/sbin/luxadm display ${FCREMOTE} | grep Product | nawk -F: '{print $2}'`
    CAPACITY=`/usr/sbin/luxadm display ${FCREMOTE} | grep capacity | nawk -F: '{print $2}'`
    LUNNAME=`/usr/sbin/luxadm display ${FCREMOTE} | grep /dev/rdsk | grep -i -v DEVICE`
    FCSTATE=`fcinfo hba-port ${i} | grep State: | nawk '{print $2}'`
    echo ""
    echo -e The FC Port WWN "\033[1m \E[36;40m ${i} \033[0m is \033[1m \E[37;42m ${FCSTATE} \033[0m"; echo -ne "\E[0m"
    printf "################################################################################## \n"
    printf "%-6s %9s %18s \n" "LUN:" "Storage" "Disk"
    printf "%-6s %9s %18s \n"  " "    "Type"    "Size"
    printf "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- \n"
    printf "%-6s\n %9s\n %2s\n" "${LUNNAME}" "${PRODUCT}" "${CAPACITY}"
done

输出结果为:

The FC Port WWN   10000000c9822976  is   online
##################################################################################
LUN:     Storage               Disk
            Type               Size
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  /dev/rdsk/c4t60060E80102A8AF005119C37000000C2d0s2
  /dev/rdsk/c4t60060E80102A8AF005119C37000000C0d0s2
  /dev/rdsk/c4t60060E80102A8AF005119C370000016Cd0s2
  /dev/rdsk/c4t60060E80102A8AF005119C3700000028d0s2
  /dev/rdsk/c4t60060E80102A8AF005119C37000000E1d0s2
                DF600F
                DF600F
                DF600F
                DF600F
                DF600F
        131072.000 MBytes
        131072.000 MBytes
        65536.000 MBytes
        262144.000 MBytes
        65536.000 MBytes

所需的输出是:

The FC Port WWN   10000000c9822976  is   online
##################################################################################
LUN:     Storage               Disk
            Type               Size
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  /dev/rdsk/c4t60060E80102A8AF005119C37000000C2d0s2   DF600F   131072.000 MBytes
  /dev/rdsk/c4t60060E80102A8AF005119C37000000C0d0s2   DF600F   131072.000 MBytes
  /dev/rdsk/c4t60060E80102A8AF005119C370000016Cd0s2   DF600F    65536.000 MBytes
  /dev/rdsk/c4t60060E80102A8AF005119C3700000028d0s2   DF600F    262144.000 MBytes
  /dev/rdsk/c4t60060E80102A8AF005119C37000000E1d0s2   DF600F    65536.000 MBytes

任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

一种快捷方法是将三个多行字符串转换为数组:

# bash 4 or later
mapfile -t LUN_ARR <<< "$LUNNAME"
mapfile -t PROD_ARR <<< "$PRODUCT"
mapfile -t CAP_ARR <<< "$CAPACITY"

# bash 3
IFS=$'\n' read -d '' -a LUN_ARR <<< "$LUNNAME"
IFS=$'\n' read -d '' -a PROD_ARR <<< "$PRODUCT"
IFS=$'\n' read -d '' -a CAP_ARR <<< "$CAPACITY"

然后简单地使用C风格的循环遍历数组

for (( idx=0; idx < ${#LUN_ARR[@]}; idx++ )); do
    printf "%-40s %9s %s\n" "${LUN_ARR[idx]}" "${PROD_ARR[idx]}" "${CAP_ARR[idx]}"
done

答案 1 :(得分:0)

你非常接近。我没有资源可以测试,但我真的会解决你的问题。

    printf "%-6s\n %9s\n %2s\n" "${LUNNAME}" "${PRODUCT}" "${CAPACITY}"

    printf "%-6s %9s %2s\n" "${LUNNAME}" "${PRODUCT}" "${CAPACITY}"

额外'\ n'总是会换行,对吗?

你也可以把它写成一个awk脚本。

或者至少通过重写脚本的前部来减少你开始的程序数,如

PRODUCT=`/usr/sbin/luxadm display ${FCREMOTE} | nawk -F: '/Product/{print $2}'`

而且,当你使用nawk时,那对我说是solaris。也许你正在写/ bin / sh(bourne shell脚本), 但如果没有,自1995年以来(至少)已经弃用了反向标记。使用现代命令替换,如

PRODUCT=$(/usr/sbin/luxadm display ${FCREMOTE} | nawk -F: '/Product/{print $2}')

IHTH