I'm facing a peculiar issue when using vi on solaris. When i open a file using vi & search for a string pattern & if that pattern is not found & if i exit, vi exits with return value 1. (Checked the return value with 'echo $?' ).
When the string is found, vi exits with return value 0.
This however is not an issue with vim on solaris.
Also, vi does not show this behavior on other ( e.g. rhel, suse) setups.
I would choose sed or ex in a script. The vi is too screen oriented for script life, unless you are into firing your optical neurons a lot, usually not a cash deliverable.
Do you need to know what happened inside vi using the $? 0/1 behavior?
BTW, I have a vi wrapper I call vix:
for (some) xterms, it saves my scroll history: drives the on screen text up by screen size lines, then calls vi
for ksh command editing, set -o vi/viraw, export $EDITOR=~/bin/vix: always returns 0 so my output is not discarded by ksh.
#!/usr/bin/ksh
(
stty -a | sed '
s/^/ /
t a
:a
s/.* rows = \([1-9][0-9]*\).*/\1/
t
d
' | read zr
# echo $zr >&2
if [ "$zr" = "" ]
then
zr=25
fi
while [ "$zr" != -1 ]
do
zr=$(( $zr - 1 ))
echo >/dev/tty
done
vi "$@"
exit 0
)
Interesting. You didn't mention the bigger picture of what you are trying to do, but maybe you would be better off using grep to see if a string exists in a file, or sed if you need to change that string?
Solaris' crontab command does something similar, when you call it with the -e switch to edit your crontab.
EDITOR=/usr/bin/vi crontab -e
starts the Solaris vi with a temporary copy of your crontab file. After vi ends, the exit status is checked. If it is not 0, then the crontab command says
The editor indicates that an error occurred while you were
editing the crontab data - usually a minor typing error.
Edit again, to ensure crontab information is intact (y/n)?
('n' will discard edits.)
If the user answers "yes", the editor is started again with the same temporary copy of the crontab file. If the answer is "no", the temporary copy is discarded.
Maybe you could implement something similar in your script.