Problem using System command in ruby

P

Pranjal Jain

HI All

I am using WIndows gem "winclicker" to run windows properties.

The code is as follows:
"
require 'watir/winClicker'
system ("D:")
system ("cd ruby")
system ("cd Temp")
system ("ruby trial.rb")
"

I am trying to run file trial.rb via this script. but it is giving me
the error as follows :

The system cannot find the path specified.
ruby: No such file or directory -- trial.rb(LoadError)

PLease tell me wht is wrong in the code.

Thanks in advance :)
 
D

Damjan Rems

Pranjal said:
HI All

I am using WIndows gem "winclicker" to run windows properties.

The code is as follows:
"
require 'watir/winClicker'
system ("D:")
system ("cd ruby")
system ("cd Temp")
system ("ruby trial.rb")
"

Maybe:
system ("cd \ruby")


by
TheR
 
M

Mikael Høilund

require 'watir/winClicker'
system ("D:")
system ("cd ruby")
system ("cd Temp")
system ("ruby trial.rb")

Whenever you run the system method, you're making a new subshell. This =20=

means that when you run it, you're essentially put back to square one; =20=

all the environment variables and information about the current shell =20=

is reset =97 including the current directory. Example:

puts "Shell 1:"
system 'pwd'

puts
puts "Shell 2:"
system 'cd Users; pwd'

puts
puts "Shell 3:"
system 'pwd'

This runs on my Mac computer. The pwd command displays the current =20
directory. This is my output:

Shell 1:
/

Shell 2:
/Users

Shell 3:
/

See, in the first shell it's in the directory "/" (the rough =20
equivalent of C:\ on Windows).
In the second shell, I change to the directory "Users", and display =20
the directory; it's in "/Users".
Now, when I run the third, it's returned to "/" because it has =20
launched a new shell, not reused the old one. Any change to the =20
environment of one system method call isn't going to affect subsequent =20=

calls.

To do what you want to do (untested, as I haven't got access to a =20
Windows machine at the moment), you'd have to do something like:

system "D:; cd ruby\Temp; ruby trial.rb"

This keeps all the calls in the same shell, which is a necessity for =20
doing what you want. If the ruby script you're calling isn't referring =20=

to any files with a relative path (the directory from which it's =20
called doesn't matter), you could just do

system "ruby D:\ruby\Temp"=
 
S

Simon Krahnke

* Pranjal Jain said:
require 'watir/winClicker'
system ("D:")
system ("cd ruby")
system ("cd Temp")
system ("ruby trial.rb")

That won't work because each system command runs in its own shell
process. A 'cd' has no effect on later commands.

mfg, simon .... l
 
S

Sebastian Hungerecker

Pranjal said:
require 'watir/winClicker'
system ("D:")
system ("cd ruby")
system ("cd Temp")
system ("ruby trial.rb")

Dir.chdir("d:/ruby/Temp") {system("ruby trial.rb")}

HTH,
Sebastian
 
P

princeofnigeria

[Note: parts of this message were removed to make it a legal post.]

Have you verified that the trial.rb file is located in teh saem directory
that you are running the "system ("ruby trial.rb")" from?

system("ruby trial.rb") should be run from the same directory as the
trial.rb script. For instance, if you attempt to run this script from the
drive root of c:\ and the trial.rb script resides in
c:\mycoolrubyscripts\testscripts\

Let me know if thats teh problem, if not I can give it another stab.
 
P

Pranjal Jain

HI there
Yes it is in the same directory
Have you verified that the trial.rb file is located in teh saem
directory
that you are running the "system ("ruby trial.rb")" from?

system("ruby trial.rb") should be run from the same directory as the
trial.rb script. For instance, if you attempt to run this script from
the
drive root of c:\ and the trial.rb script resides in
c:\mycoolrubyscripts\testscripts\

Let me know if thats teh problem, if not I can give it another stab.

On Sun, May 4, 2008 at 11:29 PM, Pranjal Jain
<[email protected]>
 
A

Albert Schlef

Whenever you run the system method, you're making a new subshell.
This means that when you run it, you're essentially put back to square
one; all the environment variables and information about the current
shell is reset — including the current directory.

That's true for posix systems, that the 'current directory' is
independent for each process.

But, AFAIK, this isn't correct for Windows. Has this changed with newer
versions of Windows?
 
P

Phillip Gawlowski

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Albert Schlef wrote:
|
| That's true for posix systems, that the 'current directory' is
| independent for each process.
|
| But, AFAIK, this isn't correct for Windows. Has this changed with newer
| versions of Windows?

Windows XP SP2 exhibits that behavior. And this is more something on
Ruby's side of things, rather than the OS (that can do little, after
all, if Ruby works its magic, no?), especially if this behavior is
consistent across platforms (which I'm too lazy to check, but somebody
with *NIX currently booted can do that easily ;).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ There's never enough time to do all the nothing you want.
-- Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgfGCoACgkQbtAgaoJTgL85tgCfe8NE4Kv7ciqca1SaHVfNu0Ac
NYUAoIT1/E+k4KgUXenjHF8Kl20hIRvT
=9KGO
-----END PGP SIGNATURE-----
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top