Friday, April 1, 2011

Re: Problems using value returned by external command

Reply to message «Re: Problems using value returned by external command»,
sent 10:21:48 02 April 2011, Saturday
by Gary Johnson:

> As you've figured out, Vim's system() function includes in its
> return value the newline at the end of the command's output. If you
> need the result without the newline, use substitute() like this:
>
> let x = substitute(system("some command"), "\n", "", "")
This will remove all newlines, not just at the end. If one knows that command
will output newline at the end, then he should just use system(...)[:-2].

Original message:
> On 2011-04-01, Spiros Bousbouras wrote:
> > The following on GNU/Linux with vim version 7.1
> >
> > prompt> cat myscript
> > #!/bin/sh
> > echo 1
> >
> > prompt> cat myscript.vim
> > function! Check()
> >
> > let l:r = system("./myscript")
> > if l:r == "1"
> >
> > echo "Good"
> >
> > else
> >
> > echo "Length of l:r is" strlen(l:r)
> > echoerr "l:r is" l:r
> >
> > endif
> >
> > endfunction
>
> As you've figured out, Vim's system() function includes in its
> return value the newline at the end of the command's output. If you
> need the result without the newline, use substitute() like this:
>
> let x = substitute(system("some command"), "\n", "", "")
>
> HTH,
> Gary

No comments:

Post a Comment