bashスクリプトを使用してスクリプト/コマンドをroot / sudoとして実行するかどうかを決定する方法

bashスクリプトを使用してスクリプト/コマンドをroot / sudoとして実行するかどうかを決定する方法

私はフォローしていますhttps://lifehacker.com/add-a-handy-separator-Between-commands-in-your-terminal-5840450Linux端末では、コマンド間にクールな区切り文字を作成します。特にCentOS 8.

コマンドを実行しているユーザーのユーザー名を出力するようにスクリプトを変更しようとしています。

こここれが私が思いついたものです。

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

reset_style='\[\033[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[\033[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[\033[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 "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
        ;;
    *)
        ;;
    esac

}
PROMPT_COMMAND=prompt_command

行15では、$USER生成されたコンテンツに「」と「」を追加します。

追加のスペースと変数の長さを含むように25行が変更されました。$USER

私が欲しいものと同じように見えます。

ターミナルスクリーンショット

ただし、コマンドを実行または実行しない場合は、出力するコードを更新したいと思いますsudo。理想的には、名前をrootまたはrootユーザー名に変更します。

私は主にを使用しようとしましたが、whoami常にrootの代わりに私のユーザー名を返します。実行するとsudo whoamiroot アクセス権を取得できますが、スクリプトでは取得できません。私もEUIDサイコロなしで試してみました。

この時点ではコードを参照のある動作状態にしておきますが、$USER必要に応じて変更する意向があります。

ソリューションプロバイダーu/pルモ

ソリューションの限界:

  • sudo --user=some_user... などのいくつかのケースは扱いません。 awkスクリプトをさらに強化するのはかなり簡単だと思います。
  • 履歴に依存するため、HISTControl=ignoreboth を使用して前に空白のあるコマンドを実行する場合など、履歴にないコマンドには機能しません。
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "

reset_style='\[\033[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[\033[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[\033[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 ($2=="sudo") { if ($3=="-u") u=$4; 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 "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
        ;;
    *)
        ;;
    esac

}
PROMPT_COMMAND=prompt_command

ターミナル出力 - ソリューション

ベストアンサー1

prompt_commandどのユーザーが最後のコマンドを実行したのかわかりません。prompt_command常に通常のユーザーセッションで実行します。

回避策として。読んで解析できますhistory

たとえば、fc -l -1最後のコマンドを印刷しawk ...て解析する場合です。


オンラインに#15変更$USER$name

行に#23以下を追加します。

name=$(fc -l -1 | awk -v u="$USER" '{if ($2=="sudo") { if ($3=="-u") u=$4; else u="root"; }; printf "%s",u}')

オンラインに変更してください#25${#USER}${#name}


これはおよびに対してroot印刷されます。sudo some commandsome_usersudo -u some_user some command

ただし、このソリューションにはいくつかの制限があります。

  • たとえば、いくつかの状況は扱いません sudo --user=some_user ...。私はスクリプトをさらに改善するのがかなり簡単だと思いますawk
  • に依存するため、history存在しないコマンドは使用できませんhistory。たとえば、HISTCONTROL=ignoreboth空白が前のコマンドを使用して実行する場合です。

おすすめ記事