Calling shell script from ruby script
Damana and I previously posted about our experiences with different Ruby LDAP solutions.
Having settled on Ruby-LDAP (although having read Ola and Steven’s comments we will now look at ruby-net-ldap) we then needed to put together the setup, installation and teardown into a ruby script file.
A quick bit of Googling revealed that we could use the Kernel.exec method to do this.
For example, you could put the following in a ruby script file and it would execute and show you the current directory listing:
exec "ls"
The problem with using Kernel.exec, which we became aware of after reading Jay’s post, is that we lose control of the current process - i.e. the script will exit after running 'exec' and won’t process any other commands that follow it in the file.
Luckily for us there is another method called Kernel.system which allows us to execute a command in a sub shell, and therefore continue processing other commands that follow it.
We were able to use this method for making calls to the make script to install Ruby-LDAP:
@extconf = "ruby extconf.rb"
system @extconf
system "make"
system "make install"
There is one more option we can use if we need to collect the results called %x[…]. We didn’t need to collect the results so we have gone with 'Kernel.system' for the time being.
Jay covers the options in more detail on his post for those that need more information than I have presented.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.