0

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 ?

2
  • 2
    There are a number of methods here tput setaf color table? How to determine color codes?. I would suggest the answer using tput rather than the ANSI escape codes. (will work with a wider selection of terminals) You can also do a general search "Linux color output with tput" or "using ANSI escape for color output" and you will find a lot. Commented Jan 24, 2020 at 9:28
  • I did not find any solution, ANSI or tput, it the same. In fact, I am now able to parse a file but I would to use variable in it and I cant achieve it Commented Feb 14, 2020 at 9:57

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I also developed an other solution using PHP by creating a kind of markup language and I needed advanced array features that is really hard to do with bash. So now banner is in same file and I am specifying style using #RED# and resetting with #/# with all style in an INI file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.