【问题标题】:Automate VIM EDITOR and update values using shell script (similar to editing crontab via shell script)使用 shell 脚本自动化 VIM EDITOR 和更新值(类似于通过 shell 脚本编辑 crontab)
【发布时间】:2013-03-25 17:38:36
【问题描述】:

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

这是 admin 为 true 的最终 json:

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

如您所见,EDITOR 环境变量必须设置或作为命令行选项 -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"]:

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

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

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

[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 非常相似,但这对我不起作用。

【问题讨论】:

  • 我在这个网站上看到了类似的东西link
  • 也许我遗漏了一些东西,但使用sed而不是尝试自动化vi不是更好吗?
  • cdarke - 这与通过 shell 脚本(使用 vi)编辑 crontab 类似的问题。是的,可以通过保存为临时文件然后加载它来完成替代方案,但在这种情况下,命令需要一个编辑器,我不太熟悉它的工作原理,并试图通过 shell 脚本模拟通过 vi 进行编辑
  • Ingo - 尝试该解决方案让我看看它是否有效,但我遇到的问题类似于 crontab 编辑。查看eggi提供的解决方案-daniweb.com/software-development/shell-scripting/threads/166819/…
  • 您找到解决此问题的方法了吗?请不要忘记将答案标记为正确! :)

标签: vim chef-infra


【解决方案1】:

除非您真的需要特殊的 Vim 功能,否则最好使用非交互式工具,例如 sedawk 或 Perl / Python / Ruby / 您最喜欢的脚本语言 .

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

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

除了通过-S "commands.ex" 读取命令的外部脚本,您还可以直接通过-c cmd1 -c cmd2 给出一些命令。请参阅:help -s-ex 了解更多信息。

【讨论】:

    【解决方案2】:

    看看

    $ knife client edit --help
    [...]
    -d, --disable-editing            Do not open EDITOR, just accept the data as is
    

    所以我猜你可以更改值而无需在 vim 中进行编辑。只是:

    • 获取json格式的客户端数据。
    • 用 sed 替换所需的值。
    • 从文件上传数据。

    代码:

    $ knife client show -Fj SQLSRVR > SQLSRVR.json
    $ sed -i.old "s/\"admin\": true,/\"admin\": false,/" SQLSRVR.json
    $ knife client edit -d SQLSRVR < SQLSRVR.json
    

    类似的东西。

    【讨论】:

    【解决方案3】:

    这里有一些参考链接:

    i) http://mirror.hep.wisc.edu/stable/chef/chef-server-webui/app/controllers/clients_controller.rb

    ii) http://www.rubydoc.info/github/opscode/chef/master/Shell/Extensions - 尝试过但无法正常工作

    最后做了以下事情(它确实给了 409 第二次待命,我不需要第二次这样做):

    # call to below rb, CLIENTNAME is the name of the client and STATE is true/false
    $ knife exec clienttransform.rb CLIENTNAME STATE
    
    $ cat clienttransform.rb
    
    Chef::Config[:solo] = false
    
    class Company
      class TransformClient
    
        attr_accessor :clientname
        attr_accessor :isclientadmin
    
        def initialize(client_name, is_client_admin)
          @clientname = client_name
          @isclientadmin = is_client_admin
        end
    
        def transform
          client=Chef::ApiClient.load(@clientname)
          # puts "client.name : " + client.name
          # puts "client.admin : " + client.admin.to_s
          # puts "XX - clientname : " + @clientname
          # puts "XX - isclientadmin : " + @isclientadmin.to_s
          boolisclientadmin = !!@isclientadmin
          client.admin(boolisclientadmin)
          client.save()
        end
    
      end
    
    end
    
    client_name = ARGV[2].to_s()
    is_client_admin = ARGV[3].to_s()
    
    # puts "YY - client_name : " + client_name
    # puts "YY - is_client_admin : " + is_client_admin
    
    trc = Company::TransformClient.new(client_name, is_client_admin)
    trc.transform
    
    exit 0
    

    【讨论】:

      【解决方案4】:

      只需设置您的编辑器,它就会工作。就我而言,我使用 vim 编辑器,这就是为什么我的命令如下:

      export EDITOR=vim
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        相关资源
        最近更新 更多