2016-03-18 43 views
1

我有一个bash脚本,依赖于vim至少是版本7.4并且安装了python。我需要检查上面的条件是否匹配,如果不退出并要求用户更新他们的vim。从bash脚本检查vim版本和python支持

到目前为止,所有我能想到的是类似下面

has_vim = command -v vim >/dev/null 

if ! $has_vim; then 
    echo "must have vim installed." 
    exit 1 
fi 

// Here I want do as the following pseudo code 
vim_info = $(vim --version | grep python) 

// suggest me if there is another way 
vim_version = // find version info from $vim_info 
has_python_support = // find python support from $vim_info 

if ! $vim_version >= 7.4 && ! has_python_support; then 
    echo "vim version must be at least 7.4 and must be installed with python support" 
fi 

// everything is ok. carry on 

目前,所有我能想到的是检查$vim_info的预期VIM版本和Python的支持。

归结问题转化为有意义的句子:

如何检查Vim版本是大于或等于7.4,从bash脚本有Python支持?

+0

如果你不想两次运行'vim --version':'vim --version | sed -n'1s/[^ 0-9] * \([0-9] [[0-9。] * \)。*/\ 1/p;/+ python/{s /.*/ python/p; q}' – andlrc

回答

4

当我问我的vim --version VIM,它吐出来的是这样的:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 16 2015 08:44:57) 
Included patches: 1-872 
Compiled by <[email protected]> 
Huge version without GUI. Features included (+) or not (-): 
+acl    +farsi   +mouse_netterm +syntax 
+arabic   +file_in_path +mouse_sgr  +tag_binary 
+autocmd   +find_in_path -mouse_sysmouse +tag_old_static 
-balloon_eval +float   +mouse_urxvt  -tag_any_white 
-browse   +folding   +mouse_xterm  -tcl 
++builtin_terms -footer   +multi_byte  +terminfo 
+byte_offset  +fork()   +multi_lang  +termresponse 
+cindent   +gettext   -mzscheme  +textobjects 
-clientserver -hangul_input +netbeans_intg +title 
+clipboard  +iconv   +path_extra  -toolbar 
+cmdline_compl +insert_expand +perl/dyn  +user_commands 
+cmdline_hist +jumplist  +persistent_undo +vertsplit 
+cmdline_info +keymap   +postscript  +virtualedit 
+comments  +langmap   +printer   +visual 
+conceal   +libcall   +profile   +visualextra 
+cryptv   +linebreak  +python/dyn  +viminfo 
+cscope   +lispindent  +python3/dyn  +vreplace 
+cursorbind  +listcmds  +quickfix  +wildignore 
+cursorshape  +localmap  +reltime   +wildmenu 
+dialog_con  -lua    +rightleft  +windows 
+diff   +menu   +ruby/dyn  +writebackup 
+digraphs  +mksession  +scrollbind  -X11 
-dnd    +modify_fname +signs   -xfontset 
-ebcdic   +mouse   +smartindent  -xim 
+emacs_tags  -mouseshape  -sniff   -xsmp 
+eval   +mouse_dec  +startuptime  -xterm_clipboard 
+ex_extra  -mouse_gpm  +statusline  -xterm_save 
+extra_search -mouse_jsbterm -sun_workshop -xpm 

所以,分析其输出会在这里一个不错的选择。

查找版本号,并将其存储在VIMVERSION

VIMVERSION=$(vim --version | head -1 | cut -d ' ' -f 5) 

从这里,检查In bash shell script how do I convert a string to an number对于如何比较反对您的最低需要7.4的字符串结果。

检查Python支持(HAS_PYTHON0如果Python是不可用):

HAS_PYTHON=$(vim --version | grep -c '+python') 

检查明确为Python 3(再次,HAS_PYTHON30如果Python 3中不可用):

HAS_PYTHON3=$(vim --version | grep -c '+python3') 

这可能有点粗糙,但我认为你明白了。

+0

Okies很棒。我会给它一个镜头。感谢那。 – Subash

+0

或者:'VIM_VERSION = $(vim --version | head -1 | egrep -o'[0-9] + \。[0-9] +')','vim --version | egrep -wq'\ + python'&& HAS_PYTHON = 1' –

1
#!/bin/bash 
has_vim=$(command -v vim >/dev/null) 

if ! $has_vim; then 
    echo "must have vim installed." 
    exit 1 
fi 

# Checking the python support based on the line output received 
has_python_support=$(vim --version | grep -c python) 

# Matching the decimal pattern from the first line 
vim_version=$(vim --version | head -1 | grep -o '[0-9]\.[0-9]') 

if [ ! $(echo "$vim_version >= 7.4" | bc -l) ] && [ ! $has_python_support ]; then 
    echo "vim version must be at least 7.4 and must be installed with python support" 
else 
    echo "vim version is > 7.4" 
fi 

应该解决您的问题

0

如果你有一个运行中的Vim实例已经,或者您可以创建一个,你永远可以要求Vim本身:

$ vim --servername SOME_NAME --remote-expr "has('python')" 
1 
$ vim --servername SOME_NAME --remote-expr "v:version" 
704 

但如果Vim的ISN已经运行它可能更直接地解析--version输出,如其他答案中的建议。