Wil*_*ill 163 ruby shell scripting
我有一些简单的shell脚本编写任务,我想做
例如:从与某些正则表达式匹配的文件列表中选择工作目录中的文件.
我知道我可以使用标准的bash和grep做这种事情,但我很高兴能够破解可以在windows和linux中运行的快速脚本,而不必记住一堆命令行程序和标志等.
我试图实现这一目标,但最终对我应该获取信息的地方感到困惑,例如对当前目录的引用
所以问题是编写ruby shell脚本需要知道Ruby库的哪些部分?
web*_*mat 146
默认情况下,您已经可以访问Dir和File,它们本身非常有用.
Dir['*.rb'] #basic globs
Dir['**/*.rb'] #** == any depth of directory, including current dir.
#=> array of relative names
File.expand_path('~/file.txt') #=> "/User/mat/file.txt"
File.dirname('dir/file.txt') #=> 'dir'
File.basename('dir/file.txt') #=> 'file.txt'
File.join('a', 'bunch', 'of', 'strings') #=> 'a/bunch/of/strings'
__FILE__ #=> the name of the current file
Run Code Online (Sandbox Code Playgroud)
从stdlib中也很有用的是FileUtils
require 'fileutils' #I know, no underscore is not ruby-like
include FileUtils
# Gives you access (without prepending by 'FileUtils.') to
cd(dir, options)
cd(dir, options) {|dir| .... }
pwd()
mkdir(dir, options)
mkdir(list, options)
mkdir_p(dir, options)
mkdir_p(list, options)
rmdir(dir, options)
rmdir(list, options)
ln(old, new, options)
ln(list, destdir, options)
ln_s(old, new, options)
ln_s(list, destdir, options)
ln_sf(src, dest, options)
cp(src, dest, options)
cp(list, dir, options)
cp_r(src, dest, options)
cp_r(list, dir, options)
mv(src, dest, options)
mv(list, dir, options)
rm(list, options)
rm_r(list, options)
rm_rf(list, options)
install(src, dest, mode = <src's>, options)
chmod(mode, list, options)
chmod_R(mode, list, options)
chown(user, group, list, options)
chown_R(user, group, list, options)
touch(list, options)
Run Code Online (Sandbox Code Playgroud)
这很不错
Geo*_*lly 109
正如其他人已经说过的那样,你的第一行应该是
#!/usr/bin/env ruby
Run Code Online (Sandbox Code Playgroud)
而且你还必须使它可执行:(在shell中)
chmod +x test.rb
Run Code Online (Sandbox Code Playgroud)
然后遵循ruby代码.如果你打开一个文件
File.open("file", "r") do |io|
# do something with io
end
Run Code Online (Sandbox Code Playgroud)
该文件pwd在shell中获取的当前目录中打开.
脚本的路径也很简单.随$0你获得shell的第一个参数,它是脚本的相对路径.绝对路径可以这样确定:
#!/usr/bin/env ruby
require 'pathname'
p Pathname.new($0).realpath()
Run Code Online (Sandbox Code Playgroud)
对于文件系统操作,我几乎总是使用Pathname.这是许多其他文件系统相关类的包装器.也很有用:Dir,File ......
Cra*_*ker 66
以下是其他答案中缺少的重要内容:命令行参数通过ARGV(全局)数组公开给您的Ruby shell脚本.
所以,如果你有一个名为my_shell_script的脚本:
#!/usr/bin/env ruby
puts "I was passed: "
ARGV.each do |value|
puts value
end
Run Code Online (Sandbox Code Playgroud)
...让它可执行(正如其他人提到的):
chmod u+x my_shell_script
Run Code Online (Sandbox Code Playgroud)
并称之为:
> ./my_shell_script one two three four five
Run Code Online (Sandbox Code Playgroud)
你会得到这个:
I was passed:
one
two
three
four
five
Run Code Online (Sandbox Code Playgroud)
参数与文件名扩展很好地配合:
./my_shell_script *
I was passed:
a_file_in_the_current_directory
another_file
my_shell_script
the_last_file
Run Code Online (Sandbox Code Playgroud)
其中大部分仅适用于UNIX(Linux,Mac OS X),但您可以在Windows中执行类似(虽然不太方便)的操作.
Dan*_*ark 32
这里有很多好建议,所以我想多补充一点.
反引号(或反向标记)可以让你更轻松地编写脚本.考虑
puts `find . | grep -i lib`
Run Code Online (Sandbox Code Playgroud)如果你在获得反引号的输出时遇到问题,那么这些东西将是标准错误而不是标准输出.使用这个建议
out = `git status 2>&1`
Run Code Online (Sandbox Code Playgroud)反引号进行字符串插值:
blah = 'lib'
`touch #{blah}`
Run Code Online (Sandbox Code Playgroud)你也可以在Ruby里面管道.它是我的博客的链接,但它链接回来,所以没关系:)这个主题可能有更多高级的东西.
正如其他人所指出的那样,如果你想认真对待Rush:不仅仅是作为shell替换(对我来说有点太过分了),而且还作为一个用于shell脚本和程序的库.
在Mac上,使用Ruby内部的Applescript获得更多功能.这是我的shell_here脚本:
#!/usr/bin/env ruby
`env | pbcopy`
cmd = %Q@tell app "Terminal" to do script "$(paste_env)"@
puts cmd
`osascript -e "${cmd}"`
Run Code Online (Sandbox Code Playgroud)
Aar*_*nni 21
使用Ruby获取Everyday Scripting的副本.它有很多关于如何做你想要做的事情的有用提示.
cra*_*igp 12
这可能也有帮助:http://rush.heroku.com/
我没有用太多,但看起来很酷
从网站:
rush是使用纯Ruby语法的unix shell(bash,zsh等)的替代品.浏览文件,查找和终止进程,复制文件 - 您在shell中执行的所有操作,现在都在Ruby中
Vas*_*sil 11
假设您编写script.rb脚本.放:
#!/usr/bin/env ruby
Run Code Online (Sandbox Code Playgroud)
作为第一行并做一个 chmod +x script.rb
“我如何编写 ruby”有点超出了 SO 的范围。
但是要将这些 ruby 脚本转换为可执行脚本,请将其作为 ruby 脚本的第一行:
#!/path/to/ruby
Run Code Online (Sandbox Code Playgroud)
然后使文件可执行:
chmod a+x myscript.rb
Run Code Online (Sandbox Code Playgroud)
你走吧。
当使用 Ruby 作为 shell 脚本时,上面的答案很有趣并且非常有用。对我来说,我不使用 Ruby 作为我的日常语言,我更喜欢将 ruby 仅用作流程控制,并且仍然使用 bash 来完成任务。
一些辅助函数可用于测试执行结果
#!/usr/bin/env ruby
module ShellHelper
def test(command)
`#{command} 2> /dev/null`
$?.success?
end
def execute(command, raise_on_error = true)
result = `#{command}`
raise "execute command failed\n" if (not $?.success?) and raise_on_error
return $?.success?
end
def print_exit(message)
print "#{message}\n"
exit
end
module_function :execute, :print_exit, :test
end
Run Code Online (Sandbox Code Playgroud)
使用 helper,ruby 脚本可以像 bash 一样:
#!/usr/bin/env ruby
require './shell_helper'
include ShellHelper
print_exit "config already exists" if test "ls config"
things.each do |thing|
next if not test "ls #{thing}/config"
execute "cp -fr #{thing}/config_template config/#{thing}"
end
Run Code Online (Sandbox Code Playgroud)