百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rubyshell

> 编程语言
开源

使用 Ruby 创建 Shell 脚本的方法!!

191 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使用 Ruby 创建 Shell 脚本的方法!!

The Rubyist way to write shell scripts

Installation
·
Usage
·
Wiki
·
Examples
·
Contributing
ruby
  cd "/log" do
    ls.each_line do |line|
      puts cat(line)
    end
  end

Yes, that's valid Ruby! ls and cat are just shell commands, but RubyShell makes them behave like Ruby methods.

Installation

bash
bundle add rubyshell

Or install directly:

bash
gem install rubyshell

Why RubyShell?

The Problem

Ever written something like this?

bash
# Bash: Find large files modified in the last 7 days, show top 10 with human sizes
find . -type f -mtime -7 -exec ls -lh {} \; 2>/dev/null | \
  awk '{print $5, $9}' | \
  sort -hr | \
  head -10

Or tried to do error handling in bash?

bash
# Bash: Hope nothing goes wrong...
output=$(some_command 2>&1) || echo "failed somehow"

The Solution

ruby
sh do
  # Ruby + Shell: Same task, actually readable
  find(".", type: "f", mtime: "-7")
    .lines
    .map { |f| [File.size(f.strip), f.strip] }
    .sort_by(&:first)
    .last(10)
    .each { |size, file| puts "#{size / 1024}KB  #{file}" }
rescue RubyShell::CommandError => e
  puts "Failed: #{e.message}"
  puts "Exit code: #{e.status}"
end

Usage

Basic Commands

ruby
require 'rubyshell'

sh do
  pwd                        # Run any command
  ls("-la")                  # With arguments
  mkdir("project")           # Create directories
  docker("ps", all: true)    # --all flag
  git("status", s: true)     # -s flag
end

# Or chain directly
sh.git("log", oneline: true, n: 5)

Pipelines

ruby
sh do
  # Using chain block
  chain { cat("access.log") | grep("ERROR") | wc("-l") }

  # Using bang pattern
  (cat!("data.csv") | sort! | uniq!).exec
end

Directory Scoping

ruby
sh do
  cd "/var/log" do
    # Commands run here, then return to original dir
    tail("-n", "100", "syslog")
  end
  # Back to original directory
end

Error Handling

ruby
sh do
  begin
    rm("-rf", "important_folder")
  rescue RubyShell::CommandError => e
    puts "Command: #{e.command}"
    puts "Stderr: #{e.stderr}"
    puts "Exit code: #{e.status}"
  end
end

Parallel Execution

Run multiple commands concurrently and get results as they complete:

ruby
sh do
  results = parallel do
    curl("https://api1.example.com")
    curl("https://api2.example.com")
    chain { ls | wc("-l") }
  end

  results.each { |r| puts r }
end

Returns an Enumerator with results in completion order. Errors are captured and returned as values (not raised).

Environment Variables

ruby
# Command-level
sh.npm("start", _env: { NODE_ENV: "production" })

# Block-level
sh(env: { DATABASE_URL: "postgres://localhost/db" }) do
  rake("db:migrate")
end

# Global
RubyShell.env[:API_KEY] = "secret"
RubyShell.config(env: { DEBUG: "true" })

Debug Mode

ruby
# Global
RubyShell.debug = true

# Block scope
RubyShell.debug { sh.ls }

# Per command
sh.git("status", _debug: true)
# Output:
#   Executed: git status
#   Duration: 0.003521s
#   Pid: 12345
#   Exit code: 0
#   Stdout: "On branch main..."

Output Parsers

Parse command output directly into Ruby objects:

ruby
sh.cat("data.json", _parse: :json)   # => Hash
sh.cat("config.yml", _parse: :yaml)  # => Hash
sh.cat("users.csv", _parse: :csv)    # => Array

Chain Options

ruby
# Debug mode for chains
chain(debug: true) { ls | grep("test") }

# Parse chain output
chain(parse: :json) { curl("https://api.example.com") }

Real-World Examples

Git Workflow Automation

ruby
sh do
  # Stash changes, pull, pop, and show what changed
  changes = git("status", porcelain: true).lines

  if changes.any?
    puts "Stashing #{changes.count} changed files..."
    git("stash")
    git("pull", rebase: true)
    git("stash", "pop")
  else
    git("pull", rebase: true)
  end

  # Show recent commits by author
  git("log", oneline: true, n: 100)
    .lines
    .map { |line| `git show -s --format='%an' #{line.split.first}`.strip }
    .tally
    .sort_by { |_, count| -count }
    .first(5)
    .each { |author, count| puts "#{author}: #{count} commits" }
end

Log Analysis

ruby
sh do
  cd "/var/log" do
    # Parse nginx logs: top 10 IPs by request count
    cat("nginx/access.log")
      .lines
      .map { |line| line.split.first }  # Extract IP
      .tally
      .sort_by { |_, count| -count }
      .first(10)
      .each { |ip, count| puts "#{ip.ljust(15)} #{count} requests" }
  end
end

Docker Cleanup

…

Batch File Processing

ruby
sh do
  # Convert all PNGs to WebP, preserving directory structure
  find(".", name: "*.png")
    .lines
    .map(&:strip)
    .each do |png|
      webp = png.sub(/\.png$/, ".webp")
      puts "Converting: #{png}"

      begin
        cwebp("-q", "80", png, o: webp)
        rm(png)
      rescue RubyShell::CommandError => e
        puts "  Failed: #{e.message}"
      end
    end
end

System Health Check

…

Interactive Script with Confirmation

ruby
sh do
  files = find(".", name: "*.tmp", mtime: "+30").lines.map(&:strip)

  if files.empty?
    puts "No old temp files found."
    exit
  end

  puts "Found #{files.count} temp files older than 30 days:"
  files.first(10).each { |f| puts "  #{f}" }
  puts "  ... and #{files.count - 10} more" if files.count > 10

  total_size = files.sum { |f| File.size(f) rescue 0 }
  puts "\nTotal size: #{total_size / 1024 / 1024}MB"

  print "\nDelete all? [y/N] "
  if gets.strip.downcase == 'y'
    files.each { |f| rm(f) }
    puts "Deleted #{files.count} files."
  end
end

Deploy Script

…

Comparison

Task Bash RubyShell
Error handling cmd || echo "fail" rescue CommandError
String manipulation echo $var | sed | awk result.gsub(/.../)
Data structures Arrays only Hashes, objects, classes
Iteration for f in *; do .each, .map, .select
Testing DIY RSpec, Minitest

Documentation

See Wiki for complete documentation including all options and advanced features.

Development

bash
bin/setup          # Install dependencies
rake spec          # Run tests
rake rubocop       # Lint code
bin/console        # Interactive console

Contributing

Bug reports and pull requests are welcome on GitHub. See CONTRIBUTING.md for guidelines and testing patterns.

Sponsors

License

MIT License - see LICENSE.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyautomationhacktoberfestrubyscript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言