LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > *BSD
User Name
Password
*BSD This forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.

Notices


Reply
  Search this Thread
Old 01-22-2013, 08:08 AM   #1
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Rep: Reputation: Disabled
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

Last edited by Hoxygen232; 01-22-2013 at 10:22 AM.
 
Old 01-22-2013, 08:39 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
  }

Last edited by shivaa; 01-22-2013 at 08:42 AM.
 
Old 01-22-2013, 08:46 AM   #3
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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

Last edited by Hoxygen232; 01-22-2013 at 08:48 AM.
 
Old 01-22-2013, 08:55 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
What type of script it is - bash? I have doubt about how you defined variables.
 
Old 01-22-2013, 09:09 AM   #5
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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
 
Old 01-22-2013, 09:32 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
1 members found this post helpful.
Old 01-22-2013, 10:12 AM   #7
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
I used: system("./script1.sh");

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

Last edited by Hoxygen232; 01-22-2013 at 10:20 AM.
 
Old 01-22-2013, 10:15 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
2 members found this post helpful.
Old 01-22-2013, 12:31 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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]}"
}
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No color for ls -l after being root on bash shell cola Slackware 15 02-15-2013 06:15 AM
Color in bash shell zivota Linux - Newbie 6 12-06-2008 04:31 PM
Bash color shell L0CTiT3 *BSD 5 10-22-2008 10:59 AM
how do i chage the color of a file or a directory in a bash shell Paxmaster Linux - Software 3 10-17-2004 11:21 PM
how change text (and background) color within the bash shell? Xavius Linux - Newbie 4 03-29-2004 02:21 PM

LinuxQuestions.org > Forums > Other *NIX Forums > *BSD

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration