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
?