我正在尝试执行 3 个命令并将输出作为变量获取,该变量需要一些百分比计算并显示与示例相同的内容。这可以作为脚本执行吗?
第一个命令输出
$ isi status -q | grep -e "^Size" -e "^Used" -e "^Avail"
Size:573T
Used:448.0T
Avail:74T
第二个命令输出
$ isi_classic snapshot usage | tail -n 1 | awk '{printf "\t\t\tSnapshot USED %=%.1f%%\n", $4}'
Snapshot USED =0.6%
$ isi_classic snapshot usage | tail -n -1 | awk '{ print "\t\t\tSnapshot USED:" $1}'
Snapshot USED=3.2T
示例脚本awk 脚本不工作。删除空间后
# isi status -q | grep -e "^Size" -e "^Used" -e "^Avail" | awk 'function print (num)
pipe pipe quote> /Size:/ {TotalSize=$NF~}
pipe pipe quote> /Used:/ {TotalUsed=$NF}
pipe pipe quote> /Avail:/ {TotalAvail=$NF} }
pipe pipe quote> END {
pipe pipe quote> used=TotalUsed/Total_Size*100
pipe pipe quote> system("isi_classic snapshot usage | tail -n -1 | awk '{ print "Snapshot_used=" $1}'")
pipe pipe quote> system("isi_classic snapshot usage | tail -n 1 | awk '{printf "Snapshot_per :%.1f\n", $4}'")
pipe pipe quote> Usedw/o=Total Used-Snapshot USED/Total Size
pipe pipe quote> print hostname ": " "\t","Total Size:" Total_Size
pipe pipe quote> printf "\t\t\tTotalUsed:%.1f\n", TotalUsed, "TB"
pipe pipe quote> printf "\t\t\tTotalAvail:%.1f\n", TotalAvail, "TB"
pipe pipe quote> printf "\t\t\tTotalUsed %:%.1f\n", used, "TB"
pipe pipe quote> printf "\t\t\tSnapshot Used :%.1f\n", Snapshot_used, "TB"
pipe pipe quote> printf "\t\t\tSnapshot Used %:%.1f\n", Snapshot_per, "TB"
pipe pipe quote> printf "\t\t\tUsed w/o:%.1f\n", Usedw/o, TB)'
awk: syntax error at source line 1
context is
function >>> print <<< (num)
missing }
missing )
awk: bailing out at source line 7
样品要求
Total Size : 573 TB
Total Used : 448.0 TB
Total Available : 74 TB
Total Used % : 86.1 % # Total Used(%) = Total Used/Total Size *100 = 86.1(%)
Snapshot Used : 3.2 TB
Snapshot Used % : 0.6 %
Used w/o : 85.5 % # {(Total used)-(Snapshot used)} / (Total size) = 85.49%
答案1
原答案:
Totalanswer=$(command1 ; command2)
Subanswer1=$(echo $Totalanswer|grep Sub1)
Subanswer2=$(echo $Totalanswer|grep Sub2)
printf " Answer1:%s\n Answer2:%s\n" $Subanswer1 $SubAnswer2
添加评论:诀窍在于使用正确的工具获取所需的信息。应避免使用外部(即非本机 shell)命令。用 bash 本机替换 grep (暗示)留给读者作为练习。