要求用户从 root 模式运行 makefile

Smi*_*yne 1 linux bash shell makefile

我的项目中有一个 makefile 和配置 shell。我编写了代码来要求用户使用以下代码在 root 模式下运行 configure shell。

[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"
Run Code Online (Sandbox Code Playgroud)

但是当我运行 ' make install' 时,我需要让用户从 root 模式运行。所以我只是从配置 shell 中复制了代码,并将其复制到另一个名为“ runasroot.sh”的shell 脚本文件中。然后我从make install.

install:
    @echo About to install XXX Project
    ./runasroot.sh
    find . -name "*.cgi" -exec cp {}  $(SCRIPTDEST)/ \;
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,出现以下错误。

About to install XXX Project
./runasroot.sh \;
make: *** [install] Error 1
Run Code Online (Sandbox Code Playgroud)

运行根目录

#!/bin/bash
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"
Run Code Online (Sandbox Code Playgroud)

Pip*_*ipo 6

target:
       @if ! [ "$(shell id -u)" = 0 ];then
             @echo "You are not root, run this target as root please"
             exit 1
       fi
Run Code Online (Sandbox Code Playgroud)


Ami*_*IRI 6

说明

例如,您可以用来ifneq检查用户是否不是 root 并回显消息,如果用户确实是用户,则不执行实际操作root。由于用户的 IDroot通常0位于类 UNIX 操作系统上,因此我们可以检查用户 ID0是否在条件中。

提案解决方案

install:
ifneq ($(shell id -u), 0)
    @echo "You must be root to perform this action."
else
    @echo "TODO: The action when the user is root here..."
endif
Run Code Online (Sandbox Code Playgroud)

输出

$ make install
You must be root to perform this action.
$ sudo make install
TODO: The action when the user is root here...
Run Code Online (Sandbox Code Playgroud)

外部资源

Makefile 的条件部分

外壳函数

食谱回响

Man 的 id 函数

什么是root用户