45721

run command in parent shell from ruby

Question:

I'm trying to change the directory of the shell I start the ruby script form via the ruby script itself...

My point is to build a little program to manage favorites directories and easily change among them.

Here's what I did

#!/usr/bin/ruby Dir.chdir("/Users/luca/mydir") and than tried executing it in many ways... my_script (this doesn't change the directory) . my_script (this is interpreted as bash) . $(ruby my_script) (this is interpreted as bash too!)

any idea?

Answer1:

Cannot be done. Child processes cannot modify their parents environment (including the current working directory of the parent). The . (also known as source) trick only works with shell scripts because you are telling the shell to run that code in the current process (rather than spawning a subprocess to run it). Just for fun try putting exit in a file you run this way (spoiler: you will get logged out).

If you wish to have the illusion of this working you need to create shell functions that call your Ruby script and have the shell function do the actual cd. Since the functions run in the current process, they can change the directory. For instance, given this ruby script (named temp.rb):

#!/usr/bin/ruby print "/tmp";

You could write this BASH function (in, say, you ~/.profile):

function gotmp { cd $(~/bin/temp.rb) }

And then you could say gotmp at the commandline and have the directory be changed.

Answer2:

#!/usr/bin/env ruby `../your_script`

Like this?

Or start your script in the directory you want it to do something.

Maybe I don't get your question. Provide some more details.

Recommend

  • How to test a Python script with an input file filled with testcases?
  • Trying jQuery wrapAll seems to wrap the content twice
  • Access subdomain session from main domain
  • make m4 see macro when macro ends with same character as string following macro
  • Android NotificationListenerService: how to know if user clicked on the notification (opening the re
  • How do you increment a count while using the sleep function?
  • How to optimize multithreaded program for use in LSF?
  • Find Background running apps in iphone
  • Difference between a changeset and a patch?
  • How to wait for a thread to finish execution in C#?
  • Node: one core, many processes
  • How atomic are mongoengine's operations
  • Running exec() command with dev>null & doesn't work, but dev>null does
  • PHP Queue System with Codeigniter. HOW? [closed]
  • User matching with current data
  • writing file in heroku filesystem and reading it with web app
  • Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
  • GetFinalPathNameByHandle() without prepended '\\\\?\\'
  • Gem not installing package
  • What is the difference between Socket.Send and Stream.Write? (in relation to tcp ip connections)
  • Unable to get CAP_CHOWN and CAP_DAC_OVERRIDE working for regular user
  • How to start server for Selenium grid Java Maven setup
  • How to run bash commands like “npm install” on complie
  • 'include' of functions in groovy scripts
  • Copying rows in a database when rows have children
  • Passing information to server-side function in a Google Docs Add On
  • Can XOR be expressed using SKI combinators?
  • Why doesnt this Java loop in a thread work?
  • How can I get the full list of running processes on a Mac from a python app
  • Get specific string
  • Code in Job's Script Block after Start-Process Does not Execute
  • How do I open a C file with a relative path?
  • Xamarin Forms - UWP Fonts
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • javascript inside java/jsp code
  • WOWZA + RTMP + HTML5 Playback?
  • Error creating VM instance in Google Compute Engine
  • How does Linux kernel interrupt the application?