Unix Shell - Vi backups

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2006 > Vi backups





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Vi backups
Mitch Johnston

2006-03-15, 7:49 am

I have the following code in my .kshrc:

vi(){ # backup edited files
if [ ! -d ~/.bak ] # make sure the backup directory is in place
then
mkdir ~/.bak
fi

for FILE in $* # for each file passed to it
do
if [ -f $FILE ] # check to see if it's a file and not an argument
then
test -f ~/.bak/$FILE.4 && mv ~/.bak/$FILE.4 ~/.bak/$FILE.5
test -f ~/.bak/$FILE.3 && mv ~/.bak/$FILE.3 ~/.bak/$FILE.4
test -f ~/.bak/$FILE.2 && mv ~/.bak/$FILE.2 ~/.bak/$FILE.3
test -f ~/.bak/$FILE.1 && mv ~/.bak/$FILE.1 ~/.bak/$FILE.2
test -f ~/.bak/$FILE.0 && mv ~/.bak/$FILE.0 ~/.bak/$FILE.1
cp $FILE ~/.bak/$FILE.0
fi
done
if [ -x /usr/local/bin/vim ] # is Vim in place, then use it
then
/usr/local/bin/vim $*
else
/usr/bin/vi $*
fi
}

I find it very helpful, thought some of you might be interested.



--
..\\itch Johnston - Bearded Dragon
Chris F.A. Johnson

2006-03-15, 7:49 am

On 2006-03-15, Mitch Johnston wrote:
> I have the following code in my .kshrc:
>
> vi(){ # backup edited files
> if [ ! -d ~/.bak ] # make sure the backup directory is in place
> then
> mkdir ~/.bak
> fi
>
> for FILE in $* # for each file passed to it


Perhaps you are lucky enough not to have any files whose names
contain spaces or other pathological characters, but why take that
risk?

for FILE in "$@"

Or just:

for FILE

> do
> if [ -f $FILE ] # check to see if it's a file and not an argument


In case it has spaces, etc.:

if [ -f "$FILE" ]

> then
> test -f ~/.bak/$FILE.4 && mv ~/.bak/$FILE.4 ~/.bak/$FILE.5
> test -f ~/.bak/$FILE.3 && mv ~/.bak/$FILE.3 ~/.bak/$FILE.4
> test -f ~/.bak/$FILE.2 && mv ~/.bak/$FILE.2 ~/.bak/$FILE.3
> test -f ~/.bak/$FILE.1 && mv ~/.bak/$FILE.1 ~/.bak/$FILE.2
> test -f ~/.bak/$FILE.0 && mv ~/.bak/$FILE.0 ~/.bak/$FILE.1
> cp $FILE ~/.bak/$FILE.0
> fi
> done


More flexible is:

bu=5 ## Adjust to the number of backups you want
while [ $bu -gt 0 ]
do
bu_next=$(( $bu - 1 ))
test -f "$HOME/.bak/$FILE.$bu_next" &&
mv "$HOME/.bak/$FILE.$bu_next" "$HOME/.bak/$FILE.$bu"
bu=$bu_next
done
cp "$FILE" "$HOME/.bak/$FILE.0"

> if [ -x /usr/local/bin/vim ] # is Vim in place, then use it
> then
> /usr/local/bin/vim $*
> else
> /usr/bin/vi $*
> fi
> }


Or:

if type vim >/dev/null 2>&1
then
vim "$@"
elif type vi >/dev/null 2>&1
then
vi "$@"
else
emacs "$@" ## ;)
fi

> I find it very helpful, thought some of you might be interested.


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Dan Mercer

2006-03-15, 5:55 pm


"Mitch Johnston" <mxj_97@yahoo.com> wrote in message news:op.s6f9p3j33jmpd0@mitchell.tampabay.rr.com...
: I have the following code in my .kshrc:
:
: vi(){ # backup edited files
: if [ ! -d ~/.bak ] # make sure the backup directory is in place
: then
: mkdir ~/.bak
: fi

You can simply do "mkdir -p ~/.bak"

:
: for FILE in $* # for each file passed to it
: do
: if [ -f $FILE ] # check to see if it's a file and not an argument
: then
: test -f ~/.bak/$FILE.4 && mv ~/.bak/$FILE.4 ~/.bak/$FILE.5
: test -f ~/.bak/$FILE.3 && mv ~/.bak/$FILE.3 ~/.bak/$FILE.4
: test -f ~/.bak/$FILE.2 && mv ~/.bak/$FILE.2 ~/.bak/$FILE.3
: test -f ~/.bak/$FILE.1 && mv ~/.bak/$FILE.1 ~/.bak/$FILE.2
: test -f ~/.bak/$FILE.0 && mv ~/.bak/$FILE.0 ~/.bak/$FILE.1
: cp $FILE ~/.bak/$FILE.0
: fi
: done
: if [ -x /usr/local/bin/vim ] # is Vim in place, then use it
: then
: /usr/local/bin/vim $*
: else
: /usr/bin/vi $*
: fi
: }
:
: I find it very helpful, thought some of you might be interested.
:
:
:
: --
: .\\itch Johnston - Bearded Dragon


Mitch Johnston

2006-03-15, 8:48 pm

On Wed, 15 Mar 2006 07:04:58 -0500, Chris F.A. Johnson
<cfajohnson@gmail.com> wrote:

> On 2006-03-15, Mitch Johnston wrote:
>
> Perhaps you are lucky enough not to have any files whose names
> contain spaces or other pathological characters, but why take that
> risk?
>
> for FILE in "$@"
>
> Or just:
>
> for FILE
>
>
> In case it has spaces, etc.:
>
> if [ -f "$FILE" ]
>
>
> More flexible is:
>
> bu=5 ## Adjust to the number of backups you want
> while [ $bu -gt 0 ]
> do
> bu_next=$(( $bu - 1 ))
> test -f "$HOME/.bak/$FILE.$bu_next" &&
> mv "$HOME/.bak/$FILE.$bu_next" "$HOME/.bak/$FILE.$bu"
> bu=$bu_next
> done
> cp "$FILE" "$HOME/.bak/$FILE.0"
>
>
> Or:
>
> if type vim >/dev/null 2>&1
> then
> vim "$@"
> elif type vi >/dev/null 2>&1
> then
> vi "$@"
> else
> emacs "$@" ## ;)
> fi
>
>


OK,

I like the tips and will use them, except for the emacs! What are you mad
:D



--
..\\itch Johnston - Bearded Dragon
Chris F.A. Johnson

2006-03-15, 8:48 pm

On 2006-03-16, Mitch Johnston wrote:
> On Wed, 15 Mar 2006 07:04:58 -0500, Chris F.A. Johnson
><cfajohnson@gmail.com> wrote:
>
> OK,
>
> I like the tips and will use them, except for the emacs! What are you mad
>:D


I don't know what I was thinking:

if type vim >/dev/null 2>&1
then
vim "$@"
elif type emacs >/dev/null 2>&1
emacs "$@" ## ;)
elif type vi >/dev/null 2>&1
then
vi "$@"
fi

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
bsh

2006-03-21, 3:17 am

Mitch Johnston wrote:
> ...


I think you are trying too hard. As for the case of myself, I find
the simple vi(1) macro (that is put into file "~/.exrc)

map ^G :w! %~

(IIRC) sufficient to map CTRL-G to the action of copying the
edited file to filename "file~".

I tried to find a script that I know had existed that emulates
VMS-like versioning as a shell script (as you are attempting);
however, if you wish to do so at this level of sophistication
better tools exist for the purpose:

[url]http://www.faqs.org/docs/Linux-HOWTO/VMS-to-Linux-HOWTO. html#Numbered%20Backups%20Under%20Linux[
/url]

=Brian

Mitch Johnston

2006-03-21, 3:17 am

On Sun, 19 Mar 2006 18:37:01 -0500, bsh <brian_hiles@rocketmail.com> wrote:

> Mitch Johnston wrote:
>
> I think you are trying too hard. As for the case of myself, I find
> the simple vi(1) macro (that is put into file "~/.exrc)
>
> map ^G :w! %~
>
> (IIRC) sufficient to map CTRL-G to the action of copying the
> edited file to filename "file~".
>
> I tried to find a script that I know had existed that emulates
> VMS-like versioning as a shell script (as you are attempting);
> however, if you wish to do so at this level of sophistication
> better tools exist for the purpose:
>
> [url]http://www.faqs.org/docs/Linux-HOWTO/VMS-to-Linux-HOWTO. html#Numbered%20Backups%20Under%20Linux[
/url]
>
> =Brian
>

I wanted to keep 5 copies, plus if availible call vim. Nice macro non the
less and cool link.

..\\itch - eol


--
..\\itch Johnston - Bearded Dragon
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com