0

I want to have a function in my .vimrc that will run a bash script, check the output, and do different things depending on the bash script output.

Currently, my bash script returns "yes". My vim function is then:

function! Callscript()

  if 'r ! . test.sh' == 'yes'
    echom "returned yes"
  else
    echom "returned something else"
  endif
endfunction

This will always print "returned something else". When I run the command ':r ! . test.sh', it returns:

test.sh: line 5: return: yes: numeric argument required

I didn't see anything in :help r! that mentioned doing anything extra to get the return values, but I tried:

  if 'r ! . test.sh'.return == 'yes'
  if 'r ! . test.sh':return == 'yes'

which gave me errors. I have tried using system, but could not get that to work. The most obvious approach I tried with system was:

  if system('r ! . test.sh') == 'yes'

When I try system('. test.sh') in the editor, it tells me system is not an editor command.

How can I access the return value of the bash script so that I can use it in my vim function?

1
  • What does 'my bash script returns "yes"' mean? Usually processes return an exit-code, rather than a string. Commented May 1, 2014 at 19:00

1 Answer 1

3
  if 'r ! . test.sh' == 'yes'

That syntax is nonsense. First, the :r !{cmd} is reading the output of {cmd} into the current buffer, which is probably not what you want. To get this into a variable, use :let output = system('test.sh'), or directly compare via:

if system('test.sh') == 'yes'

Note that comparing the output (especially if it's a true / false or success / failure boolean value as suggested by your question) is unusual. Rather, the script's exit status is checked (e.g. with commands like grep). You can get that in Vim via the special v:shell_error variable (which is filled by both system() and :r !).

Sign up to request clarification or add additional context in comments.

1 Comment

I see. Thank you for the information. I have changed the bash script to have an exit code of 1 or 0 and changed the vim script if to call system('. test.sh') if v:shell_error == 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.