Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

toddburch

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
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

macrumors G5
Jan 5, 2006
12,907
2,152
Redondo Beach, California
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

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
OK, here's the ruby script.

Code:
# 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:
Code:
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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
Code:
do shell script "cd Library"
do shell script "pwd"
produces "/" but
Code:
do shell script "cd Library;pwd"
produces "/Library"
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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:

Code:
#!/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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.