简体   繁体   中英

Launch interactive Bash shell in Ruby script, with initial command

I'm working on an interactive Ruby script, which build and packages resources. In the middle of the process, I'd like to drop into an interactive shell, but pre- cd 'd into a specific working directory , as well as with an explanatory message (CTRL-D to continue). The interactive bash + given initial command is what's problematic.

Per the answer for doing something like this in Bash, given at https://stackoverflow.com/a/36152028 , I've tried

system '/bin/bash', '--init-file', '<(echo "cd ~/src/devops; pwd")'

However, bash runs interactively but completely ignores the '<(echo "cd ~/src/devops; pwd")' section .

Interestingly system '/bin/bash', '--init-file complains if no argument is given, but literally anything runs bash, but with no initial command.

*Note that (--rcfile instead of --init-file) has the same effect.

Change the working directory of the Ruby script first, so that bash inherits the correct working directory.

curr_dir = Dir.pwd
Dir.chdir("#{Dir.home}/src/devops")
system "/bin/bash"
Dir.chdir(curr_dir)   # Restore the original working directory if desired

Oh, this is probably far better (you can probably guess how little familiarity I have with Ruby):

system("/bin/bash", :chdir=>"#{Dir.home}/src/devops")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM