0

我有以下 bash 脚本,我正在尝试实现,但它失败得很厉害,谁能帮我一些建议......

#!/bin/bash
FILE='/var/www/router.rrd'
OUTPUT='/var/www/router.png'
RRDTOOL='rrdtool graph'
$RRDTOOL $OUTPUT \
        -t "192.168.10.1" -v "Time in ms" \
        --start="now-1d" \
        --end="now" \
        --height="120" \
        --width="440" \
        "DEF:ping_time=$FILE:ping:AVERAGE" \
        "CDEF:shading2=ping_time,0.98,*" "AREA:shading2#F90000:router" \
        "GPRINT:ping_time:LAST:Last\: %5.2lf ms" \
        "GPRINT:ping_time:MIN:Min\: %5.2lf ms" \
        "GPRINT:ping_time:MAX:Max\: %5.2lf ms" \
        "GPRINT:ping_time:AVERAGE:Avg\: %5.2lf ms" >/dev/null


错误消息如下所示

hostname:/# sh -v /var/www/router.sh
FILE='/var/www/router.rrd'
OUTPUT='/var/www/router.png'
RRDTOOL='rrdtool graph'
HOST='hello'
: command not foundre_retea/ping/router.sh: line 6:
"$RRDTOOL" $OUTPUT \
: command not foundre_retea/ping/router.sh: line 7: rrdtool graph
        -t "192.168.10.1" -v "Time in ms" \
/var/www/router.sh: line 8: -t: command not found
        --start="now-1d" \
/var/www/router.sh: line 9: --start=now-1d: command not found
        --end="now" \
/var/www/router.sh: line 10: --end=now: command not found
        --height="120" \
/var/www/router.sh: line 11: --height=120: command not found
        --width="440" \
/var/www/router.sh: line 12: --width=440: command not found
        "DEF:ping_time=$FILE:ping:AVERAGE" \
/var/www/router.sh: line 13: DEF:ping_time=/var/www/router.:ping:AVERAGE: No such file or directory
        "CDEF:shading2=ping_time,0.98,*" "AREA:shading2#F90000:router" \
/var/www/monitorizare_retea/ping/100.100.100.6_graph.sh: line 14: CDEF:shading2=ping_time,0.98,*: command not found
        "GPRINT:ping_time:LAST:Last\: %5.2lf ms" \
.... and so on

4

1 回答 1

1

我的第一个猜测是它会给你一个错误,上面写着“找不到命令”。如果是这种情况,那是因为您将命令和参数放入变量中。不要那样做。请参阅BashFAQ/050

此外,看起来您的脚本文件可能有 Windows 行结尾。如果是这种情况,那么dos2unix在它上面运行将解决这个问题。

dos2unix scriptname

另一种可能性是在续行反斜杠之后可能有空格。删除空白字符可能会有所帮助:

sed 's/\\[[:blank:]]\+$/\\/' scriptname
于 2011-01-22T18:46:42.573 回答