BashScripts.org
http://bashscripts.org/forum/

a script to set terminal colors with ansi
http://bashscripts.org/forum/viewtopic.php?f=7&t=443
Page 1 of 1

Author:  donnied [ Sun Nov 18, 2007 8:28 pm ]
Post subject:  a script to set terminal colors with ansi

I've been trying to make a script to choose ansi colors to set the terminal.
So far I have:

Code:
#!/bin/bash

clear
echo "Hello, you are logged in as " $USER". What foreground color would you like? "

sleep 1


echo "..........................."
echo "...........MENU............"
echo
echo "1 black "
echo "2 red "
echo "3 green "
echo "4 yellow"
echo "5 blue"
echo "6 purple"
echo "7 cyan"
echo "8 white"



read fore
while [ "$fore" != 0 ];
do
        case $fore in
                1)  clear;
                  export front=[30m ;
                  ;;
                2)  clear;
                  export front=[31m ;
                  ;;
                3)  clear;
                  export front=[32m ;
                  ;;
                4)  clear;
                  export front=[33m ;
                  ;;
                5)  clear;
                  export front=[34m ;
                  ;;
                6)  clear;
                  export front=[35m ;
                  ;;
                7)  clear;
                  export front=[36m ;
                ;;
esac
done
clear
echo "What background color would you like? "

sleep 1

#The Menu
echo "..........................."
echo "...........MENU............"
echo
echo "1 black "
echo "2 red "
echo "3 green "
echo "4 yellow"
echo "5 blue"
echo "6 purple"
echo "7 cyan"
echo "8 white"

read back
while [ "$back" != 0 ];
do
        case $back in

                1)  clear;
                  export rear=[40m ;
                  ;;
                2)  clear;
                  export rear=[41m ;
                  ;;
                3)  clear;
                  export rear=[42m ;
                  ;;
                4)  clear;
                  export rear=[43m ;
                  ;;
                5)  clear;
                  export rear=[44m ;
                  ;;
                6)  clear;
                  export rear=[45m ;
                  ;;
                7)  clear;
                  export rear=[46m ;
                  ;;
                8)  clear;
                  export rear=[47m ;
                  ;;
esac
done
while [ "$back" = "$fore" ];
        do
        echo "You can't read that!!!!"
        ;;

while [ "$back"  != "$fore" ];
        do
        PS1="\[\033[0;$front;$rear\[email protected]\h:\w\$ \]";
        ;;
done
exit



I'm not sure about terminating the while statements with done. It seems to give error messages. I also don't know if I need some sort of eval statement for setting PS1 . The error message write now is from the PS1 line but that is only because I had to # the last two while statements to see what else was happening.

Suggestions?

Author:  donnied [ Wed Nov 21, 2007 11:24 am ]
Post subject: 

I couldn't get that script to work so I started over with:

Code:
PS3="Enter foreground color choice:  "
select fgcc in black red green yellow blue purple cyan white exit
do
case ${fgcc} in
  "black") export fgc=30;;
  "red") export fgc=31;;
  "green") export fgc=32;;
  "yellow") export fgc=32;;
  "blue") export fgc=34;;
  "purple") export fgc=35;;
  "cyan") export fgc=36;;
  "white") export fgc=37;;
  "exit") exit;;
  *) echo "Invalid Choice"
     exit;;
esac
break
done

PS3="Enter background color choice:  "
select bgcc in black red green yellow blue purple cyan white exit
do
case ${bgcc} in
  "black") export bgc=40;;
  "red") export bgc=41;;
  "green") export bgc=42;;
  "yellow") export bgc=33;;
  "blue") export bgc=44;;
  "purple") export bgc=45;;
  "cyan") export bgc=46;;
  "white") export bgc=47;;
  "exit") exit;;
  *) echo "Invalid Choice"
     exit;;
esac
break
done

#PS1=$'[\e[0;'"$fgc;$bgc"'\[email protected]\h:\w\$ \]'
export PS1=$"\[\e[0;$fgc;"$bgc"m\[email protected]\h:\w\$ \]"


Which gives the output that I can use to set the terminal colors but doesn't actually set it. ?

Another question would be how to use it so that the terminal colors changed randomly ever time a command was entered at the command line.

Author:  donnied [ Sat Nov 24, 2007 5:04 pm ]
Post subject: 

putting
Code:
PS1=$"\[\e[0;$fgc;"$bgc"m\[email protected]\h:\w\$ \]"
at the end and running the script with . or source makes it work fine.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com