如何使用Ruby进行Shell脚本编写?

时间:2020-03-06 15:04:09  来源:igfitidea点击:

我要执行一些简单的Shell脚本编写任务

例如:从与某些正则表达式匹配的文件列表中选择工作目录中的文件。

我知道我可以使用标准的bash和grep来做这种事情,但是我很高兴能够破解快速脚本,这些脚本可以在Windows和linux中工作,而无需记住大量的命令行程序和标志等。

我试图解决这个问题,但最终对应该在哪里获取信息(例如对当前目录的引用)感到困惑

那么问题来了,我需要了解Ruby库的哪些部分来编写ruby shell脚本?

解决方案

"如何编写红宝石"超出了SO的范围。

但是要将这些ruby脚本转换为可执行脚本,请将其作为ruby脚本的第一行:

#!/path/to/ruby

然后使文件可执行:

chmod a+x myscript.rb

而你走了。

假设我们编写了script.rb脚本。放:

#!/usr/bin/env ruby

作为第一行并执行chmod + x script.rb

将其放在script.rb的开头

#!/usr/bin/env ruby

然后将其标记为可执行文件:

chmod +x script.rb

正如其他人已经说过的那样,第一行应该是

#!/usr/bin/env ruby

而且我们还必须使其可执行:(在shell中)

chmod +x test.rb

然后遵循红宝石代码。如果我们打开一个文件

File.open("file", "r") do |io|
    # do something with io
end

该文件会在我们在shell中使用pwd获取的当前目​​录中打开。

脚本的路径也很容易获得。使用$ 0,我们将获得shell的第一个参数,这是脚本的相对路径。绝对路径可以这样确定:

#!/usr/bin/env ruby
require 'pathname'
p Pathname.new(
#! /usr/bin/env ruby
# Extension of this script does not matter as long
# as it is executable (chmod +x)
puts File.expand_path(__FILE__)
).realpath()

对于文件系统操作,我几乎总是使用路径名。这是许多其他与文件系统相关的类的包装。也有用:Dir,File ...

在ruby中,常量" FILE"将始终为我们提供正在运行的脚本的路径。

在Linux上,/ usr / bin / env是朋友:

# This script filename must end with .rb
puts File.expand_path(__FILE__)

在Windows上,它取决于.rb文件是否与ruby相关联。
如果他们是:

@ruby %~dp0\my_script.rb

如果不是,则必须显式调用ruby,我使用一个中间.cmd文件:

my_script.cmd:

puts File.expand_path(__FILE__)

my_script.rb:

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

去给自己一个Ruby日常脚本的副本。它具有许多有用的技巧,可指导我们如何做自己想做的事情。

默认情况下,我们已经可以访问Dir和File,它们本身非常有用。

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)

从stdlib也有用的是FileUtils

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

哪个很好

这也可能会有所帮助:http://rush.heroku.com/

我没用过,但是看起来很酷

从站点:

#!/usr/bin/env ruby
puts "I was passed: "
ARGV.each do |value|
  puts value
end

这是其他答案中缺少的重要内容:命令行参数通过ARGV(全局)数组向Ruby Shell脚本公开。

因此,如果我们有一个名为my_shell_script的脚本:

chmod u+x my_shell_script

...使其可执行(正如其他人提到的那样):

> ./my_shell_script one two three four five

并这样称呼它:

I was passed: 
one
two
three
four
five

你会得到这个:

./my_shell_script *

I was passed: 
a_file_in_the_current_directory
another_file    
my_shell_script
the_last_file

这些参数可以很好地与文件名扩展一起使用:

puts `find . | grep -i lib`

大多数操作仅在UNIX(Linux,Mac OS X)上有效,但是我们可以在Windows中执行类似(尽管不太方便)的操作。

这里有很多好的建议,所以我想再增加一点。

  • 反引号(或者反引号)使我们可以更轻松地执行一些脚本编写工作。考虑
out = `git status 2>&1`
  • 如果我们在获取反引号的输出时遇到问题,则这些内容将变为标准err而不是标准输出。使用此建议
blah = 'lib'
`touch #{blah}`
  • 反引号进行字符串插值:
#!/usr/bin/env ruby
`env | pbcopy` 
cmd =  %Q@tell app "Terminal" to do script "$(paste_env)"@
puts cmd

`osascript -e "${cmd}"`
  • 我们也可以在Ruby中使用管道。它是我博客的链接,但它链接回到这里,所以还可以:)关于这个主题,可能还有更高级的内容。
  • 正如其他人指出的那样,如果我们想变得认真一点,那就会有Rush:不仅是作为Shell的替代品(对我来说有点太可笑了),而且还可以作为我们在Shell脚本和程序中使用的库。

在Mac上,请在Ruby中使用Applescript以获得更多功能。这是我的" shell_here"脚本:

##代码##