[working directory to be changed (in terminal) by the Ruby ]

M

Max Dev

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
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sun, 28 Sep 2008 10:20:03 +0900
Von: Max Dev <[email protected]>
An: (e-mail address removed)
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

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
 
R

Robert Klemme

2008/9/28 Max Dev said:
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
 
B

Brian Candler

Max said:
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.
 
M

Max Dev

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.
 
B

Brian Candler

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.
 
B

Brian Candler

Ara said:
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.
 
A

ara.t.howard

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top