0
let os = system('uname -o')
function Strcmp(str1, str2)
    if a:str1 < a:str2
        return -1
    elseif a:str1 == a:str2
        return 0
    else
        return 1
    endif
endfunction

echo Strcmp(os,"Android")

I do not know why does it return 1 when os stores Android

I tried echo os and it printed Android, still this function returns 1

3
  • are you sure os is not Android\n?
    – Maxim Kim
    Mar 29 at 11:07
  • 1
    Whenever encountering situations like this (in any language) it's always good to run the strings through a length function (e.g. strlen()) and/or, if available, a function that makes control characters visible like Vim's strtrans() which in this case will probably show Android^@. Then you'll have a better chance of figuring out why the difference exists.
    – B Layer
    Mar 29 at 11:16
  • 1
    Also, FYI, it's best to try to post a minimal reproducible example. Your example is reducible to something like if system('uname -o') !=# "Android" | echo 'No match!' | endif. In the process of reducing things you might even figure out what's going on. Welcome to Vi&Vim SE.
    – B Layer
    Mar 29 at 13:04
1

The system() function captures the full output of the command; in practice this almost always ends with a newline (\n); almost all Unix commands end with this as the last character.

This is common gotcha with system(). You can use the trim() function to remove any leading or trailing whitespace:

let os = trim(system('uname -o'))

Or you can use string slicing to remove the last byte:

let os = system('uname -o')[:-2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.