Enter and Exit subprocess from Shell Script

I am trying to enter irb from a shell script, do something, and then return to the shell script.

How can I do this?

#!/bin/bash
#example_app

# Some shell instructions here
irb
four = 2 + 2
exit
echo "This text is not reached and the process hangs open"

NOTE: I should not try to load irb to enter static code, I agree with you guys. I didn't think about what I was doing. I was interacting with an AWS library and I tried to carry that same philosophy into automating this without thinking twice about it.

Answers


You can pass a here document to irb:

irb <<SCRIPT
  four = 2 + 2
  puts four
SCRIPT

However, I don't think that will accomplish what you are trying to do. Here is the output:

Switch to inspect mode.
  four = 2 + 2
4
  puts four
4
nil

It is similar to bash -x. Why not simply use plain old ruby for this?

ruby <<SCRIPT
  four = 2 + 2
  puts four
SCRIPT
4

There is no need to spawn an interactive shell if you aren't going to interact with it.


I think you want the answer explained from fork and exec in bash essentially:

function_to_fork() { irb }

function_to_fork &

that may not work if you want some interaction there to influence the parent process or for the parent process to wait.

My comment below didn't format very well:

With the following program I was able to achieve:

#!/bin/bash
# irbb.sh
irb
echo "here"

$ ./irbb.sh 
>> four = 2 + 2
=> 4
>> exit
here

Need Your Help

Converting Enum for use in a class

c# enums

Below I have a piece of code I have been working on for quite sometime:

C++ Server is terminating if the front end Tomcat is killed. Error "Received untrapped signal [13] - SIGPIPE"

c++ signals

I am facing a problem in my C++ server program. The request XML comes from the front end (Java) and the back end server (C++) process the request and returns the reply XML.