I'd like to know how the shell could display the last column (OK or FAIL) as shown in this image
(from this question Reboot a Server from Command Line?.)
Could anyone help me work out how this is done?
I'd like to know how the shell could display the last column (OK or FAIL) as shown in this image
(from this question Reboot a Server from Command Line?.)
Could anyone help me work out how this is done?
Those fancy OK/FAIL messages that you see are actually boot messages that are output to TTY1 during the boot process. Typically, while the boot process is ongoing you can see those messages instead of the pretty boot logo by pressing the Esc key. Same thing can be done during shutdown process. The screen is typically supposed to be cleared once the boot process is complete, but that doesn't always happen ( for variety of reasons, one of which could be that the messages appear at one speed or baud rate and the TTY1 is then set to be at different baud rate)
To answer your question, that technically is not a shell, but a separate program, namely the systemd
init system that puts out those messages ( in pre-15.04 that would be upstart
init system ). If you're just starting to learn Linux, don't worry about systemd
, but this is a very good topic to know because systemd
is what supposed to start all the important services on the system.
Technical aspects of writing these types of messages are simple: you can use "\r" control character and special escape sequences to make output of certain color. These control characters can be used in any programming language, whether it is C or shell script. In fact, I've answered a related question before, and have written a simple script to demonstrate how one can print status message. As for color, see The Linux Documentation Project's article about colorizing the scripts
The program displaying that output is not the shell. It is systemd
, which is written in C.
What the program probably does is determine the width of the screen/window (technically "number of columns", i.e. spaces available for characters), and then right-align the OK/fail column accordingly.
To do the same thing in Bash, you could get columns from $COLUMNS
, then use printf
to print the output with right-alignment. For example:
print_status(){
local message="$1"
local status="$2"
# Get the number of columns, but subtact 8 to leave space for the status.
local columns=$((COLUMNS-8))
# Print left-aligned message and right-aligned status.
printf "%-*s [%s] \n" "$columns" "$message" "$status"
}
# Print a status.
print_status "Doing something..." " OK "
# Print another status.
print_status "Doing something else..." "fail"
For colorization, you could do this:
print_status "Doing something else..." $'\e[31mfail\e[m'