0

I'm trying to display path in colors but I have problem with this.

I have some constant path - in variable MyPath. I want to create colorized path in PS1 but if pwd showing different path then my (but including MyPath) then the rest I want to print in different color (with slash in different color).

I wrote some code but I don't know hot to apply this to PS1.

It should be something like this :

[ root@192.168.1.199:/ -> /media/user/folder/ ]
# : cd /var/www/html
[ root@192.168.1.199:/ -> /var/www/html/ ] (blue slash and green dir names)
# : cd applications
[ root@192.168.1.199:/ -> /var/www/html/applications/ ] (blue slash and green dir names but last 2 slashes in green color and last dir "application" in red color)
# : cd tmp
[ root@192.168.1.199:/ -> /var/www/html/applications/tmp/ ] (blue slash and green dir names but last 3 slashes in green color and 2 last dirs "application" and "tmp" in red color)

And I got stuck - I don't know how to do this.

My code :

#!/bin/bash
MyPath="/var/www/html"
MyPathLength=$( echo ${MyPath} | wc -m)
CurrentPath="/var/www/html/functions/design"
slashColor="\[$(tput setaf 6)\]/\[$(tput sgr0)\]"
dirColor="\[$(tput setaf 2)\]"
path="";
FinalPath="";

for w in $(echo ${CurrentPath} | tr "/" " "); 
do 
  path="${path}/${w}";
  pathLength=$( echo ${path} | wc -m)

  if [ "${pathLength}" == "${MyPathLength}" ];
  then 
       FinalPath="${FinalPath}\[$(tput setaf 6)\]/\[$(tput sgr0)\]\[$(tput setaf 2)\]$w\[$(tput sgr0)\]\[$(tput setaf 6)\]/\[$(tput sgr0)\]";
  elif [ "${pathLength}" -lt "${MyPathLength}" ];
       then
            FinalPath="${FinalPath}\[$(tput setaf 6)\]/\[$(tput sgr0)\]\[$(tput setaf 2)\]$w\[$(tput sgr0)\]";
       elif [ "${pathLength}" -gt "${MyPathLength}" ];
            then
                 FinalPath="${FinalPath}\[$(tput setaf 1)\]$w\[$(tput sgr0)\]\[$(tput setaf 2)\]/\[$(tput sgr0)\]";
            fi;
done

echo "PS1=\"${FinalPath}\"" > /home/bashrc_split
cd "/";
(
 bash --rcfile /home/bashrc_split # I want to open new shell with new PS1
)

Any help?

1

I'd put something similar to the followig code in .bashrc without requiring other files to be in open&write each time you press the enter key

SEP=("/" "/")
SEP_COLOR=("\e[0;34m" "\e[0;32m")     #colors for: (FIXED - DEFAULT) SEPARATOR STRING
DIR_COLOR=("\e[0;32m" "\e[0;31m")     #colors for: (FIXED - DEFAULT) DIR NAMES
CLOSE_COLOR="\e[0m"

FIXED_DIR=" /var/www/html"
FIXED_DIR=$(realpath ${FIXED_DIR})
FIXED_DIR_ARRAY=()

DIR=${FIXED_DIR}
while [[ "$DIR" != "/" ]]; do
    B=$(basename  -z $DIR)
    DIR=$(dirname -z $DIR) 
    FIXED_DIR_ARRAY+=($B)
done

set_PS1 (){
    local DIR=$PWD
    local CUR_DIR_ARRAY=()

while : ; do
    local B=$(basename  -z $DIR)
    local DIR=$(dirname -z $DIR) 
    CUR_DIR_ARRAY+=($B)
    [[ "$DIR" == "/" ]] && break
done
local SELECTOR=0
local STR=""

local i=1 
while [[ "$i" -le "${#CUR_DIR_ARRAY[@]}" ]] ; do 
    if [ -n $SELECTOR ] &&
       [ $i -gt ${#FIXED_DIR_ARRAY[@]} ] ||
       [ "${CUR_DIR_ARRAY[-$i]}" != "${FIXED_DIR_ARRAY[-$i]}" ];
    then
        SELECTOR=1
    fi  
    local x=$(($SELECTOR%2));
    STR+="${SEP_COLOR[$x]}${SEP[$x]}"
    [[ "${CUR_DIR_ARRAY[-$i]}" != "${SEP[$x]}" ]] &&  STR+="${DIR_COLOR[$x]}${CUR_DIR_ARRAY[-$i]}"
    STR+="${CLOSE_COLOR}"
    ((i++))
done
    printf "${STR}"

}

PS1="[ \u@\h:/ -> \[\$(set_PS1)\] ] "
0

For now it's work good but it must be rewriten. When somebody go to /var/www/html/next_dir/ then next_dir is green and var www and html are red :)

subshell.sh :

cd "/var/www/html";
(
  bash --rcfile /home/username/bashrc_mount;
)

bashrc_mount :

#!/bin/bash
function UpdatePath()
{
    MyPath="$1";
    MyPathLength=$( echo ${MyPath} | wc -m);
    CurrentPath="`pwd`";

    if [ $( echo ${CurrentPath} | grep "${MyPath}" | wc -l) == "0" ];
    then 
         $(cd ${MyPath}"/"); # this doesn't work in subshell - I don't know why... :(
    fi;

    path="";
    FinalPath="";

    for w in $(echo ${CurrentPath} | tr "/" " "); 
    do 
        path="${path}/${w}";
        pathLength=$( echo ${path} | wc -m);

     if [ "${pathLength}" == "${MyPathLength}" ];
     then 
          FinalPath="${FinalPath}$(tput setaf 3)/$(tput sgr0)$(tput setaf 1)$w$(tput sgr0)$(tput setaf 4)/$(tput sgr0)";
     elif [ "${pathLength}" -lt "${MyPathLength}" ];
          then
               FinalPath="${FinalPath}$(tput setaf 3)/$(tput sgr0)$(tput setaf 1)$w$(tput sgr0)";
          elif [ "${pathLength}" -gt "${MyPathLength}" ];
               then
                    FinalPath="${FinalPath}$(tput setaf 2)$w$(tput sgr0)$(tput setaf 4)/$(tput sgr0)";
               fi;
    done
    if [ "${CurrentPath}" == "/" ];
    then 
             FinalPath="$(tput setaf 3)/$(tput sgr0)";
    fi;

    echo "${FinalPath}";
}

DIR="$(pwd)";
PS1="$(tput setaf 4)$(tput bold)[ $(tput setaf 6)\u$(tput setaf 4)@$(tput setaf 1)${address}$(tput setaf 4):$(tput setaf 6)${mountFrom} $(tput setaf 4)]\n\[\$(UpdatePath \$(echo -en \$DIR) \${montTo} )\] \n$(tput setaf 6)\\$ $(tput setaf 7): $(tput sgr0)$(tput sgr0)";

Now I have problem with changing directory

When subshell is started in /var/www/html and somebody will go up (like /var/www) then it should move him to /var/www/html. the same when will do "cd /". For now it doesn't work but after this I'll rewrite code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.