colors in bash scripts

Colours (Photo credit: CherrySoda!)

I have been trying to understand the color codes for bash for a pretty long time, but somehow never got time to understand this clearly. So this time around when I was writing a script to analyze some logs. I thought I will give it a go and finally understood some part of this.

So, first we will start with this script. This is taken from here.

#!/bin/bash -
#          FILE:
#         USAGE:  ./
#       OPTIONS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka),
#       COMPANY:
#       CREATED: 09/04/12 14:29:47 IST
# Last modified: Sun Apr 15, 2012  06:29PM
#      REVISION:  ---

#   This file echoes a bunch of color codes to the
#   terminal to demonstrate what's available.  Each
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a
#   test use of that color on all nine background
#   colors (default + 8 escapes).

T='gYw'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    #do echo -en "$EINS \033[$FG\033[$BG  \$T  \033[0m";
    do echo -en " \033[$FG\033[$BG  $T  \033[0m";

This script will give you all the colors that are possible in the bash scripting. And now let’s understand color’s a little more.

There are basically two ways you can use colors and I will illustrate both with echo command.

Before we get into all these, we will see hot to reset the colors:

echo -e "\033[00m"

The first one is the one used in the above script, where in you can use the codes multiple times to get the desired effect. For example, see the screenshot below:

colors in bash
colors in bash

And the other way to achieve the same is to use all the color codes with one escape sequence :

colors in bash
colors in bash

Important to note here is that the “m” is missing.

All the codes that you want can be used separated by “;” and end with “m”. I think I have explained the things.

Enhanced by Zemanta

systemadm – simpler way to manage the service files for systemd

Once the systemd unit and service files have come into being, understanding of the system services have changed a little bit and it might require some time to understand the cli tools for managing these services and units. That being the case, systemadm comes to rescue. This is a simple tool which :

Graphical front-end for systemd.

For installation :

sudo yum install systemd-gtk

And here’s a screenshot::



Enhanced by Zemanta

Convert string to hex.

If you want to use snoop or tcpdump with advanced search in the packet, then you would need to convert the string to hex string. For this, either you can use a web search and find some web application to do that or you can use a simple C program like this 🙂

* =====================================================================================
*       Filename:  ascii.c
*    Description:  ascii to dec
*        Version:  1.0
*        Ceated:  03/02/2012 12:08:49 PM
*       Revision:  none
*       Compiler:  gcc
*         Author:  Amit Agarwal (aka),
*  Last modified: Thu Mar 08, 2012  08:47PM
* =====================================================================================
void main (int argc, char **argv){
    int i=0;
    char a[200]="",b[200]="";
    printf("String - %s, Length - %d\n",argv[1],strlen(argv[1]));
    /*printf("%x\n 0x",argv[1]);*/
    while (i < strlen(argv[1])){
    printf ("\n");
    printf ("String : %s\n",argv[1]);
    printf ("Hex    : 0x%s\n", a);
    printf ("Dec    : %s\n", b);

Enhanced by Zemanta