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 ]
|