作者Carlchen (chemadent)
看板LinuxDev
标题[问题] Shell script 计算total used space
时间Wed Mar 6 20:33:51 2013
如题,我想用shell script计算df产生之total used space,
但不使用df --total
以下是我的code(看起来很不shell script@@)
我用很奇怪的方法才只印出我要的那个数值,有没有什麽比较优的写法?
另外,想问awk里面能使用在awk外面宣告的变数吗?
比如说在df那行之前有 ans=0 然後想在awk里面使用ans纪录最後的数值,再印出来ans
恳请各位大大解惑 m(__)m
#! /bin/bash
#Program:
# Try to calculate the total used space without using cmd: "df --total"
df | awk 'NR==1{total = 0};NR>=2{total = (total + $3)};{print total}' | sed
-n '$p'
exit 0
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.229.66.69
※ 编辑: Carlchen 来自: 220.229.66.69 (03/06 21:43)
※ 编辑: Carlchen 来自: 220.229.66.69 (03/06 21:45)
※ 编辑: Carlchen 来自: 220.229.66.69 (03/06 22:05)
1F:→ losepacific:df | awk 'BEGIN{total=0}NR>=2{total += $3} 03/07 02:21
2F:→ losepacific:END{print total}' 03/07 02:21
3F:→ losepacific:ans=789 ; awk -v a=${ans} 'BEGIN{print a+1}' 03/07 02:23
4F:→ losepacific:ans=$(awk -v a=${ans} 'BEGIN{print a+1}') 03/07 02:28
5F:→ losepacific:echo $ans 03/07 02:28