Forum: Ruby Getting the original file path on argument

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel M. (Guest)
on 2009-03-05 00:13
Hi all, I have a little ruby script that accepts a file name as a
parameter. Given the company standards, the script has to be deployed on
a specific "bin" folder. It has a cryptic name so a more "human" symlink
was created on the same folder, let's call it "run_process". I ended up
with something like this:

some_path/bin/run_process
some_path/bin/cryptic_ruby_script.rb

The "some_path/bin" appears on the path of the user but every time he
runs the script he has to provide the expanded file name otherwise the
script will try to find the file on some_path/bin folder.

Is there a way to avoid this by just executing "run_process my_file" ?

Thanks in advance for any help.

Daniel
Jeff S. (Guest)
on 2009-03-05 00:35
(Received via mailing list)
Daniel M. wrote:
> runs the script he has to provide the expanded file name otherwise the
> script will try to find the file on some_path/bin folder.

Do you mean that Ruby's current working directory is some_path/bin, even
when the program is run elsewhere?

> Is there a way to avoid this by just executing "run_process my_file" ?

Does cryptic_ruby_script.rb have execute permission and a shebang
(#!/usr/bin/env ruby)?

You could try replacing the symlink with a shell script:

cat > some_path/bin/run_process
#!/bin/sh
ruby /full/path/to/some_path/bin/cryptic_ruby_script.rb
^D
chmod +x some_path/bin/run_process
Robert K. (Guest)
on 2009-03-05 00:40
(Received via mailing list)
On 04.03.2009 23:11, Daniel M. wrote:
> Hi all, I have a little ruby script that accepts a file name as a
> parameter. Given the company standards, the script has to be deployed on
> a specific "bin" folder. It has a cryptic name so a more "human" symlink
> was created on the same folder, let's call it "run_process". I ended up
> with something like this:
>
> some_path/bin/run_process
> some_path/bin/cryptic_ruby_script.rb

So you have a symlink "run_process" which points to
"cryptic_ruby_script.rb" in the same directory?

> The "some_path/bin" appears on the path of the user but every time he
> runs the script he has to provide the expanded file name otherwise the
> script will try to find the file on some_path/bin folder.
>
> Is there a way to avoid this by just executing "run_process my_file" ?

I am not sure I understand your problem.  Since the symlink and the real
file reside in the same directory all relative paths must work properly.

If you want to just use the file name, then you need to add a directory
to get the full path.  I have no idea how that directory is defined,
i.e. is it relative to the binary, is it a system fixed directory etc.?
  Please fill in the missing information.

In the meantime you can experiment with File.readlink for reading
symlinks.

Cheers

  robert
Daniel M. (Guest)
on 2009-03-05 16:36
Thanks guys, I think I didn't explain myself very clear.

The script runs fine, the problem is with the command line parameter.
For example, if I try to process my_file on /home/dan :

cd /home/dan
run_process my_file

The script tries to find my_file on some_path/bin instead of /home/dan.
I have to provide a full path for the script to use my_file:

run_process /home/dan/my_file

I also tried with

run_process ./my_file

Same results.

What do I have to do on my script for "run_process my_file" to run fine?

I hope things are more clear now.

Thanks.
Robert K. (Guest)
on 2009-03-05 17:30
(Received via mailing list)
On 05.03.2009 15:35, Daniel M. wrote:
>
> I hope things are more clear now.
Yes.  The error must be in your script.  As long as you do not change
the current directory (e.g. via Dir.chdir) the relative path works
properly.  I can think of at least these two solutions:

1. remember the proper location by converting the relativ path to an
absolute path using File.expand_path *before* you chdir.

2. open the file before the chdir.

3. completely get rid of the chdir.

What is best depends on your script which you did not provide.

Cheers

  robert
Daniel M. (Guest)
on 2009-03-06 00:57
Thanks Robert. It's curious, I don't do any chdir on my code. But you
have given me a clue of why my code is not working properly. I will send
an update tomorrow if it works.

Daniel
This topic is locked and can not be replied to.