Loenix
Loenix

Reputation: 1090

How yo output file using bash coloring?

I would like to print a MOTD using styled text when an user connects to the server (ubuntu 18.04) using SSH.

The only way I found is to print the file by myself because Ubuntu originally only cats the motd file.

So now, I have a colord motd file but I don't found any way to print out the contents with the style.

I found this command on stackoverflow:

cat /home/user/conf/bash/motd | sed  's/$/\\n/' | sed 's/ /\\a /g'

But this is not working propertly with large ASCII text. enter image description here

here is the current test motd file

 ____      ____  ________  _____       ______    ___   ____    ____  ________
|_  _|    |_  _||_   __  ||_   _|    .' ___  | .'   `.|_   \  /   _||_   __  |
  \ \  /\  / /    | |_ \_|  | |     / .'   \_|/  .-.  \ |   \/   |    | |_ \_|
   \ \/  \/ /     |  _| _   | |   _ | |       | |   | | | |\  /| |    |  _| _
    \  /\  /     _| |__/ | _| |__/ |\ `.___.'\\  `-'  /_| |_\/_| |_  _| |__/ |
     \/  \/     |________||________| `.____ .' `.___.'|_____||_____||________|


Welcome to my Server !

Aliases:
\e[4ml\e[0m => ls -lA
\e[1;93mll\e[0m => ls -l

Have you any solution to do it please ?

Upvotes: 0

Views: 765

Answers (2)

Loenix
Loenix

Reputation: 1090

I would use variable in my file but It's seems that parsing variable implies parsing other things so it's not compatible with the banner that is ASCII only. So I exported the banner to another file and now, I am able to print color and the banner.

Here is the SH sample

RED="\e[31m"
BANNER=$(<banner.txt)
printf "$(eval "echo \"$(<myfile.txt)\"")"

banner.txt

 ____      ____  ________  _____       ______    ___   ____    ____  ________
|_  _|    |_  _||_   __  ||_   _|    .' ___  | .'   `.|_   \  /   _||_   __  |
  \ \  /\  / /    | |_ \_|  | |     / .'   \_|/  .-.  \ |   \/   |    | |_ \_|
   \ \/  \/ /     |  _| _   | |   _ | |       | |   | | | |\  /| |    |  _| _
    \  /\  /     _| |__/ | _| |__/ |\ `.___.'\\  `-'  /_| |_\/_| |_  _| |__/ |
     \/  \/     |________||________| `.____ .' `.___.'|_____||_____||________|

myfile.txt

$BANNER

Welcome to my Server !

TEXT FIRST
${RED}TEXT IS RED
\e[34mTEXT IS BLUE\e[0m
TEXT THIRD

I am now looking for a prettier solution.

Upvotes: 0

Ivan
Ivan

Reputation: 7277

Hi i'm using my color table when i need colors in scripts. Here it is.

#!/bin/bash
#--------------------------------------------------------------------+
#Color picker, usage: printf ${BLD}${CUR}${RED}${BBLU}"Hello!)"${DEF}|
#-------------------------+--------------------------------+---------+
#       Text color        |       Background color         |         |
#-----------+-------------+--------------+-----------------+         |
# Base color|Lighter shade|  Base color  | Lighter shade   |         |
#-----------+-------------+--------------+-----------------+         |
BLK='\e[30m'; blk='\e[90m'; BBLK='\e[40m'; bblk='\e[100m' #| Black   |
RED='\e[31m'; red='\e[91m'; BRED='\e[41m'; bred='\e[101m' #| Red     |
GRN='\e[32m'; grn='\e[92m'; BGRN='\e[42m'; bgrn='\e[102m' #| Green   |
YLW='\e[33m'; ylw='\e[93m'; BYLW='\e[43m'; bylw='\e[103m' #| Yellow  |
BLU='\e[34m'; blu='\e[94m'; BBLU='\e[44m'; bblu='\e[104m' #| Blue    |
MGN='\e[35m'; mgn='\e[95m'; BMGN='\e[45m'; bmgn='\e[105m' #| Magenta |
CYN='\e[36m'; cyn='\e[96m'; BCYN='\e[46m'; bcyn='\e[106m' #| Cyan    |
WHT='\e[37m'; wht='\e[97m'; BWHT='\e[47m'; bwht='\e[107m' #| White   |
#----------------------------------------------------------+---------+
# Effects                                                            |
#--------------------------------------------------------------------+
DEF='\e[0m'   #Default color and effects                             |
BLD='\e[1m'   #Bold\brighter                                         |
DIM='\e[2m'   #Dim\darker                                            |
CUR='\e[3m'   #Italic font                                           |
UND='\e[4m'   #Underline                                             |
INV='\e[7m'   #Inverted                                              |
COF='\e[?25l' #Cursor Off                                            |
CON='\e[?25h' #Cursor On                                             |
#--------------------------------------------------------------------+
# Text positioning, usage: XY 10 10 "Hello World!"                   |
XY   () { printf "\e[${2};${1}H${3}";   } #                          |
#--------------------------------------------------------------------+
# Print line, usage: line - 10 | line -= 20 | line "Hello World!" 20 |
line () { printf -v LINE "%$2s"; printf -- "${LINE// /$1}"; } #      |
# Create sequence like {0..X}                                        |
cnt () { printf -v _N %$1s; _N=(${_N// / 1}); printf "${!_N[*]}"; } #|
#--------------------------------------------------------------------+

welcome=(''
    $RED" ____      ____  ________  _____       ______    ___   ____    ____  ________      \n"$DEF
    $RED"|_  _|    |_  _||_   __  ||_   _|    .' ___  | .'   \`.|_   \  /   _||_   __  |    \n"$DEF
    $GRN"  \ \  /\  / /    | |_ \_|  | |     / .'   \_|/  .-.  \ |   \/   |    | |_ \_|     \n"$DEF
    $GRN"   \ \/  \/ /     |  _| _   | |   _ | |       | |   | | | |\  /| |    |  _| _      \n"$DEF
    $BLU"    \  /\  /     _| |__/ | _| |__/ |\ \`.___.'\\\\\  \`-'  /_| |_\/_| |_  _| |__/ |\n"$DEF
    $BLU"     \/  \/     |________||________| \`.____ .' \`.___.'|_____||_____||________|   \n"$DEF
)

printf "${welcome[*]}"

enter image description here

Upvotes: 4

Related Questions