How do I use Ruby for shell scripting?

前端 未结 13 1854
粉色の甜心
粉色の甜心 2020-12-22 15:26

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

相关标签:
13条回答
  • 2020-12-22 16:05

    By default, you already have access to Dir and File, which are pretty useful by themselves.

    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
    

    Also useful from the stdlib is 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)
    

    Which is pretty nice

    0 讨论(0)
  • 2020-12-22 16:06

    "How do I write ruby" is a little beyond the scope of SO.

    But to turn these ruby scripts into executable scripts, put this as the first line of your ruby script:

    #!/path/to/ruby
    

    Then make the file executable:

    chmod a+x myscript.rb
    

    and away you go.

    0 讨论(0)
  • 2020-12-22 16:07

    The above answer are interesting and very helpful when using Ruby as shell script. For me, I does not use Ruby as my daily language and I prefer to use ruby as flow control only and still use bash to do the tasks.

    Some helper function can be used for testing execution result

    #!/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
    

    With helper, the ruby script could be bash alike:

    #!/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
    
    0 讨论(0)
  • 2020-12-22 16:08

    Go get yourself a copy of Everyday Scripting with Ruby. It has plenty of useful tips on how to do the types of things your are wanting to do.

    0 讨论(0)
  • 2020-12-22 16:11

    let's say you write your script.rb script. put:

    #!/usr/bin/env ruby
    

    as the first line and do a chmod +x script.rb

    0 讨论(0)
  • 2020-12-22 16:11

    The answer by webmat is perfect. I just want to point you to a addition. If you have to deal a lot with command line parameters for your scripts, you should use optparse. It is simple and helps you tremendously.

    0 讨论(0)
提交回复
热议问题