0

So I have the below code generating my 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 ]]; 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 \$?`";

The problem is in these lines:

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

I am trying to check to see if the last command had an error. However, I can't seem to figure out how to pass back the information correctly for the IF statement to work.

My Question: How do I pass back the value of $? to custom_prompt?

Nicholas Summers
  • 4,444
  • 4
  • 19
  • 35

1 Answers1

2

You can try running your function with prompt_command, if I uderstood what you are trying to do (and be sure to not execute anything before you get the value of $? otherwise the value will change):

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
MauricioRobayo
  • 2,207
  • 23
  • 26