如何从Makefile执行SED命令

时间:2016-07-27 18:05:45

标签: bash sed makefile raspbian

所以我试图在运行Raspbian的RaspberryPi上从Makefile运行一个sed命令。当我直接在终端中输入命令时,我正在使用的命令工作正常,但当我尝试从Makefile执行它们时,我得到以下反馈:

sed:-e表达式#1,字符14:命令后的额外字符

#
#  Quick and dirty Makefile for logsnag
#

CC      = gcc

INCLUDE     = -I.

CFILES      = myThing.c
OBJS        = myThing.o

CFLAGS          = ${INCLUDE}

all:    myThing

myThing: ${OBJS}
    ${CC} ${CFLAGS} ${OBJS} -o myThing

myThing.o: ${CFILES}
    ${CC} ${CFLAGS} myThing.c -c

install:    myThing
    sudo cp -f myThing/usr/local/bin
    sudo cp -f ../bin/startlogging.sh /usr/local/bin
    sudo cp -f ../cfg/rotateThing.cfg /etc

    if [ ! -d /var/log/thingLog ]; then\
        sudo mkdir /var/log/thingLog;\
    fi;

    sudo sed -i -e '$i touch /var/log/thingLog/thing.log /var/log/thingLog/myThing \n' /etc/rc.local;
    sudo sed -i -e '$i logrotate -f /etc/rotateThing.cfg \n' /etc/rc.local;
    sudo sed -i -e '$i touch /var/log/thingLog/thing.log /var/log/thingLog/myThing \n' /etc/rc.local;
    sudo sed -i -e '$i /usr/local/bin/startlogging.sh > /var/log/thingLog/myThing 2>&1 & \n' /etc/rc.local;

clean:
    rm -f myThing *.o

1 个答案:

答案 0 :(得分:1)

你的问题是Makefile变量扩展看起来很像shell变量扩展。也就是说,如果Makefile中有一个单字母变量:

X=Some string

然后你将这个变量称为:

$X

所以当你在一个构建节中有这样的命令时:

sed -i -e '$i /usr/local/bin/startlogging.sh > /var/log/thingLog/myThing 2>&1 & \n'

$i被make (带有空字符串)替换为,导致 无效的sed命令。您可以通过转义$来解决此问题 加倍:

sed -i -e '$$i /usr/local/bin/startlogging.sh > /var/log/thingLog/myThing 2>&1 & \n'

讨论in the Make documentation