Sponsored Content
Full Discussion: Using color in scripts
Operating Systems Linux Ubuntu Using color in scripts Post 303033728 by drew77 on Wednesday 10th of April 2019 05:53:25 PM
Old 04-10-2019
I am using mate terminal.

I found another way which is easier to use and tput is part of the ncurses package and is supplied with most Linux distributions.

Code:
FILE="/usr/share/sounds/My_Sounds/Short_doorbell.wav"

if [ -f "$FILE" ];
then
     tput setaf 2; echo "File $FILE does exist."
else
     tput setaf 1; echo "File $FILE does NOT exist."
     
fi
tput sgr0    # Reset text format to the terminal's default


#Num  Colour    #define         R G B

#0    black     COLOR_BLACK     0,0,0
#1    red       COLOR_RED       1,0,0
#2    green     COLOR_GREEN     0,1,0
#3    yellow    COLOR_YELLOW    1,1,0
#4    blue      COLOR_BLUE      0,0,1
#5    magenta   COLOR_MAGENTA   1,0,1
#6    cyan      COLOR_CYAN      0,1,1
#7    white     COLOR_WHITE     1,1,1

These 3 Users Gave Thanks to drew77 For This Post:
 

2 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to change the background color in the init 3 mode(not line color)

Hello, I am using RHEL 6.1 on VMware I am searching for a way to change background color (not line by line color wich one can using tput command) basically changing the color of the whole screen to white instead of the default black and changing font color to black and alos would like to... (2 Replies)
Discussion started by: Dexobox
2 Replies

2. UNIX for Beginners Questions & Answers

Text color in Linux scripts via putty

hi Folks, Can anyone help with changing the color of the words in a linux shell script? I get how to change default background etc in putty, but for some reason the text in the script has different colors for different parts of the cript. Is there a way to have one color in a linux shell... (5 Replies)
Discussion started by: jonnyd
5 Replies
CURSES_COLOR(3) 					   BSD Library Functions Manual 					   CURSES_COLOR(3)

NAME
curses_color, has_colors, can_change_color, start_color, init_pair, pair_content, COLOR_PAIR, PAIR_NUMBER, init_color, color_content, no_color_attributes -- curses color manipulation routines LIBRARY
Curses Library (libcurses, -lcurses) SYNOPSIS
#include <curses.h> bool has_colors(void); bool can_change_color(void); int start_color(void); int init_pair(short pair, short fore, short back); int pair_content(short pair, short *fore, short *back); int COLOR_PAIR(int n); int PAIR_NUMBER(int val); int init_color(short color, short red, short green, short blue); int color_content(short color, short *red, short *green, short *blue); attr_t no_color_attributes(void); extern int COLOR_PAIRS; extern int COLORS; DESCRIPTION
These functions manipulate color on terminals that support color attributes. The function has_colors() indicates whether a terminal is capable of displaying color attributes. It returns TRUE if the terminal is capable of displaying color attributes and FALSE otherwise. The function can_change_color() indicates whether a terminal is capable of redefining colors. It returns TRUE if colors can be redefined and FALSE if they can not. The function start_color() initializes the curses color support on a terminal. It must be called before any color manipulation functions are called on that terminal. The function initializes the eight basic colors (black, red, green, yellow, blue, magenta, cyan and white) that are specified using the color macros (such as COLOR_BLACK) defined in <curses.h>. start_color() also initializes the global external variables COLORS and COLOR_PAIRS. COLORS defines the number of colors that the terminal supports and COLOR_PAIRS defines the number of color-pairs that the terminal supports. These color-pairs are initialized to white foreground on black background. start_color() sets the colors on the terminal to the curses defaults of white foreground on black background unless the functions assume_default_colors() or use_default_colors() have been called previously. The function init_pair(pair, fore, back) sets foreground color fore and background color back for color-pair number pair. The valid range for the color-pair pair is from 1 to COLOR_PAIRS - 1 and the valid range for the colors is any number less than COLORS. Specifying a nega- tive number will set that color to the default foreground or background color. The 8 initial colors are defined as: COLOR_BLACK COLOR_RED COLOR_GREEN COLOR_YELLOW COLOR_BLUE COLOR_MAGENTA COLOR_CYAN COLOR_WHITE Color-pair 0 is used as the default color pair, so changing this will have no effect. Use the function assume_default_colors() to change the default colors. The function pair_content(pair, *fore, *back) stores the foreground and background color numbers of color-pair pair in the variables fore and back, respectively. The macro COLOR_PAIR(n) gives the attribute value of color-pair number n. This is the value that is used to set the attribute of a character to this color-pair. For example, attrset(COLOR_PAIR(2)) will display characters using color-pair 2. The macro PAIR_NUMBER(val) gives the color-pair number associated with the attribute value val. The function init_color(color, red, green, blue) sets the red, green and blue intensity components of color color to the values red, green and blue, respectively. The minimum intensity value is 0 and the maximum intensity value is 1000. The function color_content(color, *red, *green, *blue) stores the red, green and blue intensity components of color color in the variables red, green, and blue, respectively. The function no_color_attributes() returns those attributes that a terminal is unable to combine with color. RETURN VALUES
The functions start_color(), init_pair(), pair_content(), init_color() and color_content() return OK on success and ERR on failure. SEE ALSO
curses_attributes(3), curses_background(3), curses_default_colors(3) STANDARDS
The NetBSD Curses library complies with the X/Open Curses specification, part of the Single Unix Specification. The function no_color_attributes() and the use of negative color numbers are extensions to the X/Open Curses specification. HISTORY
These functions first appeared in NetBSD 1.5. BSD
July 20, 2009 BSD
All times are GMT -4. The time now is 01:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy