从bash脚本

时间:2016-03-18 07:43:56

标签: bash vim

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

到目前为止,我所能想到的就像下面的内容

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支持?

3 个答案:

答案 0 :(得分:4)

当我问我的vim vim --version时,它会吐出这样的东西:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 16 2015 08:44:57)
Included patches: 1-872
Compiled by <alexpux@gmail.com>
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支持(如果Python不可用,HAS_PYTHON将是0):

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

明确检查Python 3(如果Python 3不可用,HAS_PYTHON3将再次为0):

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

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

答案 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

应该解决你的问题

答案 2 :(得分:0)

如果你已经有一个正在运行的Vim实例,或者你可以创建一个,你总是可以问Vim本身:

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

但是如果Vim还没有运行它,可能会更直接地解析--version输出,如其他答案所示。