#! /bin/sh # Help function # function help() { cat <<-EOF Usage: `basename $0` [-h] SEDSCRIPT FILE Runs sed on FILE using SEDSCRIPT as a sed script. -h, --help Display this help text EOF } # The IFS business is to prevent the script from choking on filenames with spaces. old_ifs=${IFS} IFS=$'\n' # Check arguments and issue a help statement, if wrong # if [ $# -eq 0 ]; then help exit 1 elif [ $1 = "-h" -o $1 = "--help" ]; then help exit 0 fi SEDSCR=$1 shift for x do echo "editing $x" if test "$x" = sedscr; then echo "not editing sedscript!" elif test -s $x; then y=`basename $x`_$RANDOM sed -f $SEDSCR $x > /tmp/$y$$ echo "Output written to /tmp/$y$$" if test -s /tmp/$y$$ then if cmp -s $x /tmp/$y$$ then echo "file not changed" else mv $x $x.bak # save original, just in case cp /tmp/$y$$ $x fi echo "done" else echo "Sed produced an empty file" echo " - check your sedscript." fi rm -f /tmp/$y$$ else echo "original file is empty." fi done echo "all done" IFS=${old_ifs}