Bash
如何將我的 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 沒有被翻譯。將其插入上述命令的正確方法是什麼?
您使用單引號。在 bash 中,單引號內的變數不會被擴展。
ruby -rjson -e "j = JSON.parse($json); puts j[\"Instances\"][0][\"ImageId\"]"