RRDTool GPRINT使用printf格式化

gen*_*ion 5 linux bash printf sed rrdtool

与此问题密切相关:Bash 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"
Run Code Online (Sandbox Code Playgroud)

产生这样的图形(请注意\图例中低成本线的开头):

RRD一周功率

专心于以下几行:

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

这是在图例中打印出成本较低的行的行。

我正在将GPRINT格式的值传递£4.54给Bash的printf函数,以将其填充为11个空格,并cost在其上添加一个标签。然后,我sed将其用于在£和实际值之间添加一个空格。

我想知道的是,为什么\输出中会转义?如果我删除了\\printf bash之后,它将抱怨缺少某些内容。

我如何抑制这种情况\通过输出。

sar*_*old 5

试试这一行:

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

我将内部 "标记更改为'标记并删除了反斜杠。