Issue
Noob question, but how do I add a command in a ruby script for the terminal?
for example ruby tool.rb
and I want to add a command -c
that will invoke a method that prints blah blah, and then execute it via the terminal in kali linux, so it would look something like this ruby tool.rb -c
. Would anyone know how to do this and know what this is called?
Solution
That's called running a ruby script/program from the command line and passing "flags" like -c
are passed to the script as command line arguments and are an array of string values separated by spaces normally.
Here's your very simple script:
tool.rb
#!/usr/bin/env ruby
if ARGV[0] == '-c'
puts 'blah blah'
end
You can run this from the command line exactly as you requested.
ruby tool.rb -c
Updated
If you need additional arguments or want to pass something else to your flag you can do as I mentioned ARGV
is an array constructed from string passed after the name of your ruby script so:
#!/usr/bin/env ruby
if ARGV[0] == '-c'
puts "blah blah #{ARGV[1]}" # array items are called by index
end
So if you do this:
ruby tool.rb -c foo
You will get:
blah blah foo
Answered By - lacostenycoder Answer Checked By - Mildred Charles (WPSolving Admin)