【问题标题】:Pass last command status to $PS1 function [duplicate]将最后一个命令状态传递给 $PS1 函数 [重复]
【发布时间】:2018-01-05 01:30:06
【问题描述】:

所以我有下面的代码生成我的命令行提示符:

function custom_prompt {
    local black="\\[\\033[38;5;0m\\]";
    local red="\\[\\033[38;5;1m\\]";
    local green="\\[\\033[38;5;2m\\]";
    local yellow="\\[\\033[38;5;3m\\]";
    local blue="\\[\\033[38;5;4m\\]";
    local magenta="\\[\\033[38;5;5m\\]";
    local cyan="\\[\\033[38;5;6m\\]";
    local lgray="\\[\\033[38;5;7m\\]";
    local gray="\\[\\033[38;5;8m\\]";
    local lred="\\[\\033[38;5;9m\\]";
    local lgreen="\\[\\033[38;5;10m\\]";
    local lyellow="\\[\\033[38;5;11m\\]";
    local lblue="\\[\\033[38;5;12m\\]";
    local lmagenta="\\[\\033[38;5;13m\\]";
    local lcyan="\\[\\033[38;5;14m\\]";
    local white="\\[$(tput sgr0)\\]";
    local bold="\\[$(tput bold)\\]";

    local n="\\n";

    local user="`whoami`";
    local host="`hostname`";
    local path=$white"`pwd`";
    local date=$magenta"`date`"$white;
    local items="$(ls -A1 | wc -l | sed 's: ::g') Items"
    local folders="$(ls -Ap1 | grep / | wc -l | sed 's: ::g') Folders"
    local files="$(ls -Ap1 | grep -v / | wc -l | sed 's: ::g') Files"
    local size="$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b"
    local listing=$cyan"("$folders", "$files", "$size")"$white
    local bash=$white"$"
    if [[ $EUID -eq 0 ]]; then
        local user=$lred$user$white;
        local bash=$white"#";
    else
        local user=$lgreen$user$white;
    fi
    if [[ $1 -eq 0 ]]; then
        local check=$lgreen"✔"$white;
    else
        local check=$lred"✘"$white;
    fi
    local authority=$user$lgreen"@"$host$white;

    echo $n$authority" "$listing" "$path$n$date" "$check" "$bash": ";
}
export PS1="`custom_prompt \$?`";

问题出在以下几行:

# ...
if [[ $1 -eq 0 ]]; then
    local check=$lgreen"✔"$white;
else
    local check=$lred"✘"$white;
fi
# ...
export PS1="`custom_prompt \$?`";

我正在尝试检查最后一个命令是否有错误。但是,我似乎无法弄清楚如何正确传回信息以使 IF 语句起作用。

我的问题:如何将$? 的值传递回custom_prompt

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    如果我了解您要执行的操作,您可以尝试使用prompt_command 运行您的函数(确保在获得$? 的值之前不要执行任何操作,否则该值会改变):

    function custom_prompt {
    
        # get $? first so it's not modified by any other command
        local exit_status=$?
    
        local black="\\[\\033[38;5;0m\\]";
        local red="\\[\\033[38;5;1m\\]";
        local green="\\[\\033[38;5;2m\\]";
        local yellow="\\[\\033[38;5;3m\\]";
        local blue="\\[\\033[38;5;4m\\]";
        local magenta="\\[\\033[38;5;5m\\]";
        local cyan="\\[\\033[38;5;6m\\]";
        local lgray="\\[\\033[38;5;7m\\]";
        local gray="\\[\\033[38;5;8m\\]";
        local lred="\\[\\033[38;5;9m\\]";
        local lgreen="\\[\\033[38;5;10m\\]";
        local lyellow="\\[\\033[38;5;11m\\]";
        local lblue="\\[\\033[38;5;12m\\]";
        local lmagenta="\\[\\033[38;5;13m\\]";
        local lcyan="\\[\\033[38;5;14m\\]";
        local white="\\[$(tput sgr0)\\]";
        local bold="\\[$(tput bold)\\]";
    
        local n="\\n";
    
        local user="`whoami`";
        local host="`hostname`";
        local path=$white"`pwd`";
        local date=$magenta"`date`"$white;
        local items="$(ls -A1 | wc -l | sed 's: ::g') Items"
        local folders="$(ls -Ap1 | grep / | wc -l | sed 's: ::g') Folders"
        local files="$(ls -Ap1 | grep -v / | wc -l | sed 's: ::g') Files"
        local size="$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b"
        local listing=$cyan"("$folders", "$files", "$size")"$white
        local bash=$white"$"
        if [[ $EUID -eq 0 ]]; then
             local user=$lred$user$white;
             local bash=$white"#";
        else
             local user=$lgreen$user$white;
        fi
        if [[ $exit_status -eq 0 ]]; then
             local check=$lgreen"✔"$white;
        else
           local check=$lred"✘"$white;
        fi
        local authority=$user$lgreen"@"$host$white;
    
        PS1=$n$authority" "$listing" "$path$n$date" "$check" "$bash": ";
     }
     PROMPT_COMMAND=custom_prompt
    

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2017-01-30
      • 2010-11-20
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 2011-07-03
      相关资源
      最近更新 更多