#!/bin/bash ## unchg -- undo an erroneous chg[sed] command -- by Eugene Reimer 2001; ## ## Copyright © 2001,2007 Eugene Reimer; can be used, modified, copied, and distributed or sold under the terms of either the LGPL or the GPL (your choice); ## see http://www.gnu.org/licenses for the details of these terms. while [ $# -gt 0 ] && [[ $1 == -* ]]; do ##parse the options case "$1" in --tmp*) TMPBKUP=1 ;; --tes*|-t) TEST=1 ;; --h*|-\?) CMD="help"; HELP=0 ;; --v*|-v) VERS=1 ;; --) shift; break ;; esac shift; ##remove that arg done [ $VERS ] && echo "unchg version:$(date -r$0 +%y%m%d). Copyright 1998,2005 Eugene Reimer. Licensed under the GPL" && exit 0 [ "$CMD" = "" ] && [ $# -eq 0 ] && HELP=2 if [ $HELP ]; then echo "usage: unchg options file..." echo " or: unchg options --tmpbackup file..." echo "Restores file F from F~ or from under /tmp where the filename under /tmp is as made by chg." echo " --tmpbackup backup found under /tmp, instead of tilde-file in current directory" echo " --test | -t just show which files would be affected, but change nothing" echo " --help | -? just show this info on usage" echo " --version | -v just show version info" exit $HELP; ##exit fi for f in "$@"; do [ -f "$f" ] || continue fbk="$f~"; [ $TMPBKUP ] && fbk=/tmp/bk-chg-$(fullnameNOSLASH "$f") ##(A) tilde-bkup in same dir; (B) bkup under /tmp dir [ -f "$fbk" ] || { echo "##---$fbk not found---"; continue; } ##backup doesnot exist: inform user, and continue if [ $TEST ]; then echo "/bin/mv -fv -- \"$fbk\" \"$f\"" ##just SHOW the mv command else #/bin/mv -f -- "$f" "$f~unchg$$" ##rename to temp for swap-renaming YANKED /bin/mv -fv -- "$fbk" "$f" ##EXECUTE the mv command, with informative msg #/bin/mv -f -- "$f~unchg$$" "$fbk" ##rename temp for swap-renaming so next unchg is a redo YANKED fi done exit CONSIDER: making --tmp the default (only) backup option in chg?? IDENTIFYING backup-copies "made" on a certain date: display the "status-chg" timestamp, using: d-status (or ls -c --full); 2005may23: added --tmpbackups option, to restore from /tmp/bk-chg-$(fullnameNOSLASH $f) 2007jan30: use /bin/mv to ensure my sdb6-backups are unaffected (as they are by chg) 2010-05-02: while working on weblinkcheck.cron in a test-run, unchg, try-again mode, realized I'd be happier without SWAP-style renaming; ie: when in doubt about whether or not I've done the undoing, would rather have a 2nd undo-attempt fail (than be a redo); ==YANKED the swap-renaming;