Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: echo something to right end of window (right aligned)

Tags:

bash

I am looking for producing success/fail messages which are right aligned in bash. An example would be what apache2 produces on executing: sudo /etc/init.d/apache2 reload etc.

In above example, apache2 produces very nice and concise [OK] or [fail] message which are right aligned.

Also, would love to know how to get the text red, in case, we are to produce a [fail] message.

like image 373
Stoic Avatar asked Mar 31 '11 20:03

Stoic


Video Answer


1 Answers

#!/bin/bash

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)

col=80 # change this to whatever column you want the output to start at

if <some condition here>; then
  printf '%s%*s%s' "$GREEN" $col "[OK]" "$NORMAL"
else
  printf '%s%*s%s' "$RED" $col "[FAIL]" "$NORMAL"
fi
like image 166
SiegeX Avatar answered Sep 26 '22 00:09

SiegeX