Replacing subString in an array with bash

0

1

What I got:

array[0]="Programm is Running"
array[1]="programm is down"

What I want:

array[0]="Programm is \e[0;32mRunning\e[0m"
array[1]="programm is \e[0;31mDown\e[0m"

This will make things a little more colorful(using the echo -e command), atleast I hope so. I tried to replace this with:

array[$i]=${array[$i]//"running"/"\e[0;32mrunning\e[0m"}
array[$i]=${array[$i]//"down"/"\e[0;31mdown\e[0m"}

That didnt work. Error Message:

0403-011 The specified substitution is not valid for this command.

Why does that happen? What do I need to different?

Dave

Posted 2014-05-12T13:00:48.527

Reputation: 1

Works for me, even if it doesn't produce colourful output. What version of bash are you running? – choroba – 2014-05-12T13:22:46.090

Answers

0

it appears you have the case wrong Running vs running, the following works for me

    array[0]="Programm is running"
    array[1]="Programm is down"

            for i in 0 1; do
            array[$i]=${array[$i]//"running"/"\e[0;32mrunning\e[0m"}
            array[$i]=${array[$i]//"down"/"\e[0;31mdown\e[0m"}
    done
    echo ${array[0]}
    echo ${array[1]}

produces

    Programm is \e[0;32mrunning\e[0m
    Programm is \e[0;31mdown\e[0m

with echo -e instead of just echo, it produces the correct colored text, however, this would be easier to just fix by hand than re-fix it every time it is run.

Mobius

Posted 2014-05-12T13:00:48.527

Reputation: 238

thanks for your Input! I thought of that myself - but as im doing my apprenticeship (learning to be something / im not a native speaker) to be an IT guy I'm not allowed to alter that program that originally produces the output. – Dave – 2014-05-13T05:42:50.943

oh, got it. Good luck! – Mobius – 2014-05-13T16:38:39.253