LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   *BSD (https://www.linuxquestions.org/questions/%2Absd-17/)
-   -   bash color that works for any shell (https://www.linuxquestions.org/questions/%2Absd-17/bash-color-that-works-for-any-shell-4175446744/)

Hoxygen232 01-22-2013 08:08 AM

bash color that works for any shell
 
Hi,

I have created a .c sorce file that call with "system();" a bash script .sh
In this script I have done this

Code:

 
white='\E[37;40m'
Reset="tput sgr0"   
  cecho ()                   
  {
  msg_default="No message"
  messaggio=${1:-$msg_default}
  colore=${2:-$white}       
    printf "$color $message"
    $Reset                     
    return
  }

doing this:
Code:

cecho "String" $cyan
and it works (it shows colors) in geany terminal emulator and also in normal gnome terminal and kde terminal (konsole).
The problem is that it doesn't work while running .c file.
I use Codeblocks to compile and run .c files and it can use "konsole", "gnome terminal" or "xterm" as terminal but it doesn't work with any of them. It calls myscript.sh and shows this:
Code:

\E[37;40m String
without colors.

How can I solve it to make colors work?

Thanks

shivaa 01-22-2013 08:39 AM

Check, you've mentioned colore while declaring it, but color while using it, in your script.
Code:

white='\E[37;40m'
Reset="tput sgr0"   
  cecho ()                   
  {
  msg_default="No message"
  messaggio=${1:-$msg_default}
  color=${2:-$white}      # Changed colore to color       
    printf "$color $message"
    $Reset                     
    return
  }

Or you can try:-

Code:

# white='\E[37;40m'
Reset="tput sgr0"   
  cecho ()                   
  {
  msg_default="No message"
  messaggio=${1:-$msg_default}
  color=${2:-$(echo -e '\E[37;40m')}       
    printf "$color $message"
    $Reset                     
    return
  }


Hoxygen232 01-22-2013 08:46 AM

Quote:

Originally Posted by shivaa (Post 4875425)
Check, you've mentioned colore while declaring it, but color while using it, in your script.
Code:

white='\E[37;40m'
Reset="tput sgr0"   
  cecho ()                   
  {
  msg_default="No message"
  messaggio=${1:-$msg_default}
  color=${2:-$white}      # Changed colore to color       
    printf "$color $message"
    $Reset                     
    return
  }

Or you can try:-

Code:

# white='\E[37;40m'
Reset="tput sgr0"   
  cecho ()                   
  {
  msg_default="No message"
  messaggio=${1:-$msg_default}
  color=${2:-$(echo -e '\E[37;40m')}       
    printf "$color $message"
    $Reset                     
    return
  }


sorry I have just copied wrong in here, yes I already had color=${2:-$white} and it works only in normal terminal but it does not in .c file, that's the problem.
Note: I have written "white='\E[37;40m'" but of course it's the same behaviour for other colors

shivaa 01-22-2013 08:55 AM

What type of script it is - bash? I have doubt about how you defined variables.

Hoxygen232 01-22-2013 09:09 AM

Quote:

Originally Posted by shivaa (Post 4875438)
What type of script it is - bash? I have doubt about how you defined variables.

it's bash, and it works in all terminals (it display different colors), but when running .c file and so it runs the script it doesn't display colors in any terminal

TobiSGD 01-22-2013 09:32 AM

How do you call the script in your .c file? May it be possible that you run the script with sh instead of Bash? May be adding a proper shebang at the beginning of the script helps.

Hoxygen232 01-22-2013 10:12 AM

I used: system("./script1.sh");

I'm so forgetful, you are right, infact it works in this way: system("bash script1.sh");
thanks

theNbomr 01-22-2013 10:15 AM

Color rendering is a function of the terminal type, not the shell. Various terminal programs such as xterm and konsole have different emulations, and so respond differently to the embedded escape sequences you're using. The easiest way to build in terminal handling like you want is to use the ncurses library, which knows all about the various terminal types and how to control them.

--- rod.

David the H. 01-22-2013 12:31 PM

Actually, tput itself should be terminal aware. That's why it's often recommended over raw escape sequences.

Associative arrays are also handy in cases like this.

Code:

declare -A color=(
        [black]=$( tput setaf 0 )
        [red]=$( tput setaf 1 )
        [green]=$( tput setaf 2 )
        [yellow]=$( tput setaf 3 )
        [blue]=$( tput setaf 4 )
        [magenta]=$( tput setaf 5 )
        [cyan]=$( tput setaf 6 )
        [white]=$( tput setaf 7 )
        [reset]=$( tput sgr0 )
)

cecho(){
        echo -n "${color[${2:-white}]}"
        echo "$1"
        echo -n "${color[reset]}"
}



All times are GMT -5. The time now is 07:08 PM.