Notities

Notities

ing. M.A.C.M. (Martijn) van den Boom

Computer Infrastructure Engineer
ingenieur, docent en mentor

Social Media Links:

ing. M.A.C.M. (Martijn) van den Boom

1-Minute Read

See below an example script to display a SUCCESS message in green or a FAILED message in red, right-aligned at the end of the line.

#!/bin/bash

# Set the message
success_message="Your command was successful"
failed_message="Your command failed"

# Set the "OK" string in green
ok="$(tput setaf 2)[SUCCES]$(tput sgr0)"
failed="$(tput setaf 1)[FAILED]$(tput sgr0)"

# Get the width of the terminal
term_width=$(tput cols)+9

# Calculate the number of dots needed
success_dots_count=$((term_width - ${#success_message} - ${#ok}))
failed_dots_count=$((term_width - ${#failed_message} - ${#failed}))

# Create a string of dots for success and failed messages
success_dots=$(printf "%0.s." $(seq 1 $success_dots_count))
failed_dots=$(printf "%0.s." $(seq 1 $failed_dots_count))

# Print the success message with dots and "OK" at the end of the line
printf "%s%s%s\n" "$success_message " "$success_dots" " $ok"

# Print the failed message with dots and "FAILED" at the end of the line
printf "%s%s%s\n" "$failed_message " "$failed_dots" " $failed"

Recent Posts:

Categories

About

Sponsor me link:
ko-fi.com/martijnvandenboom
paypal.me/ingmacmvandenboom
buymeacoffee.com/ingvdboom