Reputation: 107
I have a gem installed that displays a ascii image of a train moving across the terminal screen when someone types "ls". I also have a file called runner.rb.
If it's possible, how can I input the "ls" command to the terminal from within the ruby file?
Upvotes: 2
Views: 969
Reputation: 18833
If you want to execute the command as if you were sitting in the shell (so its output displays, etc.), use system
.
> system('ls')
Look At All
My Cool Files
=> true
If you want to capture the output for use, use backticks.
> files = `ls`
=> "Look\nAt\nAll\nMy\nCool\nFiles\n"
You can interpolate in backticks if you want to (and obviously in the string you pass to system
).
Upvotes: 2
Reputation: 3477
You can also use ( ` ) character to execute Linux commands from ruby file.
e.g.
`ls` ## OR `whoami` etc.
Upvotes: 2
Reputation: 777
cmd = "ls > somefile.txt"
system( cmd )
or even just simply
system( "ls" )
Thus, you can use system.
Upvotes: 3