#!/bin/sh -ef cmdcache() { if [ $# -lt 2 ]; then echo "cmdcache: not enough arguments" >&2 pod2usage --exit=2 cmdcache || [ $? = 2 ] return 2 fi local cmd="$1"; shift local cdir="$HOME/.cmdcache/${cmd##*/}" [ -d "$cdir" ] || mkdir -p -m700 "$cdir" || return 1 [ -n "${RANDOM##*0}" ] || find "$cdir" -type f -mtime +33 -atime +33 -delete local file; for file; do :; done if [ ! -e "$file" ]; then echo "cmdcache: $file: no such file or directory" >&2 return 1 fi local cache="$*"; cache="$cdir/${cache//[-.\/ ]/_}" if [ -f "$cache" ] && ! [ "$file" -nt "$cache" -o "$cache" -nt "$file" ]; then cat "$cache" return fi local rc=0 local mtime0; mtime0="$(stat --format $'%Y\n' "$file")" || return "$cmd" "$@" >"$cache" && touch -r "$file" "$cache" || rc=$? cat "$cache" local mtime1; mtime1="$(stat --format $'%Y\n' "$file")" || rc=$? if [ $rc = 0 -a "$mtime0" != "$mtime1" ]; then echo "cmdcache: $file has changed" >&2 rc=1 fi [ $rc = 0 ] || rm -f "$cache" return $rc } if [ -z "$*" -a "${0##*/}" != cmdcache ]; then : okay, maybe I am sourced elif [ "x$*" = x-h -o "x$*" = x--help ]; then pod2usage --exit=0 "$0" else cmdcache "$@" fi : <<'__EOF__' =head1 NAME cmdcache - simple command cache based on timestamps =head1 SYNOPSIS B I [I...] I B B '' =head1 AUTHOR Written by Alexey Tourbin . =head1 COPYING Copyright (c) 2005 Alexey Tourbin, ALT Linux Team. This is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. =cut __EOF__