You are not logged in.

#1 2012-11-24 19:02:37

rix
Member
Registered: 2012-07-25
Posts: 221

[SOLVED] Bash changes consolle's colors by itself

Hi archers,

I've wrote a little script which ask for the result of a given random moltiplication because I was exercising with $RANDOM and something strange's happend.
No matter where I am (Tty, Tmux, Urxvt, Xterm...) Bash changes consolle's colors by itself if I do ^c to stop the script, instead of write a reply.

i.e.'s:
correct behaviour

$ 8 * 9 = ?
$ 72
$ ls
file in red    file in yellow    file in grey...

Wrong behaviour

$ 8 *9 = ?
$ ^c
$ ls
file in blue    file in green    file in white...

What the heck?

Bash version is 4.2.39(2)-release.
The script is the following:

while :
        do
            min="3"
            max="7"
            
            fat1="$(($RANDOM%$max+$min))"
            fat2="$(($RANDOM%$max+$min))"

            prod="$(($fat1*$fat2))"
            echo "$fat1 * $fat2 = ?"
            read res
            
            while [ "$res" != "$prod" ]
                do
                    echo "$prod"
                    echo "Insert the correct result."
                    tavPit
            done
            
            return 0
    done
}

Everything is up to date.

.bashrc

[[ $- != *i* ]] && return

complete -cf sudo
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion

[ -f ~/.bash/include ] && . ~/.bash/include

[ -e "$HOME"/.dircolors ] && eval $(dircolors -b "$HOME"/.dircolors)

include

#!/bin/bash
[ -f ~/.bash/alias ] && . ~/.bash/alias
[ -f ~/.bash/color ] && . ~/.bash/color
[ -f ~/.bash/export ] && . ~/.bash/export
[ -f ~/.bash/shopt ] && . ~/.bash/shopt
[ -f ~/.bash/stty ] && . ~/.bash/stty
[ -f ~/.bash/set ] && . ~/.bash/set

color

nc="\e[0m"
nbk="\e[0;30m"
nre="\e[0;31m"
ngr="\e[0;32m"
nye="\e[0;33m"
nbl="\e[0;34m"
nma="\e[0;35m"
ncy="\e[0;36m"
nwh="\e[0;37m"
bbk="\e[1;30m"
bre="\e[1;31m"
bgr="\e[1;32m"
bye="\e[1;33m"
bbl="\e[1;34m"
bma="\e[1;35m"
bcy="\e[1;36m"
bwh="\e[1;37m"
end="\[\e[m\]"

man() {
    env \
        LESS_TERMCAP_mb=$(printf "\e[0;32m") \
        LESS_TERMCAP_md=$(printf "\e[0;32m") \
        LESS_TERMCAP_me=$(printf "\e[0m") \
        LESS_TERMCAP_se=$(printf "\e[0m") \
        LESS_TERMCAP_so=$(printf "\e[1;31m") \
        LESS_TERMCAP_ue=$(printf "\e[0m") \
        LESS_TERMCAP_us=$(printf "\e[1;33m") \
    man "${@}"
}

if [ "$TERM" = "linux" ]; then
    echo -en "\e]P0000000" # Black.
    echo -en "\e]P9ff0000" # Red.
    echo -en "\e]PA00ff00" # Green.
    echo -en "\e]PBffff00" # Yellow.
    echo -en "\e]PC2b4f98" # Blue.
    echo -en "\e]PDff00ff" # Magenta.
    echo -en "\e]PE00ffff" # Cyan.
    echo -en "\e]PFffffff" # White.
    clear
fi

.dircolors

TERM linux
TERM linux+utf8
TERM rxvt-unicode
TERM rxvt-unicode-256color
TERM screen
TERM screen-256color
TERM xterm
TERM putty

EIGHTBIT 1

NORMAL          01;30
FILE            01;30
DIR                31
LINK               36
FIFO            03;33
SOCK            03;33
DOOR               32
BLK                32
CHR                32
ORPHAN          05;33
EXEC               33
    
.tar               31
.tgz               31
.arj               31
.taz	           31
.lzh	           31
.zip	           31
.7z 	           31
.z  	           31
.Z  	           31
.gz 	           31
.bz2	           31
.deb	           31
.rpm	           31
.jar	           31
.rar	           31
.xz        	       31

.jpg               35
.jpeg              35
.gif               35
.bmp               35
.pbm               35
.pgm               35
.ppm               35
.tga               35
.xbm               35
.xpm               35
.tif               35
.tiff              35
.png               35
.fli               35
.gl                35
.dl                35
.xcf               35
.xwd               35
.pdf               35

.ogg               34
.mp3               34
.wav               34
.mov               34
.mpg               34
.mpeg              34
.asf               34
.avi               34
.mkv               34
.wmv               34
.ogm              34

.C                37
.H                37
.c                37
.h                37
.cxx              37
.hxx              37
.cpp              37
.hpp              37
.py               37
.sh               37
.vim              37

.o                 37
.so               37
.a                 37
.ko                37

.rc                36
*rc                36

Thanks a lot.

Last edited by rix (2012-11-26 15:38:28)

Offline

#2 2012-11-25 21:34:48

rix
Member
Registered: 2012-07-25
Posts: 221

Re: [SOLVED] Bash changes consolle's colors by itself

No ideas?

Offline

#3 2012-11-25 21:54:22

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,348
Website

Re: [SOLVED] Bash changes consolle's colors by itself

You haven't provided enough information. For example, what is tavPit?


Also, moving to programming and scripting...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#4 2012-11-25 22:03:33

rix
Member
Registered: 2012-07-25
Posts: 221

Re: [SOLVED] Bash changes consolle's colors by itself

Sorry, I didn't notice it. A really little forgetfulness.

This way it'd be more clear:

tavPit() {
    while :
        do
            min="3"
            max="7"
            
            fat1="$(($RANDOM%$max+$min))"
            fat2="$(($RANDOM%$max+$min))"

            prod="$(($fat1*$fat2))"
            echo "$fat1 * $fat2 = ?"
            read res
            
            while [ "$res" != "$prod" ]
                do
                    echo "$prod"
                    echo "Insert the correct result."
                    tavPit
            done
            
            return 0
    done
}
tavPit

Isn't it?

Offline

#5 2012-11-25 22:25:07

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,013

Re: [SOLVED] Bash changes consolle's colors by itself

tavPit starts a while loop which calls tavPit which starts a while loop which calls tavPit which starts a while loop...
Is that really what you want? Don't know how that would affect the colours tho...


You're just jealous because the voices only talk to me.

Offline

#6 2012-11-25 22:29:33

rix
Member
Registered: 2012-07-25
Posts: 221

Re: [SOLVED] Bash changes consolle's colors by itself

Thanks for the reply, btw the script works as aspected, I don't said the script is the problem but that I don't know what is the problem.

Offline

#7 2012-11-25 22:43:46

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 12,436

Re: [SOLVED] Bash changes consolle's colors by itself

rix wrote:

...                   
Please let me know if I make English mistakes.
Thanks.

aspectedexpected


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Like you, I have no idea what you are doing, but I am pretty sure it is wrong...Jasonwryan
----
How to Ask Questions the Smart Way

Offline

#8 2012-11-25 22:52:15

rix
Member
Registered: 2012-07-25
Posts: 221

Re: [SOLVED] Bash changes consolle's colors by itself

@ ewaller:
thanks a lot. smile

Offline

#9 2012-11-26 00:07:16

Bandit Bowman
Member
Registered: 2010-08-25
Posts: 18

Re: [SOLVED] Bash changes consolle's colors by itself

My colours are unaffected after running the function. What does $LS_COLORS look like before and after? Is this the whole script or just a snippet because I don't see anything that should modify it.

The function is a bit convoluted, here's a slightly more straightforward version that should work the same, although it won't help with the colours.

tavPit() {
    while :
        do
            min="3"
            max="7"
            
            fat1="$(($RANDOM%$max+$min))"
            fat2="$(($RANDOM%$max+$min))"

            prod="$(($fat1*$fat2))"
            echo "$fat1 * $fat2 = ?"
            read res
            
            if [ "$res" == "$prod" ]
            then
                return 0
	    else
                echo "$prod"
                echo "Insert the correct result."
            fi
    done
}

English:
consolle => console
I've wrote => I wrote  or  I've written
which ask for => which asks for
moltiplication => multiplication
exercising with $RANDOM => using $RANDOM
something strange's happend => something strange happened

Offline

#10 2012-11-26 15:11:06

rix
Member
Registered: 2012-07-25
Posts: 221

Re: [SOLVED] Bash changes consolle's colors by itself

Bandit Bowman wrote:

My colours are unaffected after running the function. [...]

Here colors change when I send SIGINT to a running instance of my script; as said before.

Bandit Bowman wrote:

[...] What does $LS_COLORS look like before and after? [...]

When everything is working right:

$ echo $LS_COLORS
$ no=01;30:fi=01;30:di=31:ln=36:pi=03;33:so=03;33:do=32:bd=32:cd=32:or=05;33:ex=33:
*.tar=31:*.tgz=31:*.arj=31:*.taz=31:*.lzh=31:*.zip=31:*.7z=31:*.z=31:*.Z=31:*.gz=31:
*.bz2=31:*.deb=31:*.rpm=31:*.jar=31:*.rar=31:*.xz=31:*.jpg=35:*.jpeg=35:*.gif=35:
*.bmp=35:*.pbm=35:*.pgm=35:*.ppm=35:*.tga=35:*.xbm=35:*.xpm=35:*.tif=35:*.tiff=35:
*.png=35:*.fli=35:*.gl=35:*.dl=35:*.xcf=35:*.xwd=35:*.pdf=35:*.ogg=34:*.mp3=34:
*.wav=34:*.mov=34:*.mpg=34:*.mpeg=34:*.asf=34:*.avi=34:*.mkv=34:*.wmv=34:*.ogm=34:
*.C=37:*.H=37:*.c=37:*.h=37:*.cxx=37:*.hxx=37:*.cpp=37:*.hpp=37:*.py=37:*.sh=37:
*.vim=37:*.o=37:*.so=37:*.a=37:*.ko=37:*.rc=36:*rc=36:

When texts change color (the strange behaviour):

$ echo $LS_COLORS
$
Bandit Bowman wrote:

[...] Is this the whole script or just a snippet [...]

Whole.

Bandit Bowman wrote:

[...] because I don't see anything that should modify it. [...]

That's why I'm asking.

Bandit Bowman wrote:

[...] here's a slightly more straightforward version [...]

Thanks, I've learnt something.

Bandit Bowman wrote:

[...] although it won't help with the colours.

Thanks for the reply anyway.

Bandit Bowman wrote:

[...] English: [...]

Also, many thanks again.


Edit: solved. "$LS_COLORS" must be set if you want colors.

Last edited by rix (2012-11-26 15:38:06)

Offline

Board footer

Powered by FluxBB