我有一个 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
Run Code Online (Sandbox Code Playgroud)
目前我能想到的就是检查$vim_info预期的 vim 版本和 python 支持。
将问题归结为有意义的句子:
如何检查 vim 版本是否大于或等于 7.4 并且是否有来自 bash 脚本的 python 支持?
当我问我的 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
Run Code Online (Sandbox Code Playgroud)
因此,在这里解析输出将是一个不错的选择。
找到版本号并将其存储在VIMVERSION:
VIMVERSION=$(vim --version | head -1 | cut -d ' ' -f 5)
Run Code Online (Sandbox Code Playgroud)
从这里开始,检查在 bash shell 脚本中如何将字符串转换为数字,以了解如何将字符串结果与您需要的最小值进行比较7.4。
检查 Python 支持(如果 Python 不可用,HAS_PYTHON将检查0):
HAS_PYTHON=$(vim --version | grep -c '+python')
Run Code Online (Sandbox Code Playgroud)
明确检查 Python 3(如果 Python 3 不可用,HAS_PYTHON3则再次检查0):
HAS_PYTHON3=$(vim --version | grep -c '+python3')
Run Code Online (Sandbox Code Playgroud)
这可能有点粗糙,但我想你明白了。