Changing the bash shell directory via Ruby in Terminal

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

  1. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #1
    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. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    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. thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    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. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #4
    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. thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    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.
     
  6. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    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"
     
  7. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #7
    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...
     

Share This Page