How to resolve the algorithm Terminal control/Coloured text step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Terminal control/Coloured text step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Display a word in various colours on the terminal. The system palette, or colours such as Red, Green, Blue, Magenta, Cyan, and Yellow can be used.

Optionally demonstrate:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Terminal control/Coloured text step by step in the UNIX Shell programming language

Source code in the unix programming language

#!/bin/sh
# Check if the terminal supports colour

# We should know from the TERM evironment variable whether the system
# is comfigured for a colour terminal or not, but we can also check the
# tput utility to check the terminal capability records.

COLORS=8    # Assume initially that the system supports eight colours
case $TERM in
  linux)
    ;;      # We know this is a colour terminal
  rxvt)
    ;;      # We know this is a colour terminal
  *)   
    COLORS=`tput colors 2> /dev/null`    # Get the number of colours from the termcap file
esac
if [ -z $COLORS ] ; then
  COLORS=1    # Watch out for an empty returned value
fi

if [ $COLORS -le 2 ] ; then
  # The terminal is not colour
  echo "HW65000 This application requires a colour terminal" >&2
  exit 252    #ERLHW incompatible hardware
fi

# We know at this point that the terminal is colour

# Coloured text
tput setaf 1    #red
echo "Red"
tput setaf 4    #blue
echo "Blue"
tput setaf 3    # yellow
echo "Yellow"

# Blinking
tput setab 1    # red background
tput setaf 3    # yellow foreground
#tput blink     # enable blinking (but does not work on some terminals)
echo "Flashing text"

tput sgr0    # reset everything before exiting


  

You may also check:How to resolve the algorithm Tic-tac-toe step by step in the Haskell programming language
You may also check:How to resolve the algorithm String case step by step in the Lasso programming language
You may also check:How to resolve the algorithm Window creation step by step in the PowerShell programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the NetRexx programming language