Forming Sanitary Shell Commands or System Calls in Ruby

Forming sanitary shell commands or system calls in Ruby

It doesn't look like you need a shell for what you're doing. See the documentation for system here: http://ruby-doc.org/core/classes/Kernel.html#M001441

You should use the second form of system. Your example above would become:

system 'usermod', '-p', @options['shadow'], @options['username']

A nicer (IMO) way to write this is:

system *%W(usermod -p #{@options['shadow']} #{@options['username']})

The arguments this way are passed directly into the execve call, so you don't have to worry about sneaky shell tricks.

Getting output of system() calls in Ruby

I'd like to expand & clarify chaos's answer a bit.

If you surround your command with backticks, then you don't need to (explicitly) call system() at all. The backticks execute the command and return the output as a string. You can then assign the value to a variable like so:

output = `ls`
p output

or

printf output # escapes newline chars

How do I sanitize shell command and get the output in Ruby?

You want IO::popen instead of system. You can still pass an array of strings to invoke the command without a shell, and you can read from the resulting IO object.

If you want to read stderr too, then use the open3 module instead of IO.

Background tasks and shell commands from a Rails app

There are several ways, all are Rubyisms.

system('cmd','arg1',...)

Or the very Unixy

val = `ls -l`

The latter would run the command and return it's output into the variable val.

In Ruby, how to choose whether a symbol or string to be used in a given scenario?

a = :foo
b = :foo

a and b refer to the same object in memory (same identity)

a.object_id # => 898908
b.object_id # => 898908

Strings behave differently

a = 'foo'
b = 'foo'

a.object_id # => 70127643805220
b.object_id # => 70127643805200

So, you use strings to store data and perform manipulations on data (replace characters or whatnot) and you use symbols to name things (keys in a hash or something). Also see this answer for more use cases for symbol.

Network Programming: to maintain sockets or not?

There is a trade off between the cost of keeping the connections open and the cost of creating those connections.

Creating connections costs time and bandwidth. You have to do the 3-way TCP handshake, launch a new server thread, ...

Keeping connections open costs mainly memory and connections. Network connections are a resource limited by the OS. If you have too many clients connected, you might run out of available connections. It will cost memory as you will have one thread open for each connection, with its associated state.

The right balanced will be different based on the usage you expect. If you have a lot of clients connecting for short period of times, it's probably gonna be more efficient to close the connections. If you have few clients connecting for long period of time, you should probably keep the connections open ...

Do a global file search

Find recurses into subdirectories, so just start at the root path, it'll go everywhere:

Find.find('/') do |path|
# look for your filename
end

run git from within ruby script

Call a shell command in your ruby script. There are lots of ways to execute a shell command in ruby. Backticks are one of them. Look at this Calling shell commands from Ruby for more.

`git commit -am "Committing from ruby script"`


Related Topics



Leave a reply



Submit