I have some simple shell scripting tasks that I want to do
For example: Selecting a file in the working directory from a list of the files matching some regular e
As the others have said already, your first line should be
#!/usr/bin/env ruby
And you also have to make it executable: (in the shell)
chmod +x test.rb
Then follows the ruby code. If you open a file
File.open("file", "r") do |io|
# do something with io
end
the file is opened in the current directory you'd get with pwd
in the shell.
The path to your script is also simple to get. With $0
you get the first argument of the shell, which is the relative path to your script. The absolute path can be determined like that:
#!/usr/bin/env ruby
require 'pathname'
p Pathname.new($0).realpath()
For file system operations I almost always use Pathname. This is a wrapper for many of the other file system related classes. Also useful: Dir, File...
There's a lot of good advice here, so I wanted to add a tiny bit more.
Backticks (or back-ticks) let you do some scripting stuff a lot easier. Consider
puts `find . | grep -i lib`
If you run into problems with getting the output of backticks, the stuff is going to standard err instead of standard out. Use this advice
out = `git status 2>&1`
Backticks do string interpolation:
blah = 'lib'
`touch #{blah}`
You can pipe inside Ruby, too. It's a link to my blog, but it links back here so it's okay :) There are probably more advanced things out there on this topic.
As other people noted, if you want to get serious there is Rush: not just as a shell replacement (which is a bit too zany for me) but also as a library for your use in shell scripts and programs.
On Mac, Use Applescript inside Ruby for more power. Here's my shell_here
script:
#!/usr/bin/env ruby
`env | pbcopy`
cmd = %Q@tell app "Terminal" to do script "$(paste_env)"@
puts cmd
`osascript -e "${cmd}"`
When you want to write more complex ruby scripts, these tools may help:
For example:
thor(a scripting framework)
gli(git like interface)
methadone(for creating simple tools)
They give you a quick start to write your own scripts, especially 'command line app'.
Place this at the beginning of your script.rb
#!/usr/bin/env ruby
Then mark it as executable:
chmod +x script.rb
In ruby, the constant __FILE__
will always give you the path of the script you're running.
On Linux, /usr/bin/env
is your friend:
#! /usr/bin/env ruby
# Extension of this script does not matter as long
# as it is executable (chmod +x)
puts File.expand_path(__FILE__)
On Windows it depends whether or not .rb files are associated with ruby. If they are:
# This script filename must end with .rb
puts File.expand_path(__FILE__)
If they are not, you have to explicitly invoke ruby on them, I use a intermediate .cmd file:
my_script.cmd:
@ruby %~dp0\my_script.rb
my_script.rb:
puts File.expand_path(__FILE__)
This might also be helpful: http://rush.heroku.com/
I haven't used it much, but looks pretty cool
From the site:
rush is a replacement for the unix shell (bash, zsh, etc) which uses pure Ruby syntax. Grep through files, find and kill processes, copy files - everything you do in the shell, now in Ruby