![]() |
[working directory to be changed (in terminal) by the Ruby ]
I have a little problem...
I created a script. At some point I should change from whatever directory I'm currently in (in Terminal) to a certain directory from the ruby script. So far I find other solutions that do not satisfy me. The one with which I managed to change directories in the terminal needs another .sh file. When launching the .sh script from the terminal, actually changes the directory (pwd). But if I launch from the ruby script, does not change the output terminal (pwd). Then, I want the working directory to be changed (in terminal) by the Ruby program after it exits. Any suggestions? Thanks -- Posted via http://www.ruby-forum.com/. |
Re: [working directory to be changed (in terminal) by the Ruby ]
-------- Original-Nachricht -------- > Datum: Sun, 28 Sep 2008 10:20:03 +0900 > Von: Max Dev <max.devel@gmail.com> > An: ruby-talk@ruby-lang.org > Betreff: [working directory to be changed (in terminal) by the Ruby ] > I have a little problem... > > I created a script. At some point I should change from whatever > directory I'm currently in (in Terminal) to a certain directory from the > ruby script. > > So far I find other solutions that do not satisfy me. The one with which > I managed to change directories in the terminal needs another .sh file. > > When launching the .sh script from the terminal, actually changes the > directory (pwd). But if I launch from the ruby script, does not change > the output terminal (pwd). > > Then, I want the working directory to be changed (in terminal) by the > Ruby program after it exits. > > Any suggestions? > Thanks > -- > Posted via http://www.ruby-forum.com/. Dear Max, not quite sure whether I understand correctly what you want to do, but you should be able to perform everything you need inside a Ruby script, within which you can change the directory using a Ruby command and then execute any shell commands via the system or backtick commands, and then switch back/again the directory using Ruby. previous_dir=Dir.pwd puts "your first directory was #{previous_dir}" puts "System command pwd says we're here:" p shell_previous_dir=`pwd` Dir.chdir("/usr/local") # in Linux/Mac; use e.g., Dir.chdir("C:/") in Windows changed_dir=Dir.pwd puts "you're now in directory #{changed_dir}" puts "System command pwd says we're here:" p shell_changed_dir=`pwd` Dir.chdir(previous_dir) puts "you changed your directory again, back to #{Dir.pwd}" puts "System command pwd says we're here:" p shell_changed_again_dir=`pwd` Best regards, Axel -- GMX Kostenlose Spiele: Einfach online spielen und Spaß haben mit Pastry Passion! http://games.entertainment.gmx.net/d...puzzle/6169196 |
Re: [working directory to be changed (in terminal) by the Ruby ]
2008/9/28 Max Dev <max.devel@gmail.com>:
> I have a little problem... > > I created a script. At some point I should change from whatever > directory I'm currently in (in Terminal) to a certain directory from the > ruby script. > > So far I find other solutions that do not satisfy me. The one with which > I managed to change directories in the terminal needs another .sh file. > > When launching the .sh script from the terminal, actually changes the > directory (pwd). But if I launch from the ruby script, does not change > the output terminal (pwd). > > Then, I want the working directory to be changed (in terminal) by the > Ruby program after it exits. You do not explicitly state this but from what you write I assume you are on a Unix like OS. And you want to change a parent process's idea of CWD from a child process. In order to be able to do this your child process needs to communicate the new CWD to the parent in some form. One option is to write a shell function which executes the child process and changes directory according to that child process's output like (/bin/echo serves as the child process here): # bourne (again) shell mycd(){ cd "`/bin/echo '/'`" } You can as well use temporary files or other means to communicate this but you almost certainly need to do something in the parent shell. Kind regards robert -- remember.guy do |as, often| as.you_can - without end |
Re: [working directory to be changed (in terminal) by the Ru
Max Dev wrote:
> I created a script. At some point I should change from whatever > directory I'm currently in (in Terminal) to a certain directory from the > ruby script. > > So far I find other solutions that do not satisfy me. The one with which > I managed to change directories in the terminal needs another .sh file. > > When launching the .sh script from the terminal, actually changes the > directory (pwd). But if I launch from the ruby script, does not change > the output terminal (pwd). > > Then, I want the working directory to be changed (in terminal) by the > Ruby program after it exits. > > Any suggestions? If this is under Unix, then basically it can't happen. When you start a ruby interpreter, you are spawning (fork+exec) a new process. This new process has its own environment, including its own concept of "current directory". Any change to current directory in this new process does not, indeed cannot, affect the current directory in the parent. This isn't a Ruby limitation, but of any spawned process, including another shell. See what happens when you do this: cd /tmp pwd sh # start a new shell cd /usr pwd exit # exit the new shell, returning control to parent pwd # parent working directory unchanged The 'cd' command only works because it is a shell builtin, not an external command like /bin/cd. Another test: create a file "chdir.sh" containing the following: #!/bin/sh cd /tmp Make it executable (chmod +x chdir.sh) and run it (./chdir.sh). Nothing happens; the directory change was in the subshell. However if you run it as ". chdir.sh" (note the space after the dot) it is read as a series of instructions into the current shell. The first line is ignored as a comment, and the second is run as if you had typed it into the current shell, so it invokes the cd builtin as before. I'm not sure if you can get a shell to accept commands from its child on a pipe. I tried . <(ruby -e 'puts "cd /tmp"') but that doesn't seem to work, even though cat <(ruby -e 'puts "cd /tmp"') works as expected. -- Posted via http://www.ruby-forum.com/. |
Re: [working directory to be changed (in terminal) by the Ru
Axel, Robert, Brian.
Thank you all for the replies. Actually I started from an approach like that of Robert and stopped to something like that exhibited by Brian. Initially I have created an external file .sh (via ruby) with the bash code inside, then called from the ruby script, i.e.: system ("'.' ./cd_#{app_name}.sh;pwd") So far I can not imagine if there is a "hack" to succeed. -- Posted via http://www.ruby-forum.com/. |
Re: [working directory to be changed (in terminal) by the Ru
> Initially I have created an external file .sh (via ruby) with the bash
> code inside, then called from the ruby script, i.e.: > > system ("'.' ./cd_#{app_name}.sh;pwd") FYI, that is the same as: system("/bin/sh","-c",". ./cd_#{app_name}.sh;pwd") In other words you're starting a new shell as a new process, and inside that new subshell you're changing directory. Ruby waits for this new shell to terminate before continuing. Therefore, if you're starting Ruby itself from a shell, then you have the following processes: initial shell -------> ruby --------> new shell The 'cd' command within the new shell affects only its own current directory, not the current directory of either Ruby or the initial shell. It's really a question of what you're trying to achieve. If you want to run a shell script with a particular current directory, then it's easy (just cd to the right directory and then start the script; you can use Dir.chdir within Ruby to do this). But Ruby can only affect the current directory of new processes which it starts, not the parent process ("initial shell" in the above diagram) which started Ruby. Only the initial shell itself can change its own current directory. -- Posted via http://www.ruby-forum.com/. |
Re: [working directory to be changed (in terminal) by the Ru
>Only the initial shell itself can change its own
> current directory. This is the point :) -- Posted via http://www.ruby-forum.com/. |
Re: [working directory to be changed (in terminal) by the Ruby ]
On Sep 27, 2008, at 7:20 PM, Max Dev wrote: > Then, I want the working directory to be changed (in terminal) by the > Ruby program after it exits. cfp:~ > cat a.rb Dir.chdir '/tmp' exec 'bash' cfp:~ > ruby a.rb cfp:/private/tmp > pwd /private/tmp a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama |
Re:
Ara Howard wrote:
> On Sep 27, 2008, at 7:20 PM, Max Dev wrote: > >> Then, I want the working directory to be changed (in terminal) by the >> Ruby program after it exits. > > > > cfp:~ > cat a.rb > Dir.chdir '/tmp' > exec 'bash' > > > cfp:~ > ruby a.rb > > > cfp:/private/tmp > pwd > /private/tmp Sneaky. That's not the original shell; type "exit" and you get back to the original shell, with its original working directory. And if you keep doing this, you'll eat up more and more process slots and RAM. But maybe it's sufficient for the OP. -- Posted via http://www.ruby-forum.com/. |
Re:
On Sep 29, 2008, at 12:53 PM, Brian Candler wrote: > > Sneaky. That's not the original shell; type "exit" and you get back to > the original shell, with its original working directory. And if you > keep > doing this, you'll eat up more and more process slots and RAM. But > maybe > it's sufficient for the OP. yep - all correct. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama |
| All times are GMT. The time now is 03:22 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.