Make a Bourne Again Shell Script Log its Output to a File

The Bourne Again Shell script presented in this article demonstrates techniques related to capturing and logging output (standard output and standard error stream) of a script into a log file while also delivering it to the regular output destinations (for example the terminal or whatever the caller has chosen to redirect to).

If you are just interested in the Bourne Again Shell code example, click here.

In the following, some questions are addressed:

  • How to „clone“ standard output and standard error stream of a shell script to a single log file?
  • Are the messages from both streams interleaved into a single file in the appropriate order?
  • Can a script perform output cloning for some portions of its procedure while skipping it for others?
  • Are there specific requirements for output that is to be cloned to a file?
  • Are there best practices when a script modifies its own output redirections?

The example provided below has been tested on GNU/Linux using GNU „bash“ version 5 and „tee“ version 8.32 from GNU coreutils. It uses the Bash-only feature of process substitution. An implementation in POSIX Shell (possibly using only POSIX tools) would be more difficult.

Lesen Sie mehr »

Using sed or awk to ensure a specific last Line in a Text

Given a file containing bytes of text with lines separated by the newline character (\n), one of these lines can be said to be „the last line of the file“; it is a sequence of bytes occurring in the file, for which holds:

  • The sequence contains no newline character, and
  • the sequence is followed by at most one newline character and no other bytes.

The task at hand is, using shell utilities, to write a procedure that makes sure that a given file contains a last line that contains a desired sequence of text characters.

Lesen Sie mehr »

Fotos von Palma de Mallorca und Umgebung

I went to CLT23 …

… and i got a new coffee mug. 🙂

Determine IPv4 Addresses of a Libvirt Qemu-KVM Domain

In the VM, on Redhat-likes, make sure that the Qemu guest agent is installed:

dnf install qemu-guest-agent

On Debian-likes, execute:

apt install qemu-guest-agent

For Microsoft Windows guests, there are some tutorials on the web, see for example [1].

On the HV, execute:

virsh qemu-agent-command --domain myvm \
    '{"execute":"guest-network-get-interfaces"}' | \
    jq -r '
        [.return[] | select(.name!="lo") | ."ip-addresses"] | flatten |
        .[] | select(."ip-address-type"=="ipv4")."ip-address"
    '

The output should be one IPv4 address per line.

If you are interested in IPv4 and IPv6 addresses instead, just skip the „select“ filter:

virsh qemu-agent-command --domain myvm \
    '{"execute":"guest-network-get-interfaces"}' | \
    jq -r '
        [.return[] | select(.name!="lo") | ."ip-addresses"] | flatten |
        .[]."ip-address" '

 

 Two Paintings

Did i mention that i like Blender?

The Dark City (Version 3-7)

Cryptospheres (Version 1-1)

 Two Worlds, one Reality

Sometimes, what topside constitutes an amazing engineering feat is turtles all the way down.

 The Internet Security Architecture