Problems using value returned by external command

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems using value returned by external command

Spiros Bousbouras
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 ?

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Tony Mechelynck
On 02/04/11 03:57, 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
>
> 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 ?
>

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.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Tony Mechelynck
On 02/04/11 04:18, Tony Mechelynck wrote:

> On 02/04/11 03:57, 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
>>
>> 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 ?
>>
>
> 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

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

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Ben Schmidt
In reply to this post by Spiros Bousbouras
On 2/04/11 12:57 PM, 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
>
> 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.

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.



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Spiros Bousbouras
On Apr 2, 4:52 am, Ben Schmidt <[hidden email]> wrote:

> On 2/04/11 12:57 PM, 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
>
> > 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.
>
> Try
>
>        if l:r == "1\n"

Yes , that works , thank you.

> 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.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

bill lam
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

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Gary Johnson-4
In reply to this post by Spiros Bousbouras
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

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Nikolay Aleksandrovich Pavlov
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

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Ben Schmidt
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.



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Problems using value returned by external command

Tony Mechelynck
In reply to this post by Nikolay Aleksandrovich Pavlov
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.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php