Arian Faurtosh Arian Faurtosh - 4 months ago 9
Ruby Question

When using wildcard, only one file is ran in rails runner

I am using rails runner, and I'd like to run multiple scripts in a folder.

Here is what my files structure looks like...

$ ls db/test/

de_test.rb
fr_test.rb
ja_test.rb
....


de_test.rb

puts "DE"


fr_test.rb

puts "FR"


ja_test.rb

puts "JA"


Now if I run:

$ bundle exec script/rails runner -e production db/test/de_test.rb
DE
$ bundle exec script/rails runner -e production db/test/fr_test.rb
FR
$ bundle exec script/rails runner -e production db/test/ja_test.rb
JA
$ bundle exec script/rails runner -e production db/test/*test.rb
DE


Why doesn't
db/test/*test.rb
run all the files? And what can I do to fix this.

This is on RHEL.

Answer

Bash globs are just shorthand for writing multiple filename arguments at a time:

This:

bundle exec script/rails runner -e production db/test/*test.rb

will be entirely identical to if you had written:

bundle exec script/rails runner -e production db/test/de_test.rb db/test/fr_test.rb db/test/ja_test.rb

The documentation is lacking, but most likely this will just run de_test.rb with the other files as additional command line parameter. This is why you only see DE.

What you wanted to do was instead to run one command per file, i.e.:

bundle exec script/rails runner -e production db/test/de_test.rb
bundle exec script/rails runner -e production db/test/fr_test.rb
bundle exec script/rails runner -e production db/test/ja_test.rb

You can do this with a for loop:

for file in db/test/*test.rb;
do
    bundle exec script/rails runner -e production "$file";
done

(you can put it all on one line if you prefer)

Comments