如何将 bash 变量插入到 ruby​​ 命令中?

如何将 bash 变量插入到 ruby​​ 命令中?

我正在使用 bash shell。我定义了以下变量...

localhost:tmp davea$ echo $json
{"id": "abc", "name": "dave"}

我想将此 var 的值插入到 ruby​​ 命令中,所以我尝试了这个......

localhost:tmp davea$ ruby -rjson -e 'j = JSON.parse($json); puts j["Instances"][0]["ImageId"]'
Ignoring bindex-0.5.0 because its extensions are not built.  Try: gem pristine bindex --version 0.5.0
Ignoring byebug-11.0.1 because its extensions are not built.  Try: gem pristine byebug --version 11.0.1
Ignoring byebug-10.0.2 because its extensions are not built.  Try: gem pristine byebug --version 10.0.2
Ignoring byebug-10.0.1 because its extensions are not built.  Try: gem pristine byebug --version 10.0.1
Ignoring byebug-10.0.0 because its extensions are not built.  Try: gem pristine byebug --version 10.0.0
Ignoring childprocess-1.0.1 because its extensions are not built.  Try: gem pristine childprocess --version 1.0.1
Ignoring debase-0.2.4 because its extensions are not built.  Try: gem pristine debase --version 0.2.4
Ignoring eventmachine-1.2.5 because its extensions are not built.  Try: gem pristine eventmachine --version 1.2.5
Ignoring ffi-1.11.1 because its extensions are not built.  Try: gem pristine ffi --version 1.11.1
Ignoring ffi-1.9.25 because its extensions are not built.  Try: gem pristine ffi --version 1.9.25
Ignoring ffi-1.9.23 because its extensions are not built.  Try: gem pristine ffi --version 1.9.23
Ignoring ffi-1.9.18 because its extensions are not built.  Try: gem pristine ffi --version 1.9.18
Ignoring hiredis-0.6.1 because its extensions are not built.  Try: gem pristine hiredis --version 0.6.1
Ignoring json-2.1.0 because its extensions are not built.  Try: gem pristine json --version 2.1.0
Ignoring nio4r-2.4.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.4.0
Ignoring nio4r-2.3.1 because its extensions are not built.  Try: gem pristine nio4r --version 2.3.1
Ignoring nio4r-2.3.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.3.0
Ignoring nio4r-2.2.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.2.0
Ignoring nokogiri-1.10.4 because its extensions are not built.  Try: gem pristine nokogiri --version 1.10.4
Ignoring nokogiri-1.8.2 because its extensions are not built.  Try: gem pristine nokogiri --version 1.8.2
Ignoring nokogiri-1.8.1 because its extensions are not built.  Try: gem pristine nokogiri --version 1.8.1
Ignoring pg-1.1.4 because its extensions are not built.  Try: gem pristine pg --version 1.1.4
Ignoring pg-0.21.0 because its extensions are not built.  Try: gem pristine pg --version 0.21.0
Ignoring puma-3.12.1 because its extensions are not built.  Try: gem pristine puma --version 3.12.1
Ignoring puma-3.11.4 because its extensions are not built.  Try: gem pristine puma --version 3.11.4
Ignoring puma-3.11.3 because its extensions are not built.  Try: gem pristine puma --version 3.11.3
Ignoring puma-3.11.2 because its extensions are not built.  Try: gem pristine puma --version 3.11.2
Ignoring rmagick-2.16.0 because its extensions are not built.  Try: gem pristine rmagick --version 2.16.0
Ignoring ruby-debug-ide-0.7.0 because its extensions are not built.  Try: gem pristine ruby-debug-ide --version 0.7.0
Ignoring ruby-filemagic-0.7.2 because its extensions are not built.  Try: gem pristine ruby-filemagic --version 0.7.2
Ignoring sqlite3-1.4.1 because its extensions are not built.  Try: gem pristine sqlite3 --version 1.4.1
Ignoring sqlite3-1.3.13 because its extensions are not built.  Try: gem pristine sqlite3 --version 1.3.13
Ignoring websocket-driver-0.6.5 because its extensions are not built.  Try: gem pristine websocket-driver --version 0.6.5
Ignoring websocket-native-1.0.0 because its extensions are not built.  Try: gem pristine websocket-native --version 1.0.0
/Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/pure/parser.rb:135:in `convert_encoding': nil is not like a string (TypeError)
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/pure/parser.rb:78:in `initialize'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/common.rb:156:in `new'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/common.rb:156:in `parse'
    from -e:1:in `<main>'

看来我的 $json 没有被翻译。将其插入上述命令的正确方法是什么?

答案1

您使用单引号。在 bash 中,单引号内的变量不会被扩展。

ruby -rjson -e "j = JSON.parse($json); puts j[\"Instances\"][0][\"ImageId\"]"

相关内容