这是在 makefile 中成为 root 的好方法吗?
这是在 makefile 中成为 root 的好方法吗?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不!
首先,通常用户会执行
sudo make install
。其次,您没有考虑将程序安装到自定义目录中的情况,这并不总是需要 root 权限。例如,打包工具使用此功能,并且它们没有 root 权限。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.