How to pass URL in ruby command line? - ruby-on-rails

I have written a ruby program in which it takes the input as url and it displays the components in it.
I passed the URL inside the ruby file but now I want to pass it through ruby command line like ruby filename.rb url.
How to pass an argument inside the code.

The ARGV constant holds an array which contains all of the arguments passed in on the command line. So in your script, you could get the url passed in via ARGV[0].

ARGV is an array which contains all the arguments passed into your script via the command line.
ARGV.each do|arg|
puts "Argument: #{arg}"
end

Just use ARGV constant , you can puts ARGV.to_s in log to see what`s in it , to helps you understand how to use it .

Related

Properly escape json to command line call in Ruby or Rails

I'd like to call a command line utility from a ruby script like so:
#!/env/ruby
json = {"key" => "value that \"has\" quotes"}.to_json
`aws events put-targets --cli-input-json #{json}`
Such that the resultant call should look like:
aws events put-targets --cli-input-json "{\"key\": \"value that \"has\" quotes\"}"
However, the result in this string interpolation results in a clean looking json structure without the quotes escaped and so results in error at the command line. Eg.
aws events put-targets --cli-input-json {"key": "value that \"has\" quotes"}
I need all the quotes properly escaped so that a string to the command line can be parsed as proper json.
I've tried doing string manipulation to manually escape quotes with things like:
json.gsub(/\"/,'\"')
But that doesn't work either.
This seems like it's harder than it should be. How can I render a properly escaped json string to the command line call?
I do have a rails environment that I can run this through if there are utilities that ActiveSupport provides that would facilitate this.
In this case it is simpler and more effective to make a system call without a shell. If you use the multi-argument form of Kernel#system to invoke the external command directly:
system('aws', 'events', 'puts-targets', '--cli-input-json', json)
No shell, no quoting or escaping problems with json.
If you need to do more complicated things (such as capture output or errors), look into the various methods in Open3.
If you absolutely must go through a shell there's always Shellwords.shellescape. But really, when you use the shell, you're:
Building a command line with a bunch of Ruby string operations.
Invoking a shell.
Letting the shell parse the command line (i.e. undo everything you did in (1)).
Letting the shell invoke the command with your arguments.
Why not go straight to (4) yourself?
Thanks to #mu-is-too-short, I came across Shellwords which is a neat utility. This didn't solve the problem however, but led me to search for "shell escape json" which in turn led me to: Best way to escape and unescape strings in Ruby?
So:
json = {"key" => "value that \"has\" quotes"}.to_json.dump
Properly gets the escaped string that bash will understand. Tada.
UPDATE: Don't use this in production code. You're better off following #mu-is-too-short's advice in the comments or using a higher level library.

lua custom terminal not having command outputs

Im trying to make a terminal but im stuck on one thing. In the doer program command do. I want docom to be the output of of the loadstring. input = io.read() its a lua terminal inside my program but nothing displays any output. Here is the code that is relevant:
docom = loadstring(input)
print(docom)
How do i make the output display? Because currently its like this:
welcome to the terminal!
loaded
do
do:
print("hello")
function: 0x809b60
do:
The third and fifth line are user inputs. how do i fix this so it shows the hello string instead of the function name. i want this to be able to manage it as i have everything else in the same lua script. please help.
You probably want print(docom()).
loadstring compile a script into a function. That's what you see function: 0x809b60.
loadstring does not run the function. Hence the call docom().
You may want to add error handling by checking whether docom is nil and by calling docom via pcall.

How to read a user input in a text filed in ruby on rails and pass the value to a shell script command

I am very new to ruby on rails and I have a very simple question.
I am able to read the user input from a text field in ruby on rails
#user = params[:user]
I have to pass this value as an argument to a shell script
I tried a number of things, but nothing works
run_command "./file.sh #{#user}"
and
%x(./file.sh #{#user})
The script receives "{user="
Any help is appreciated!
First, make sure you escape any parameters you pass to command line. You may be easily attacked via command line injection this way. Try this instead:
system "/path/to/file.sh #{#user.shellescape}"
You may also want to try exec, depending on how you want to track output. Please see http://ruby-doc.org/core-2.3.0/Kernel.html for more details on both

How to read parameter from Paramterized Build using ruby?

I'm using Paramterized Build, and know I can get paramter by name in bash like $name.
But I want to write script with ruby, how can I get the paramter?
Thanks
you should be able to get them using env variables
http://ruby.about.com/od/rubyfeatures/a/envvar.htm
To send parameters to your ruby script, you may use the following way:
call the script and pass the parameters from the command line (for Jenkins, you may use execute shell script option)
$ irb ruby_script.rb parameter1 parameter2
and in ruby_script.rb, you can get these parameters by using builtin ruby syntax, ARGV:
parameter_one = ARGV[0] // refer to the first paramter
parameter_one = ARGV[1] // refer to the second paramter
You may refer to this issue

How can I pass the arguments to the lua file through the lua CLI

I have a LUA CLI which takes in the lua command,
Something like this
(lua)> #
Now here inorder to execute the lua file I run the command
(lua)> # dofile("a.lua")
I want a command which will execute the file and also pass a argument to it.
Now here I want to pass a argument to the "a.lua" file which will take this argument and call one more lua file, and this 2nd lua file is called according to the argument, So, I need to parse this argument.
Please, can somebody tell me about the parsing commands that will be used in a.lua. I mean what are the functions to be used to parse it.
Please, can somebody tell me how to pass a argument to this file "a.lua".
Now here inorder to execute the lua file I run the command
This is generally not how you execute Lua files. Usually, if you have some Lua script, you execute it with this command: lua a.lua. You don't type lua and then use the interface there to execute it.
Using a proper command line to execute the script, you can pass string parameters to the file: lua a.lua someParam "Param with spaces". The a.lua script can then fetch these parameters using the standard Lua ... mechanic:
local params = {...}
params[1] -- first parameter, if any.
params[2] -- second parameter, if any.
#params -- number of parameters.
However, if you insist on trying to execute this using your method of invoking the interpreter (with lua) and typing commands into it one by one, then you can do this:
> GlobalVariable = assert(loadfile(`a.lua`))
> GlobalVariable(--[[Insert parameters here]])
However, if you don't want to do it in two steps, with the intermediate global variable, you can do it in one:
> assert(loadfile(`a.lua`))(--[[Insert parameters here]])

Resources