Changing the bash shell directory via Ruby in Terminal

Discussion in 'Mac Programming' started by toddburch, Feb 20, 2007.

  1. toddburch macrumors 6502a

    Dec 4, 2006
    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
  2. Eraserhead macrumors G4


    Nov 3, 2005
    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'
  3. toddburch thread starter macrumors 6502a

    Dec 4, 2006
    Katy, Texas
    Sorry, not sure what ruby commands you are referring to...?

    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?
  4. ChrisA macrumors G4

    Jan 5, 2006
    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.
  5. toddburch thread starter macrumors 6502a

    Dec 4, 2006
    Katy, Texas
    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.
  6. Eraserhead macrumors G4


    Nov 3, 2005
    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"
  7. pilotError macrumors 68020


    Apr 12, 2006
    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.


    a small bash script:

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

    [thisuser ~]$ ./changedir
    [thisuser ~]$ pwd
    [thisuser ~]$ . ./changedir
    [thisuser sybase]$ pwd
    [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...


Share This Page