I have the array (shown below) with arguments that hold color codes. The intended purpose of my script is to randomize those arguments when the user inputs "scriptname randoms Text" so that every time it will display the "Text" on the screen in a different color. Below I wrote some if statements not related to this particular problem, they work after i tested them. The problem I have is with randomizing colours when the user inputs "scriptname randoms Text".
#!\bin\bash
declare -A colours=(
[black]="0;30" [red]="0;31" [green]="0;32" [yellow]="0;33" [blue]="0;34" [magenta]="0;35" [cyan]="0;36" [white]="0;37"
[BLACK]="1;30" [RED]="1;31" [GREEN]="1;32" [YELLOW]="1;33" [BLUE]="1;34" [MAGENTA]="1;35" [CYAN]="1;36" [WHITE]="1;37"
[bLaCk]="0;30" [rEd]="0;31" [gReEn]="0;32" [yElLoW]="0;33" [bLuE]="0;34" [mAgEnTa]="0;35" [cYaN]="0;36" [wHiTe]="0;37"
[BlAcK]="1;30" [ReD]="1;31" [GrEeN]="1;32" [YeLlOW]="1;33" [BlUe]="1;34" [MaGeNtA]="1;35" [CyAn]="1;36" [WhItE]="1;37")
if [ $# -lt 2 ]; then
echo Usage: echoc COLOUR STRING
exit 1
fi
regex='^black$|^red$|^green$|^yellow$|^blue$|^magenta$|^cyan$|^white$'
randoms=$(( $RANDOM % $regex )); echo $randoms
shopt -s nocasematch
if [[ $1 =~ $randoms ]] ; then
echo -ne '\e['${colours[$randoms]}m
shift
echo -e $*'\e[0m'
exit 2
fi
I would appreciate some feedback. Thanks in advance.
#!/bin/bash
– glenn jackman Nov 12 '14 at 3:06