使用Shell脚本自动执行VIM EDITOR并更新值(类似于通过Shell脚本编辑crontab) [英] Automate VIM EDITOR and update values using shell script (similar to editing crontab via shell script)

查看:145
本文介绍了使用Shell脚本自动执行VIM EDITOR并更新值(类似于通过Shell脚本编辑crontab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过类似于Shell脚本的crontab编辑命令来自动执行VI编辑,但到目前为止对我还不起作用。

I am trying to automate a VI edit for a command similar to crontab editing via Shell script but not working for me so far.

这是最终的json admin为true:

Here is the final json with admin as true:

'{"name":"SQLSRVR","admin":"true","json_class":"Chef::ApiClient","chef_type":"client"}'

您可以看到必须设置EDITOR环境变量或将其作为命令行选项传递-e

As you can see the EDITOR environment variable has to be set or passed as command line option -e

[root@vrhost user]# knife client edit SQLSRVR
ERROR: RuntimeError: Please set EDITOR environment variable

[root@vrhost user]# knife client edit
USAGE: knife client edit CLIENT (options)
-s, --server-url URL             Chef Server URL
-k, --key KEY                    API Client Key
    --[no-]color                 Use colored output, defaults to enabled
-c, --config CONFIG              The configuration file to use
    --defaults                   Accept default values for all questions
-d, --disable-editing            Do not open EDITOR, just accept the data as is
-e, --editor EDITOR              Set the editor to use for interactive commands
-E, --environment ENVIRONMENT    Set the Chef environment
-F, --format FORMAT              Which format to use for output
-u, --user USER                  API Client Username
    --print-after                Show the data after a destructive operation
-V, --verbose                    More verbose output. Use twice for max verbosity
-v, --version                    Show chef version
-y, --yes                        Say yes to all prompts for confirmation
-h, --help                       Show this message
FATAL: You must specify a client name

以下命令将打开vim编辑器以进行编辑从[ admin: false]更改为[ admin: true]:

The below command opens a vim editor for editing to make the change from ["admin": "false"] to ["admin": "true"]:

[root@vrhost user]# knife client edit SQLSRVR -e vim 

{
  "name": "SQLSRVR",
  "admin": false,
  "json_class": "Chef::ApiClient",
  "chef_type": "client",
}

我正在尝试通过shell脚本执行此操作,并且希望将其自动化并尝试了许多选项,但到目前为止还没有运气。

I am trying to do this through a shell script and would like to automate it and tried many options but had no luck so far.

[root@vrhost ~]# (echo ^[:g/false/s/false/true/^[:wq!^M) | knife client edit SQLSRVR -e vim
Vim: Warning: Input is not from a terminal
Object unchanged, not saving

[root@vrhost user]# echo (^[echo     '{"name":"SQLSRVR","admin":"true","json_class":"Chef::ApiClient","chef_type":"client"}'^[:w    q!^M) | knife client edit SQLSRVR -e

[root@vrhost ~]# knife client show SQLSRVR
admin:       false
chef_type:   client
json_class:  Chef::ApiClient
name:        SQLSRVR

这与通过shell脚本自动执行crontab编辑非常相似,但是没有

this is very similar to automating crontab editing via shell script but this has not been working for me.

推荐答案

除非确实需要特殊的Vim功能,否则最好使用非交互式工具,例如 sed awk 或Perl / Python / Ruby / 此处最喜欢的脚本语言

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

也就是说,您可以使用静默批处理模式非交互式地使用Vim。

That said, you can use Vim non-interactively, using silent batch mode.

vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"

除了使用外部脚本通过 -S commands.ex 读取命令外,您还可以提供一些直接通过 -c cmd1 -c cmd2 命令。有关更多信息,请参见:help -s-ex

Instead of the external script to read the commands from via -S "commands.ex", you can also give a few commands directly via -c cmd1 -c cmd2. See :help -s-ex for more information.

这篇关于使用Shell脚本自动执行VIM EDITOR并更新值(类似于通过Shell脚本编辑crontab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆