2017-07-28 18 views
0

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

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

回答

1

你可以尝试用prompt_command运行你的函数,如果我uderstood你正在尝试做的(并确保你得到的$?值,否则该值将改变之前,不执行任何操作):

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 
相关问题