1

I am trying to pass values from a ruby hash to a bash script.. What would be the best way to do it? The size and key/value pairs in ruby are always different..

so if I want something like..

 hsh = {"key1"=>"value1", "key2"=>"value2"}
 %x[sh script.sh #{hsh}]

What would I need on the bash side?

EDIT: If the hash on ruby side won't work, I can use another data structure, what I care about is that the size of the container (hash/ array) will be always different..

EDIT2: By "care about the size" I mean that the hash/ array will have different number of elements every time.. . sorry for unclarity

2
  • If you only care about the size, why not pass the size instead of the data? Commented Jun 25, 2012 at 4:54
  • sorry, I mean that size can be variable (the hash can have 5, 7, 9, 20 etc.. key/value pairs..) Commented Jun 25, 2012 at 4:56

1 Answer 1

1

You can print the values from the Ruby script and read them in the Bash script. You will need Bash 4 in order to use associative arrays or you can iterate over the values and act on them as they're read. Why not do what you want to do in Bash within the Ruby script instead?

# works with Bash 3 or 4
while read -r key value
do
    echo "$key $value"   # act on the keys and values
done < <(ruby-script)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I will try that. I am using bash to construct somewhat complex arguments for command-line tool. But maybe you're right should try that in Ruby as well. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.