Skip to content

Instantly share code, notes, and snippets.

@rafzei
Last active October 16, 2017 09:48
Show Gist options
  • Save rafzei/50673d0a492bdff63cb469bd09b817f2 to your computer and use it in GitHub Desktop.
Save rafzei/50673d0a492bdff63cb469bd09b817f2 to your computer and use it in GitHub Desktop.
bash (scripting) colors
#!/bin/sh
add_color(){
#colors declaration
NORMAL='\\033[0m'
GREEN='\\033[0;32m'
RED='\\033[0;31m'
YELLOW='\\033[0;33m'
#recognize INFO,ERROR,SUCCESS
local rec_string=$(echo $1 | egrep -o '^[a-Z]{4}')
if [[ $rec_string == "INFO" ]]; then
local output_string=$(echo $1 | sed "s|INFO:|$YELLOW INFO: $NORMAL|g")
elif [[ $rec_string == "ERRO" ]]; then
local output_string=$(echo $1 | sed "s|ERROR:|$RED ERROR: $NORMAL|g")
elif [[ $rec_string == "SUCC" ]]; then
local output_string=$(echo $1 | sed "s|SUCCESS:|$GREEN SUCCESS: $NORMAL|g")
else
echo "Can't recognize string: $rec_string"
fi
echo -e $output_string
}
add_color "INFO: This is yellow output"
add_color "ERROR: This is red output"
add_color "SUCCESS: This is green output"
@rafzei
Copy link
Author

rafzei commented Sep 15, 2017

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment