2012-07-05 58 views
4

與此密切相關的問題格式:Bash printf prefixRRDTool的GPRINT用printf的

我有一個生成與RRDTool的一個RRDGraph以下bash腳本。

#!/bin/bash 

now=$(date +%s) 
now_formatted=$(date +%s | awk '{printf "%s\n", strftime("%c",$1)}' | sed -e 's/:/\\:/g') 

# create power graph for last week 
/usr/bin/rrdtool graph /var/www/power-week.png \ 
--start end-7d --width 543 --height 267 --end $now-1min --slope-mode \ 
--vertical-label "Watts" --lower-limit 0 \ 
--alt-autoscale-max \ 
--title "Power: Last week vs. week before" \ 
--watermark "(©) $(date +%Y) Alyn R. Tiedtke" \ 
--font WATERMARK:8 \ 
DEF:Power=/root/currentcost/ccdata.rrd:Power:AVERAGE \ 
DEF:Power2=/root/currentcost/ccdata.rrd:Power:AVERAGE:end=$now-7d1min:start=end-7d \ 
VDEF:Last=Power,LAST \ 
VDEF:First=Power,FIRST \ 
VDEF:Min=Power,MINIMUM \ 
VDEF:Peak=Power,MAXIMUM \ 
VDEF:Average=Power,AVERAGE \ 
CDEF:kWh=Power,1000,/,168,* \ 
CDEF:Cost=kWh,.1029,* \ 
SHIFT:Power2:604800 \ 
LINE1:Power2#00CF00FF:"Last Week\\n" \ 
HRULE:Min#58FAF4:"Min " \ 
GPRINT:Power:MIN:"%6.2lf%sW" \ 
COMMENT:"\\n" \ 
LINE1:Power#005199FF:"Power " \ 
AREA:Power#00519933:"" \ 
GPRINT:Last:"%6.2lf%sW" \ 
COMMENT:"\\n" \ 
HRULE:Average#9595FF:"Average" \ 
GPRINT:Power:AVERAGE:"%6.2lf%sW" \ 
COMMENT:"\\n" \ 
HRULE:Peak#ff0000:"Peak " \ 
GPRINT:Power:MAX:"%6.2lf%sW" \ 
COMMENT:"\\n" \ 
GPRINT:kWh:AVERAGE:" total %6.2lfkWh\\n" \ 
GPRINT:Cost:AVERAGE:" cost  %6.2lf £\\n" \ 
GPRINT:Cost:AVERAGE:"$(printf \\" cost %11s\\" £%.2lf | sed 's/\£/\£ /g')\\n" \ 
COMMENT:" \\n" \ 
GPRINT:First:"Showing from %c\\n":strftime \ 
GPRINT:Last:"   to %c\\n":strftime \ 
COMMENT:" Created at $now_formatted" 

將會產生這樣的曲線圖(注意在圖例中的較低成本的線領先\): -

RRD Power over Week

上下面的行具體濃縮: -

GPRINT:Cost:AVERAGE:"$(printf \\" cost %11s\\" £%.2lf | sed 's/\£/\£ /g')\\n" \ 

這是打印出圖例中較低成本線的線。

我傳遞的£4.54一個GPRINT格式化值bash的printf功能對其進行填充以11位和cost標籤前綴就可以了。然後,我將此管道傳送到sed以在£和實際值之間添加一個空格。

我想知道的是,爲什麼逃出的\通過輸出?如果我在printf bash抱怨缺少某些東西后立即刪除\\

我該如何在輸出中抑制這個\

回答

4

試試這個行:

GPRINT:Cost:AVERAGE:"$(printf ' cost %11s' £%.2lf | sed 's/\£/\£ /g')\\n" \ 

我改變了"'馬克和刪除反斜槓。

+0

哦多麼簡單。完美的作品! – 2012-07-05 22:31:12