| Sylvain Robitaille 2007-11-19, 1:33 pm |
| andrea wrote:
> I've done this (pretty simple but working)
Too simple, unfortunately. Check out below ...
> It's a wrapper for your favourite editor, it backs up the file only if
> you changed it...
Except it forces $EDITOR to vim (should perhaps do that only if $EDITOR
isn't already set), and it functions on only one file at a time, which
is contrary to most people's favorite editors. That becomes
unintuitive for most people, though it may still work out alright for
your own purposes.
What if we're working together, your favorite text editor is vim (so
you're happy with the script), and my $EDITOR is already set to emacs?
> F=$1
What if I wanted to edit 3, 5, 7, or "n" files at once? (what if the
argument contains a wildcard, expanded by the shell into "n" files?)
> ...
> # realpath e base path
> RP=`realpath $F`
> BP=`dirname $RP`
> ...
> FNAME=$RP.`date +%Y%m%d`
> P=/tmp$BP
>
> echo "creating temp directory"
> mkdir -p $P
> echo "copying temp file"
> # uso il path assoluto
> cp -v $RP /tmp$FNAME
> ...
> echo "removing temp files"
> rm -rv $P
You have a dangerous use of the /tmp directory here. You also make
no effort to test for the existence of what you're placing into /tmp,
either before or after. A malicious user (assume, for example, someone
unauthorized to use the system, but who managed to compromise a regular
user account) could cause some serious trouble by placing a symlink into
/tmp, pointing at somewhere important (such as /etc, /usr, or even /)
on your system. You'll get error messages, but your script will forge
ahead faster than you can read them and by the time you realized what
just happened it would be too late.
> if [[ `diff $RP /tmp$FNAME` ]]
The diff is backwards. You'll find it more intuitive to follow what has
changed if you reverse the order of the files being diffed here.
I hope I've helped ...
--
----------------------------------------------------------------------
Sylvain Robitaille syl@alcor.concordia.ca
Systems and Network analyst Concordia University
Instructional & Information Technology Montreal, Quebec, Canada
----------------------------------------------------------------------
|