Recently I lost a number of changes I made to a program when the SCO Unix system went down. The system "mail" suggested a "vi -r" option that took me back several days. To prevent this in the future, I am trying to create my own vi command:
if [ $# -eq 1 ]
then
cp -p $1 $1.bak
fi
/usr/bin/vi $*
if [ $# -eq 1 ]
then
rm $1.bak
fi
I put this in my home directory where my PATH in ".profile" is
I'm not sure what you think a PATH variable does. If a file has execute permissions (meaning you can run it by just typing the correct name of the file) then it can be run by typing the whole filename from anywhere
# example:
/path/to/shell.sh parameter1
This is awkward if you have your current directory somewhere else. So. PATH allows you to put directories into the variable. Then when you type the name no matter where your current directory is set, the PATH looks up the file, then lets you run it.
Example after adding the directory where shell.sh lives to PATH:
shell.sh parameter1
Not delete it
Not write to it.
Just execute it.
The which command helps you figure out what file you will execute before you try to run it. Why? Well, in the example: shell.sh could be in several places. So if you type shell.sh you could be running another script by accident. PATH can be a cause of this.
export PATH=".:${PATH}"
makes the current directory part of the PATH. Dangerous.
Example: I use t.shl as a throwaway temporary filename. So it could be in any of the my own directories.
which t.shl
tells me which one will run when I type the command.
$@ muist be in quotes. Unquoted it will expand like $*
$ cat demo
echo '$@ - word splitting takes place'
for f in $@
do
echo "$f"
done
echo '"$@" - quotes prevent word splitting; $@ (and [@] in arrays in later shells) split into arguments (elements) nevertheless'
for f in "$@"
do
echo "$f"
done
$ /bin/sh demo "" 1 2 "3 4"
$@ - word splitting takes place
1
2
3
4
"$@" - quotes prevent word splitting; $@ (and [@] in arrays in later shells split into arguments (elements) nevertheless
1
2
3 4
As a last exercise try
/bin/sh demo "*"
You see that unquoted the shell does a glob-expansion against the current directory. (That you can prevent with set -f )
if [ $# -eq 1 -a -s $1 ]
then
cp -p $1 $1.bak
echo "While using vi, $1.bak has been created."
sleep 1
fi
/usr/bin/vi $*
if [ -f $1.bak ]
then
rm $1.bak
fi
I realize vi can edit a number of files, start at a specific position, or after a search, but we seldom use any extra bells or whistles. Near the end of a Y2K conversion script, a vi searched for "-YEAR" in the command line, but that was 19 years ago.
Thanks for looking at it, I might revisit this thread and check for new ideas in the near future.