当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Is this a good way to be root in a makefile?

问题描述:

Is this a good way to be root in a makefile?

SHELL = /bin/sh

INSTDIR = /usr/bin/

OBJS = main.o file.o gen.o stat.o

program1: $(OBJS)

gcc -o program1 $(OBJS)

main.o: main.c file.h gen.h stat.h

gcc -c main.c

file.o: file.c file.h

gcc -c file.c

gen.o: gen.c gen.h

gcc -c gen.c

stat.o: stat.c stat.h

gcc -c stat.c

clean:

rm -f $(OBJS) program1

install:

@if [ -f program1 ]; then \

if [ $$(id -u) -eq 0 ]; then \

cp program1 $(INSTDIR) && \

echo "Installed in $(INSTDIR)" ; \

else \

(sudo cp program1 $(INSTDIR) 2> /dev/null && \

echo "Installed in $(INSTDIR)") || \

(echo 'No sudo on this machine, trying su.' \

&& su -c "cp program1 $(INSTDIR)" && \

echo "Installed in $(INSTDIR)") ; \

fi ; \

else \

echo "There was no program to install, run make." ; \

fi

uninstall:

@if [ -f $(INSTDIR)program1 ]; then \

if [ $$(id -u) -eq 0 ]; then \

rm $(INSTDIR)program1 && \

echo "Uninstalled in $(INSTDIR)" ; \

else \

(sudo rm $(INSTDIR)program1 2> /dev/null && \

echo "Uninstalled in $(INSTDIR)") || \

(echo 'No sudo on this machine, trying su.' \

&& su -c "rm $(INSTDIR)program1" && \

echo "Uninstalled in $(INSTDIR)") ; \

fi ; \

else \

echo "There was no program to remove." ; \

fi

网友答案:

NO!

First, generally users do sudo make install. Second, you didn't think about cases when the program is installed into a custom directory, which doesn't always need root privileges. Packaging tools use this feature, for example, and they don't have root privileges.

分享给朋友:
您可能感兴趣的文章:
随机阅读: