PDA

View Full Version : Changing the bash shell directory via Ruby in Terminal




toddburch
Feb 20, 2007, 07:22 PM
I admit it - I'm stumped.

I'm trying to write a ruby script to change the directory from whatever directory I'm currently in (in Terminal) to a certain directory - we'll call it ~/Documents/my/very/deep/directory.

It appears that when I run a ruby script under Terminal, the ruby script is running in a subshell, and I can see that the working directory is changing after issung Dir.chdir(full_dir_name). However, I want to change the Terminal shell directory, just as I would using the long, drawn out, "cd ~/Documents/my/very/deep/directory" command.

I've tried issuing the "cd .........." command in backquotes, and it does work, but again, it's the subshell that changes, and not the top level shell of the Terminal window. When the ruby script completes - there I am - back in whatever directory I started in.

How to change the top level shell directory from ruby?

Thanks, Todd



Eraserhead
Feb 20, 2007, 07:30 PM
you can join the ruby commands together with a ; to seperate them, if YOU want to access deep directories in terminal, edit the .profile file in your home directory (it's hidden) to include the line alias someName='cd ~/Documents/my/very/deep/directory'

toddburch
Feb 20, 2007, 07:35 PM
you can join the ruby commands together with a ; to seperate them,...

Sorry, not sure what ruby commands you are referring to...?

...if YOU want to access deep directories in terminal, edit the .profile file in your home directory (it's hidden) to include the line alias someName='cd ~/Documents/my/very/deep/directory'

That might be a good workaround, but I don't think it's what I want to do. And, from what I have observed, and what I explained (tried to explain), when then "cd alias" command would be issued from Ruby, would it still not only apply to the subshell?

ChrisA
Feb 20, 2007, 07:51 PM
Try this, Write the Ruby script so that it prints out the directory full path name to standard out. Then from bash start your script like this

cd `my_rubby_script`

Notice the back quotes.

I assume your ruby script is doing something much more interesting than simply printing a constant. Your problem is that there really is nothing a child proces can do to effect it's parent.

toddburch
Feb 20, 2007, 08:00 PM
OK, here's the ruby script.


# ruby script to set the directory.

puts "The current directory is #{Dir.getwd}" ;

x = Dir.chdir("/Users/toddburch/Documents/myjava") ;
y = Dir.getwd() ; #

puts "Result of chdir=#{x}, Current Directory is now #{y}" ;

z = system("cd /Users/toddburch/Documents/myjava") ;
puts "Result of 'cd' command is #{z}" ;

Here is the terminal output:

todd-burchs-computer:/ toddburch$ ruby setjavadir.rb
The current directory is /
Result of chdir=0, Current Directory is now /Users/toddburch/Documents/myjava
Result of 'cd' command is true

Issuing ls right after the script runs, I'm still in the root directory.

Eraserhead
Feb 21, 2007, 07:01 AM
Sorry, not sure what ruby commands you are referring to...?
The command line ones you want to run. Sorry I don't understand ruby itself, I'm just trying to explain the general principal

for example in Applescript

do shell script "cd Library"
do shell script "pwd"
produces "/" but

do shell script "cd Library;pwd"
produces "/Library"

pilotError
Feb 21, 2007, 07:41 AM
I really don't know if this is applicable here, but most everything runs in a separate shell.

If you wanted to run a shell script in the current shell, typically you place a "." on the command line. This is on redhat, but is pretty common across most unix variants.

ie.

a small bash script:


#!/bin/bash

cd /opt/sybase


Output (Take note of how the second command was executed with a leading "."):

[thisuser ~]$ ./changedir
[thisuser ~]$ pwd
/home/thisuser
[thisuser ~]$ . ./changedir
[thisuser sybase]$ pwd
/opt/sybase
[thisuser sybase]$[/B]

Hope this helps.

In your case, you should be able to do the bang line that tells ruby to run this script without actually having to call ruby (ie. in the above script it was #!/bin/bash )

You should be able to do

. $(ruby setjavadir.rb)

I don't have a Mac here at work or I'd try it for you...

Mike...