linux - Pass last command status to $PS1 function -
this question has answer here:
- bash prompt last exit code 4 answers
so have below code generating command line prompt:
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 ]]; local user=$lred$user$white; local bash=$white"#"; else local user=$lgreen$user$white; fi if [[ $1 -eq 0 ]]; 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 \$?`"; the problem in these lines:
# ... if [[ $1 -eq 0 ]]; local check=$lgreen"✔"$white; else local check=$lred"✘"$white; fi # ... export ps1="`custom_prompt \$?`"; i trying check see if last command had error. however, can't seem figure out how pass information correctly if statement work.
my question: how pass value of $? custom_prompt?
you can try running function prompt_command, if uderstood trying (and sure not execute before value of $? otherwise value change):
function custom_prompt { # $? first it's not modified 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 ]]; local user=$lred$user$white; local bash=$white"#"; else local user=$lgreen$user$white; fi if [[ $exit_status -eq 0 ]]; 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
Comments
Post a Comment