0

How to call function in shell script from ruby (preferably using open3)

#!/bin/sh
# A simple script with a function...

function add()
{
  echo "1"
}

Ruby Script that does not work--

#!/apollo/bin/env ruby
# -*- ruby -*-   
require 'open3'
Open3.capture3('.\something.sh', 'add')
Mohammad Adnan
  • 6,527
  • 6
  • 29
  • 47

2 Answers2

1

In the first place, you should have a valid bash function declaration.

Assuming something.sh was corrected to:

#!/bin/sh
# A simple script with a function...

bar () {
  echo "1"
}

You have to load it’s content into current shell and execute a function in it:

Open3.capture3(". ./something.sh && bar")
#β‡’ ["1\n", "", #<Process::Status: pid 17113 exit 0>]
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

For interest of others posting the answer. Basically, I end up doing work around like -

#!/apollo/bin/env ruby
# -*- ruby -*-   
require 'open3'

Open3.capture3(
    'bash',
    '-c',
    "source something.sh && add")

Basically Open3 (due to Ruby) fires every commands in different sessions and hence source of an script and method call should be done in single call.

Mohammad Adnan
  • 6,527
  • 6
  • 29
  • 47