0

I would like to pass to a given Ruby script, a file as parameter. This file contains just a number (ID).

The command to run the Ruby scripts looks something like:

test export 123456 -o ./path/to/export -x

The number 123456 rappresents the parameter that i want to pass via txt/dat file from GitLab.

I tried:

test export "$(< /home/file.dat)" -o ./path/to/export -x

And also:

test export "`cat file.dat`" -o ./path/to/export -x

But i always get the same error:

cat: file.dat: No such file or directory

The very interesting point is that if i run cat before the other command, the content of the file is there (so the file is found). If i run it "nested" inside the Ruby command, it won't be found.

Any ideas how can i solve this?

Thank you very much

t30_9
  • 347
  • 4
  • 17
  • The command doesn't make sense because [test](https://www.gnu.org/software/bash/manual/bash.html#index-test) is a shell built-in command that doesn't accept arguments like that. If you have a program called `test` you should rename it. – pjh Apr 12 '22 at 12:49
  • To get the contents of a file called `file.dat` that is in your home directory use `"$(< ~/file.dat)"`. `/home/file.dat` shouldn't be a valid path. Plain `file.dat` won't work unless the current working directory is the directory that contains it. (See [What exactly is current working directory?](https://stackoverflow.com/q/45591428/4154375).) – pjh Apr 12 '22 at 12:52
  • @pjh Thank you for your answers. Yes, `test` here is just an example. The command is actually the name of the ruby script i'm using (that i didn't implemented, but i'm just using). I'll try to follow your advice – t30_9 Apr 12 '22 at 13:03
  • @t30_9 double quotes are not required in `"\`cat file.dat\`"` and `"$(< /home/file.dat)"` case. – Chandan Apr 13 '22 at 08:11
  • Hi guys. I've tried those things. The "$(< ~/file.dat)" is not working since is going back to root, and not in the folder i want to look for (home). Quotes are not required yes, but it makes no difference with or without. I added an `ls` and i can clearly see the file listed here, and the name is matching. I've no clue why the `cat file.dat` is not working then.. – t30_9 Apr 13 '22 at 12:12

1 Answers1

0

I'm not sure if this is what you're looking for, but you could pass the name of the file via command line and read the content of the file within the ruby script:

id = nil
# ARGV holds all the parameters passed to the script
File.readlines(ARGV[0]).each do |line|
  id = line # id here will be set to the value contained in the file you passed a parameter
end
  • Thank you for your answer. For now i'm trying to keep my solution as simple and as clean as possible since i've to integrate this in Gitlab-ci. So no scripts (if possible), and also i've no access to the Ruby code, since i'm using this software as client. I just want to call this Ruby and passing, instead of an integer fix id, a file containing an id. In the back, i've already a script which is updating this file dynamically. So the goal is just to have a dynamic way to pass this ID to the ruby program. – t30_9 Apr 13 '22 at 12:09