Color prompt with file numbers does not work anymore


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Color prompt with file numbers does not work anymore
# 1  
Old 05-10-2013
Color prompt with file numbers does not work anymore

I have used this color prompt on my servers for long time, in file ~\.bashrc
Code:
Black="\[\e[0;30m\]"
Dark="\[\e[Gray 1;30m\]"
Blue="\[\e[0;34m\]"
LBlue="\[\e[1;34m\]"
Green="\[\e[0;32m\]"
LGreen="\[\e[1;32m\]"
Cyan="\[\e[0;36m\]"
LCyan="\[\e[1;36m\]"
Red="\[\e[0;31m\]"
LRed="\[\e[1;31m\]"
Purple="\[\e[0;35m\]"
LPurple="\[\e[1;35m\]"
Brown="\[\e[0;33m\]"
Yellow="\[\e[1;33m\]"
LGray="\[\e[0;37m\]"
White="\[\e[1;37m\]"
Reset="\[\e[0m\]"
PS1="$Yellow\u@\h $LBlue\w $LGreen[$(ls | wc -l):$(($(ls -ad .* | wc -l)-2))]\$([ \$? = 0 ] && echo -e '$LGreen#$Reset' || echo -e '$LRed#$Reset'\)"

It gives a prompt like this
Code:
root$faster \tmp [5:2]#

The numbers [5:2] tells me there are 5 normal files and 2 hidden.

For some reason this does not work anymore when I change directory. I am not sure when this has stopped working. If run the PS1 variable manually, it updates the file number, but this should go automatically.
Anyone could test this as see if it works or not.
I do run my server on Ubuntu 32 and 64 bits 12.04
# 2  
Old 05-12-2013
You are missing a backslash:
Code:
PS1="$Yellow\u@\h $LBlue\w $LGreen[\$(ls | wc -l):$(($(ls -ad .* | wc -l)-2))]\$([ \$? = 0 ] && echo -e '$LGreen#$Reset' || echo -e '$LRed#$Reset'\)"

# 3  
Old 05-12-2013
Thanks you for looking at this, but your solution broke other stuff. It now shows correct visible files in a folder.
This part $(($(ls -ad .* | wc -l)-2)) does not more list the hidden files. Test in home folder.

\$([ \$? = 0 ] && echo -e '$LGreen#$Reset' || echo -e '$LRed#$Reset'\)
Exit code no longer change color on # from green to red when some is wrong.

\$(($(ls -ad .* | wc -l)-2))
This works some, but only give correct number of files when its run.
Changing folder does not update the numbers.

---------- Post updated at 09:49 ---------- Previous update was at 09:08 ----------

New version that do work. (did copy some from internet)

Code:
function exitstatus {

    Exit="$?"
	Black="\[\e[0;30m\]"
	Dark="\[\e[1;30m\]"
	Blue="\[\e[0;34m\]"
	LBlue="\[\e[1;34m\]"
	Green="\[\e[0;32m\]"
	LGreen="\[\e[1;32m\]"
	Cyan="\[\e[0;36m\]"
	LCyan="\[\e[1;36m\]"
	Red="\[\e[0;31m\]"
	LRed="\[\e[1;31m\]"
	Purple="\[\e[0;35m\]"
	LPurple="\[\e[1;35m\]"
	Brown="\[\e[0;33m\]"
	Yellow="\[\e[1;33m\]"
	LGray="\[\e[0;37m\]"
	White="\[\e[1;37m\]"
	Off="\[\e[0m\]"
	Files="[$(ls | wc -l):$(($(ls -ad .* | wc -l)-2))]"

    Prompt="${Yellow}\u@\h ${LBlue}\w ${LGreen}${Files}"

    if [ "${Exit}" -eq 0 ]
    then
       PS1="${Prompt}${LGreen}#${Off}"
    else
       PS1="${Prompt}${Red}#${Off}"
    fi
}

PROMPT_COMMAND=exitstatus


Last edited by Jotne; 05-12-2013 at 05:39 AM..
# 4  
Old 05-12-2013
Quote:
Originally Posted by Jotne
Thanks you for looking at this, but your solution broke other stuff.
It didn't.
The other stuff was already broken given the fact I didn't change anything elsewhere and my change doesn't impact but the number of non hidden files displayed.
# 5  
Old 05-17-2013
Fixed it. Used some hour to get it correct
Code:
PS1='\[\e[1;33m\]\u@\h \[\e[1;34m\]\w $(es=$?; [[ $es -ne 0 ]] && echo "\[\e[1;31m\]" || echo "\[\e[1;32m\]")[$(ls | wc -l):$(($(ls -ad .* | wc -l)-2))]\$\[\e[0m\]'

Normal exit
root@server2 /etc [172:1]#

Error exit
root@server2 /etc [172:1]#
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

RHEL6 customizing prompt do not work

Hi, I'm trying to customize the ksh prompt for users on a RHEL 6.6 system for having user@host pwd : $ and user@host pwd # in red color for root. I think it's possible but i do not even succeded for a non root user : I added in my ~/.kshrc : PS1="Hello : " and it works but when i... (4 Replies)
Discussion started by: Fundix
4 Replies

2. UNIX for Advanced & Expert Users

Libvirt does not work correctly anymore on my gentoo

Hi, Since a year my libvirtd does not work anymore on my Gentoodesktop. In the meantime a used virtualbox. But I would like to have back libvirt. The problem was after libvirt should not only work with root privileges. I deinstalled all things with libvirt an kvm. I removed all things from /var... (4 Replies)
Discussion started by: darktux
4 Replies

3. Shell Programming and Scripting

zsh, prompt, variable expansion, ANSI color sequences

Hi! I am using latest ZSH and I have setopt prompt_subst I have a global hash array variable which contains some color definitions with color names as keys and 256-color ANSI codes (with %{ and %}) as values, eg %{\ePS1="$FG Hello World (%M) > " or PS1=$'$FG Hello World (%M) > ' then it... (3 Replies)
Discussion started by: galanom
3 Replies

4. UNIX for Advanced & Expert Users

pkill won't work on firefox anymore

Can someone tell me why pkill won't work on firefox anymore? (11 Replies)
Discussion started by: cokedude
11 Replies

5. Shell Programming and Scripting

Color on echo output does not work

Hi, When I run: echo "\033I see hi in color, but if I run this color is not shown, why? (echo "\033Thanks Israel. ---------- Post updated at 05:17 AM ---------- Previous update was at 04:43 AM ---------- DONE!! I had to run more -v. Thanks (4 Replies)
Discussion started by: iga3725
4 Replies

6. Shell Programming and Scripting

Extract numbers from text file work out average

Just wondering if someone could assist me with shell script I'm trying to write. I need to read the final column of a text file (shown below) and workout what the average number is. The text file will have a variable number of lines, I just want the script to pull out the values in the final field... (14 Replies)
Discussion started by: rich@ardz
14 Replies

7. Shell Programming and Scripting

tcsh/csh: set prompt in production to color red

Hi folks This is our prompt at the moment oracle@pinkipinki:/opt/oracle> grep 'set prompt' .cshrc set prompt = "$user@`uname -n`:$cwd> " We wish to have in production the same prompt, but red. Howto do that? I tried a lot a internet manuals, but it doesn't work. (1 Reply)
Discussion started by: slashdotweenie
1 Replies

8. UNIX for Dummies Questions & Answers

connection doesn't work anymore

Hello, first of all, I want to make myself clear about my language. I'm brazilian, so I ask you all to understand if i commit any mistake with the grammar. Here is the problem. Some days ago I needed to use a "sh" command in the Terminal (I use a Mac OSX 10.5.6) followed by a file... (0 Replies)
Discussion started by: anubisbr
0 Replies

9. Shell Programming and Scripting

How to change prompt color when changing path

Hi all, Can you tell me how to change the prompt color (only the path part) when I chnange directory with "cd"? I use the sequence below in ".bashrc" (Solaris 8) to change my prompt colors and I'd like to modify it to change the path color when I cange directory. PSC() { echo -ne "\"; }... (0 Replies)
Discussion started by: majormark
0 Replies
Login or Register to Ask a Question