Monday, August 20, 2007

Bash: command list (|| &&)

#!/bin/bash

# delete.sh, not-so-cunning file deletion utility.
# Usage: delete filename

E_BADARGS=65

if [ -z "$1" ]
then
echo "Usage: `basename $0` filename"
exit $E_BADARGS # No arg? Bail out.
else
file=$1 # Set filename.
fi


[ ! -f "$file" ] && echo "File \"$file\" not found. \
Cowardly refusing to delete a nonexistent file."
# AND LIST, to give error message if file not present.
# Note echo message continued on to a second line with an escape.

[ ! -f "$file" ] || (rm -f $file; echo "File \"$file\" deleted.")
# OR LIST, to delete file if present.

# Note logic inversion above.
# AND LIST executes on true, OR LIST on false.

exit 0

Sunday, August 19, 2007

Gnu-make: pattern rule

A pattern rule contains the character `%' (exactly one of them) in the
target; otherwise, it looks exactly like an ordinary rule. The target is
a pattern for matching file names; the `%' matches any nonempty
substring, while other characters match only themselves

Here are some examples of pattern rules actually predefined in make.
First, the rule that compiles `.c' files into `.o' files:

%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

defines a rule that can make any file `x.o' from `x.c'. The command uses
the automatic variables `$@' and `$<' to substitute the names of the
target file and the source file in each case where the rule applies (see
section Automatic Variables).

Blog Archive