Articles in Series "Shells and related Scripting Languages"

 GNU find hat keine Option „-older“ …

… aber eine Funktion „-newer“. Die Funktion „-newer“ ist praktisch, um absolute Zeitangaben zum Aufspüren von Dateien zu verwenden. So würde

touch -d "2010/1/1" /tmp/test.newer
find . -type f -a -name 'core.*' -a -newer /tmp/test.newer

alle regulären Dateien mit „core.“ als Anfang des Dateinamens finden, die neuer sind als der 1. Januar 2010, 00:00 Uhr.

Jedoch gibt es (komischerweise) keine Option „-older“, d.h. man kann scheinbar nicht im gleichen Aufruf auch rückwärts eingrenzen, d.h. Dateien aus einem bestimmten absoluten Zeit-Intervall suchen.

Durch kurze Überlegung wird jedoch klar, dass man keine Option „-older“ braucht, weil es ja einen Negations-Operator gibt, der aus „-newer“ („neuer als“) „\! -newer“ (also „nicht neuer als“) macht. Also:

touch -d "2010/1/1" /tmp/test.newer
touch -d "2010/12/31" /tmp/test.older
find . -type f -a -name 'core.*' -a -newer /tmp/test.newer -a \! -newer /tmp/test.older

Und so findet man alle als „core.*“ benannten regulären Dateien aus dem Jahr 2010, wobei genau genommen der Zeitpunkt (timestamp) 1. Januar 2010, 0 Uhr, 0 Minuten und 0 Sekunden fehlt. Will man 100% Abdeckung, muss man die Referenz-Datei für „-newer“ wie folgt anlegen:

touch -d "2009/12/31 23:59:59" /tmp/test.newer

oder

touch -d "2010/01/01 - 1 second" /tmp/test.newer

Done.

Bourne to Bourne Again Shell Forward Compatibility

Introduction

In this article i try to find out, if Bourne Shell scripts are runnable in Bourne Again Shell without modification. If not, i advice on how to modify the code so that it runs on both Shells.

An interpreter for some variant of Bourne Shell is available as an executable /bin/sh on most Linux and UNIX systems. Writing Bourne Shell script has the possible advantage that such a script can work on all these systems with no changes to the code; be it Linux, AIX, FreeBSD, initramfs or a string passed to a C library call system(3), expressions such as

  • echo hello | wc
  • my_pid=$$

or

  • exit 1

would always work.

Recently, i used GNU Bourne Again Shell for scripting, and i wondered if all of my longstandingly Bourne Shell established practices were seamlessly portable. So i did some research, and in this article i highlight some code constructs i found that work in Bourne Shell and do not work as expected in Bourne Again Shell. Code currently interpreted by /bin/sh and containing such constructs must be reviewed if will be interpreted by /bin/bash.

As it stands, i will focus on reserved words and built-ins, brace expansion and pipeline subprocesses.

Print XDG Desktop Definition for Application

Update Nov 3 2024:

For an application given by „application name“ or „executable name“, output the corresponding .desktop file, if any:

#!/bin/sh
IFS=":"
xdg_data_dirs=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
search=$1

for i in $xdg_data_dirs ; do
  a="$i/applications"

  [ -d $a ] && for d in "$a"/*.desktop ; do
    grep -q -e "^Name=.*$search" -e "^Exec=.*$search" "$d" && {
      echo "# $d:"
      grep -Ev '^(Comment|GenericName|Keywords|Name\[)' "$d"
    }
  done
done

To try this code out, save it to /usr/local/bin/xdg-desktop-search, make it executable and test it, for example, as follows:

xdg-desktop-search gnome-terminal

Find Files by Size given in Bytes

Some examples:

Find files in current directory that have a size of 400 bytes or more:

sfind -min 400

Find files in /etc that have a size of 50 kilobytes (1 kilobyte = 1024 bytes) or more:

sfind -dir /etc -min 50k

Find files in /var with size between 100 and 500 megabytes, suppress warnings, print sizes in human-readable format:

sfind -dir /var -min 100m -max 500m -quiet -human

Note: Yes, i know, Oracle distributes a tool that is also called „sfind“, but anyway …

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 »

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). 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 »

Maintaining Multi-Line „stat“ Formats using Bourne Again Shell

The stat command from GNU core utilities features not only a --format FORMAT option but also a --printf FORMAT one, the difference being that the latter allows for backslash escapes such as \n.

This allows for custom per-file report formats containing newlines, for example:

stat --printf 'Name: %n\nSize: %s Bytes\n' /etc/passwd

If the format string becomes more complex, the command line soon becomes unwieldy, such as:

stat --printf 'Name: %n\nOwner ID: %u\nSize: %s Bytes\nLast accessed: %x\n' /etc/passwd

Lesen Sie mehr »

Print all indented Lines following a non-indented Line

Some configuration and output text formats contain sections like the following:

foo:
   value1
   value2
bar:
   value 3

In this article, two scripts are presented that print all consecutive indented lines that follow a non-indented line that matches a search pattern given by a regular expression.

This means, given the single argument foo and the standard input above, the scripts should

  • determine the line that matches foo and
  • print the following two lines, but no other lines.

Also, the indentation of the printed lines should be removed.

Lesen Sie mehr »