Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing shell script commands in Ruby script

Tags:

linux

bash

ruby

I have a very basic question. I'm running a Ruby script to access the contents of a directory in Linux. The directory is passed through the command line when the ruby script is executed.

My question is how would I use the command line argument in the command for ruby?

I have it set like such:

usrDirectory = ARGV[0]
lsCmd = `ls -l`

I need to use something like ls -l usrDirectory. Could I just insert it into the command like is?

like image 321
Brad Kaufman Avatar asked Mar 06 '26 19:03

Brad Kaufman


1 Answers

The above is right, and if you want to have ls output to standard out, this makes it a little cleaner:

system("ls", "-l", dir)

This will make Ruby print the output to your standard out instead of putting the output in the variable as the above does.

like image 120
Linuxios Avatar answered Mar 09 '26 12:03

Linuxios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!