#!/bin/bash # owrite v0.1 # Purpose: safely use a file for output redirection and command input # simultaneously # Written and Copyright 2001 by Bill Jonas, bill@billjonas.com # Credit is given to Brian W. Kernighan and Robert Pike for _The UNIX # Programming Environment_. This program is based on an example I remember # from that book. # This program is licensed under the GNU GPL. # Please see http://www.gnu.org/copyleft/gpl.html for details. # Please note: # Because the program is licensed free of charge, there is NO WARRANTY for # the program, to the extent permitted by applicable law. Except when # otherwise stated in writing the copyright holders and/or other parties # provide the program "AS IS" WITHOUT WARRANTY OF ANY KIND, either # expressed or implied including, but not limited to, the implied # warranties of merchantability and fitness for a particular purpose. The # entire risk as to the quality and performance of the program is with you. # Should the program prove defective, you assume the cost of all necessary # servicing, repair or correction. print_help() { echo "Usage: $0 " exit 1 } case "$1" in -h|--help) print_help exit 1 ;; esac if [ -z "$2" ] # Must have at least two args, file and command then print_help exit 1 fi # Try to use the tempfile command if available, # otherwise, fall back to an inferior method. if `type tempfile >/dev/null 2>&1` then TEMPFILE=`tempfile` else TEMPFILE=/tmp/owrite.$$ fi FILE="$1" shift # Clean up if we're interrupted trap "rm $TEMPFILE" HUP INT QUIT ABRT ALRM TERM STOP TSTP PWR eval "$@ $FILE >$TEMPFILE" RETVAL=$? # How did the command exit? if [ $RETVAL -gt 0 ] then # Something bad happened. echo "Operation aborted. Return value from command was $RETVAL" rm $TEMPFILE exit 1 fi # Go for broke. We don't want to be interrupted, if at # all possible, and only a SIGKILL should do that now. trap '' HUP INT QUIT ABRT ALRM TERM STOP TSTP PWR mv "$TEMPFILE" "$FILE"