Vi backups
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Vi backups




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Vi backups  
Mitch Johnston


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 12:49 PM

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





[ Post a follow-up to this message ]



    Re: Vi backups  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 12:49 PM

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





[ Post a follow-up to this message ]



    Re: Vi backups  
Dan Mercer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10: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







[ Post a follow-up to this message ]



    Re: Vi backups  
Mitch Johnston


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 01:48 AM

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





[ Post a follow-up to this message ]



    Re: Vi backups  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 01:48 AM

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





[ Post a follow-up to this message ]



    Re: Vi backups  
bsh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-21-06 08: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:

http://www.faqs.org/docs/Linux-HOWT...
Under%20Linux

=Brian






[ Post a follow-up to this message ]



    Re: Vi backups  
Mitch Johnston


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-21-06 08: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:
>
> http://www.faqs.org/docs/Linux-HOWT...20Under%20Linux
>
> =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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:12 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register