[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting the original file path on argument

Daniel Mantilla

3/4/2009 10:12:00 PM

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
--
Posted via http://www.ruby-....

5 Answers

Jeff Schwab

3/4/2009 10:33:00 PM

0

Daniel Mantilla 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
>
> 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.

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 Klemme

3/4/2009 10:40:00 PM

0

On 04.03.2009 23:11, Daniel Mantilla 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 Mantilla

3/5/2009 2:36:00 PM

0

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.

--
Posted via http://www.ruby-....

Robert Klemme

3/5/2009 3:27:00 PM

0

On 05.03.2009 15:35, Daniel Mantilla wrote:
> 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.

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 Mantilla

3/5/2009 10:56:00 PM

0

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
--
Posted via http://www.ruby-....