Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
    
I don't understand the command line arguments: you want the user to enter a valid colour, then you print the next argument in a random colour. Is that right? –  glenn jackman Nov 12 '14 at 3:05
1  
Note that your slashes are backwards: should be #!/bin/bash –  glenn jackman Nov 12 '14 at 3:06
    
lets say the script name is showc i want when user type in showc random helloo. script print out hello from the random color pool. –  Hajir Golmohammadi Nov 13 '14 at 17:43
    
You want the user to type the word "random"? –  glenn jackman Nov 13 '14 at 18:55
    
yes i want user type the word random after script name.some thing like showc random hello. then terminal display "hello" in random color. –  Hajir Golmohammadi Nov 13 '14 at 19:23

2 Answers 2

With zsh:

zmodload zsh/terminfo
text='some text'
b=(%B '')
print -rP -- ${text//(#m)?/%F{$((RANDOM%terminfo[colors]))}$b[RANDOM%2+1]${MATCH//\%/%%}}%b%f
share|improve this answer

I don't quite understand how you want your script to work. I rewrote it a bit: print out all the command line arguments, each letter in a random colour:

#!/bin/bash

#     [black]=30
#     [red]=31
#     [green]=32
#     [yellow]=33
#     [blue]=34
#     [magenta]=35
#     [cyan]=36
#     [white]=37
function random_colour {
    local bold=$(( $RANDOM % 2 ))
    local code=$(( 30 + $RANDOM % 8 ))
    printf "%d;%d\n" $bold $code
}

sentence="$*"
for (( i=0; i<${#sentence}; i++ )); do
    printf "\e[%sm%c" "$(random_colour)" "${sentence:i:1}"
done
echo -e '\e[0m'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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