4

I'd like to know how the shell could display the last column (OK or FAIL) as shown in this image

enter image description here

(from this question Reboot a Server from Command Line?.)

Could anyone help me work out how this is done?

3
  • I know for a fact there has been similar question to yours because i answered it, but i can't find it. Once i find it, I'll provide the link Nov 8, 2016 at 4:50
  • Did you just want to see those startup and shutdown screens then?
    – Terrance
    Nov 8, 2016 at 5:14
  • OK, the related question wasn't exactly a duplicate. I've posted an answer to address your question, but also provided link to the other question I've mentioned. Let me know if it clarifies some things Nov 8, 2016 at 5:44

2 Answers 2

8

What are those ?

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.

How can I do this by myself ?

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

7

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'
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .