#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)print-header 3.1.0 05.06.2023 (c)2023 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Reads a file and pretty prints a frame around the content. #@ #@Usage: print-header [options] filename #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -c, --color : Green colorcode e.g.: "\033[0;32m". #@ Filename: A file with some content. #@ #@Examples: #@ echo "Hello world" > file.tmp #@ Prints "Hello world" to file "file.tmp" #@ print-header file.tmp #@ /-------------\ #@ Hello world #@ \-------------/ # # Exit codes: # 01: Unknown or wrong parameter. # 99: User interrupt. # # See also: # **print-table**(1), **install-scripts**(1) # # Update history: # # V 2.0.0 11.06.2019 New version # V 3.0.0 26.04.2020 Multi OS compatible # V 3.0.1 11.06.2020 Using library # V 3.0.2 20.04.2021 Rewritten using bash instead of tcsh # V 3.1.0 05.06.2023 New copyright # script=${0} # Name of this script progstr=`basename "$script"` # Basename of the script progdir=`dirname "$script"` # Dirname of the script # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Set PATH to something useful if [ "$WORKPATH" != "" ]; then PATH="$WORKPATH" fi # Preset normal='\033[0m' filename="" color="" len=0 longestline=0 # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -v | --version) shift showversion=1 ;; -h | --help) shift showhelp=1 ;; -c | --color) shift if [ "$1" != "" ]; then color=${1} shift else errstr="Please specify a color code after parameter '$pname'." fi ;; *) paramck=`echo "$1" | grep '^-'` # Types don't begin with '-' if [ "$filename" = "" -a "$paramck" = "" ]; then filename=${1} else errstr="Unknown parameter: '$1'." fi shift esac done # Check if filename is empty and file exists if [ "$filename" = "" ]; then errstr="Please specify a file with values." else if [ ! -f "$filename" ]; then errstr="File '$filename' does not exists." fi fi # Display help or error message DisplayHelp function PrintMarkerString() { local len=${1} local stype=`echo "$2" | tolower` local form="" local head="" local tail="" form=`printf "%-${len}s" | tr ' ' '-'` # Right pad string with blanks and replace blanks with dash if [ "$stype" = "begin" ]; then if [ "$color" != "" ]; then head="${color}/-" tail="\\" else head="/-" tail="\\" fi else if [ "$color" != "" ]; then head="\\-" tail="/${normal}" else head="\\-" tail="/" fi fi # Print new string printf "${head}${form}${tail}" printf "\n" } ### Main while read line; do len=`echo "$line" | wc -m` # let len-- if [ $len -gt $longestline ]; then longestline=$len fi done < $filename # Create begin string PrintMarkerString "$longestline" "begin" while read line; do line=`echo "$line" | tr ' ' ' '` # Convert tab to blank printf " %s\n" "$line" done < $filename # Create begin string PrintMarkerString "$longestline" "end" # Cleanup and exit Cleanup exit $exitcode