Vi issue with solaris

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.

Anybody can help me regarding this issue ???:confused:

Why does it matter what vi's return value is? Is it part of a script?

Yes, it is a part of a script ....

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?

Tell us what the vi part does for your data.

It can be argued this is actually a vim non conformance and Solaris vi is behaving correctly by sticking to the POSIX specification (vi) which states:

EXIT STATUS The following exit values are returned: 

 0
       Successful completion.
 >0
       An error occurred.   

I have a file, which contains many string values to be used for further use.

The script does the following operations if the user wants to change any/all of the values:

  1. Open that file using vi.
  2. Change the respective values to desired value.
  3. if(return status of vi != 1)
    save the file & proceed ahead.
    else
    exit.

due to this vi problem, the flow goes to the 'else' part & exits without saving the file.

can anybody suggest something.

Well, it sounds more appropriate to use sed, grep, awk or PERL.

Does vi exiting 1 tell you something about file validity?
Something about run time errors?

If fields were missing, invalid, out of place or files were not writable, those are easy to check.

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.