Problems using value returned by external command

5 views
Skip to first unread message

Spiros Bousbouras

unread,
Apr 1, 2011, 9:57:25 PM4/1/11
to vim_use
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

I start a vim session and do

:source myscript.vim
:echo Check()

and get

Length of l:r is 2
Error detected while processing function Check:
line 6:
l:r is 1^@
0

Replacing inside Check()
if l:r == "1"
with
if l:r == "1\000"
doesn't make any difference.

Is this a bug ?

Tony Mechelynck

unread,
Apr 1, 2011, 10:18:06 PM4/1/11
to vim...@googlegroups.com, Spiros Bousbouras

I don't know if it's a bug or a feature, but here I get the following:

:echo (system('echo 1') == "1")
0
:echo (system('echo 1') == "1\000")
0
:echo (system('echo 1') == 1
1
:echo (system('echo 1 2 3') == 1
1

Note that in the latter two cases, Vim's string-to-integer conversion
steps in.


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
43. You tell the kids they can't use the computer because "Daddy's got
work to
do" and you don't even have a job.

Tony Mechelynck

unread,
Apr 1, 2011, 10:21:20 PM4/1/11
to vim...@googlegroups.com, Spiros Bousbouras

Oops: forgot to close the parens below

>
:echo (system('echo 1') == 1)

> 1
> :echo (system('echo 1 2 3') == 1
:echo (system('echo 1 2 3') == 1
> 1
>
> Note that in the latter two cases, Vim's string-to-integer conversion
> steps in.
>
>
> Best regards,
> Tony.
--

A non-vegetarian anti-abortionist is a contradiction in terms.
-- Phyllis Schlafly

Ben Schmidt

unread,
Apr 1, 2011, 11:52:48 PM4/1/11
to vim...@googlegroups.com, Spiros Bousbouras

Try

if l:r == "1\n"

I think it's :echoerr that is not displaying the \n in the string
correctly, but interpreting it as if it were \n in a buffer (which
stands in place of \0). If you just used :echo you would see 1 followed
by a blank line.

Ben.

Spiros Bousbouras

unread,
Apr 1, 2011, 11:59:50 PM4/1/11
to vim_use
Yes , that works , thank you.

bill lam

unread,
Apr 2, 2011, 12:05:45 AM4/2/11
to vim_use
If your shell or echo command support this option, try
echo -n "Good"

--
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

Gary Johnson

unread,
Apr 2, 2011, 2:21:48 AM4/2/11
to vim_use
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

ZyX

unread,
Apr 2, 2011, 2:52:45 AM4/2/11
to vim_use
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:

signature.asc

Ben Schmidt

unread,
Apr 2, 2011, 3:15:52 AM4/2/11
to vim...@googlegroups.com, ZyX
On 2/04/11 5:52 PM, ZyX wrote:
> 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.

No, it will remove the first newline, whether it's at the end or not.

> If one knows that command will output newline at the end, then he
> should just use system(...)[:-2].

Yes.

Or if you don't, use "\n$" as the pattern. Slower, but will only catch
an end-of-string newline and only if it exists.

Ben.

Tony Mechelynck

unread,
Apr 2, 2011, 3:22:07 AM4/2/11
to vim...@googlegroups.com
On 02/04/11 08:52, ZyX wrote:
> 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].

If a newline at the end is possible but not certain, and must be
removed, just use (untested)

substitute(system('foo bar baz'), '\n$', "", "")

where (IIUC)
\n (within single quotes) is passed unchanged to substitute() and
*then* interpreted by it as a linefeed
$ means end-of-string


Best regards,
Tony.
--
Stay away from flying saucers today.

Reply all
Reply to author
Forward
0 new messages