Search code examples
bashshell

BASH Alignment/Print Issue: Output Misaligned in TXT File Despite Correct Terminal Display


In my script A, I execute SQL queries and construct a result string and a details string:

        #
        # BUILD OUTPUT
        #
        result="
DATENBANK(EN)
=============
$db_overview


INSTANZ(EN)
===========
$instance_overview


$container_overview_
"

        details=""

Afterwards, I call the following code to write the result to a TXT file:

if [ "$print_txt" = true ]; then printf "%s" "$(txt_item "Database Overview" "$result" "$details")" >> "$TXT_FILENAME"; fi

For proper formatting, the function txt_item is called:

function txt_item() {
    local h="$1"
    local result="$2"
    local details="$3"

    item="



# ######################################
# $h
# ######################################

$result
"

    if [ -n "$details" ]; then
        _details="


# $h.DETAILS

$details
"
        item+=$_details
    fi

    echo "$item" 
}

The problem now is that tables like $db_overview are misaligned in the TXT file and not displayed as they are in the terminal. Here is an example:

printf "%s" "$(txt_item "Database Overview" "$result" "$details")"
# if [ "$print_txt" = true ]; then printf "%s" "$(txt_item "Database Overview" "$result" "$details")" >> "$TXT_FILENAME"; fi

Output (Terminal):





# ######################################
# Database Overview
# ######################################


DATENBANK(EN)
=============

NAME                 OPEN_MODE       LOG_MODE             DATABASE_ROLE   CREATED
-------------------- --------------- -------------------- --------------- ----------
XXX                  READ WRITE      NOARCHIVELOG         PRIMARY         11.10.XXXX


INSTANZ(EN)
===========

IID INAME                HOST_NAME                           STATUS     PAR STATUS
--- -------------------- ----------------------------------- ---------- --- ----------
1   XXX                  XXXXXXXXXXXXXXXX                    OPEN       NO  ACTIVE

Output (TXT-FILE)




# ######################################
# Database Overview
# ######################################


DATENBANK(EN)
=============

NAME             OPEN_MODE       LOG_MODE         DATABASE_ROLE   CREATED
-------------------- --------------- -------------------- --------------- ----------
XXX          READ WRITE      NOARCHIVELOG     PRIMARY     11.10.2022


INSTANZ(EN)
===========

IID INAME        HOST_NAME               STATUS PAR STATUS
--- -------------------- ----------------------------------- ---------- --- ----------
1   XXX          XXXXXXXXXXXXXXX             OPEN   NO  ACTIVE

As you can see, the output is misaligned in the TXT file despite being displayed correctly one line prior in the terminal. How can this issue be resolved?


Solution

  • The output was identical. The issue was caused by the editor being used. See @Barmar.

    Problem closed.