如何使用 bash 脚本找出 运行 script/command 是否为 root/sudo

How to find out if running script/command as root/sudo using bash script

我正在关注 https://lifehacker.com/add-a-handy-separator-between-commands-in-your-terminal-5840450,以便在 Linux 的终端中的命令之间创建一个很好的分隔符。具体来说,CentOS 8.

我正在尝试修改脚本以输出 运行 命令的用户的用户名。

Here 是我想出的。

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "

reset_style='\[3[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[3[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[3[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[3[1;29m\]' # bold black
# Prompt variable:

OLD_PS1="$PS1"
PS1="$status_style"'$fill $USER \t\n'"$prompt_style$OLD_PS1$command_style"

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG


function prompt_command {

    # create a $fill of all screen width minus the time string and a space and USER and a space:
    let fillsize=${COLUMNS}-10-${#USER}
    fill=""
    while [ "$fillsize" -gt "0" ]
    do
        fill="-${fill}" # fill with underscores to work on 
        let fillsize=${fillsize}-1
    done

    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        bname=`basename "${PWD/$HOME/~}"`
        echo -ne "3]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}[=14=]7"
        ;;
    *)
        ;;
    esac

}
PROMPT_COMMAND=prompt_command

第 15 行在生成的内容中添加了“”和 $USER

第 25 行更改为包含一个额外的 space 和变量的长度 $USER

看起来就像我想要的那样。

Terminal Screenshot

但是,如果我 运行 命令是否为 sudo,我想更新代码以输出。 理想情况下,它将名称更改为 root 或任何 root 用户名。

我尝试了几种方法,主要是我尝试使用 whoami 但这总是 returns 我的用户名不是 root。 如果我 运行 sudo whoami 我得到 root 但不是从脚本。 我也试过EUID没有骰子

此时,我已将带有 $USER 参考的代码保持在工作状态,但我愿意将其更改为所需的任何内容。

u/pLumo

提供的解决方案

Solution Limitations:

  • There are cases not covered, for example sudo --user=some_user .... I think it's fairly easy to further enhance the awk script.
  • As it relies on the history, it won't work with commands you do not have in history, e.g. when using HISTCONTROL=ignoreboth and issue a command with a space in front.
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "

reset_style='\[3[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[3[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[3[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[3[1;29m\]' # bold black
# Prompt variable:

OLD_PS1="$PS1"
PS1="$status_style"'$fill $name \t\n'"$prompt_style$OLD_PS1$command_style"

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG


function prompt_command {

    # create a $fill of all screen width minus the time string and a space and USER and a space:
    name=$(fc -l -1 | awk -v u="$USER" '{if (=="sudo") { if (=="-u") u=; else u="root"; }; printf "%s",u}')
    let fillsize=${COLUMNS}-10-${#name}
    fill=""
    while [ "$fillsize" -gt "0" ]
    do
        fill="-${fill}" # fill with underscores to work on 
        let fillsize=${fillsize}-1
    done

    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        bname=`basename "${PWD/$HOME/~}"`
        echo -ne "3]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}[=10=]7"
        ;;
    *)
        ;;
    esac

}
PROMPT_COMMAND=prompt_command

终端输出:

不确定我是否应该删除这个问题。或者是否有更好的方法来调整它。接受建议。