Unix / linux programs

Weatherby,Gerard gweatherby at uchc.edu
Sat Jan 28 14:40:04 EST 2023


The Unix convention is 0 means everything went well, and non-zero means something else happened. Here’s a contrived example of a bash wrapper around GNU tar. By contrived I mean it works but I would not use it in practice … I’d just use tar directly or use the Python tarfile module if I wanted finer grain control of the process.

#!/bin/bash
if [ $# -lt 2 ]; then
                echo "Usage: $0 [tar] [directory to compare]"
fi
tar --diff -f $1 $2 2>/dev/null
r=$?
if [ $r -eq 0 ]; then
                echo $1 has $2 in it, unmodified
elif [ $r -eq 1 ]; then
                echo $1 does not have $2 in it unmodified
elif [ $r -eq 2 ]; then
                # maybe do more tests here?
                echo There is a problem with $1 or with $2
else
                echo "Other error $r"
fi

Incidentally, I found the man page installed on Ubuntu20.04 for tar is out of date. tar returns 64 if doesn’t like the command line arguments. Nothing works until it is tested.



More information about the Python-list mailing list