Type:
Class

Shell implements an idiomatic Ruby interface for common UNIX shell commands.

It provides users the ability to execute commands with filters and pipes, like sh/csh by using native facilities of Ruby.

Examples

Temp file creation

In this example we will create three tmpFile's in three different folders under the /tmp directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sh = Shell.cd("/tmp") # Change to the /tmp directory
sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
# make the 'shell-test-1' directory if it doesn't already exist
sh.cd("shell-test-1") # Change to the /tmp/shell-test-1 directory
for dir in ["dir1", "dir3", "dir5"]
  if !sh.exists?(dir)
    sh.mkdir dir # make dir if it doesnt' already exist
    sh.cd(dir) do
      # change to the `dir` directory
      f = sh.open("tmpFile", "w") # open a new file in write mode
      f.print "TEST\n"            # write to the file
      f.close                     # close the file handler
    end
    print sh.pwd                  # output the process working directory
  end
end

Temp file creationg with self

This example is identical to the first, except we're using Shell::CommandProcessor#transact.

Shell::CommandProcessor#transact executes the given block against self, in this case sh; our Shell object. Within the block we can substitute sh.cd to cd, because the scope within the block uses sh already.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sh = Shell.cd("/tmp")
sh.transact do
  mkdir "shell-test-1" unless exists?("shell-test-1")
  cd("shell-test-1")
  for dir in ["dir1", "dir3", "dir5"]
    if !exists?(dir)
      mkdir dir
      cd(dir) do
        f = open("tmpFile", "w")
        f.print "TEST\n"
        f.close
      end
      print pwd
    end
  end
end

Pipe /etc/printcap into a file

In this example we will read the operating system file /etc/printcap, generated by cupsd, and then output it to a new file relative to the pwd of sh.

1
2
3
4
5
sh = Shell.new
sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
(sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12"
sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2"
(sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"
alias_map
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::CommandProcessor

alias_map() Class Public methods Returns a list of aliased commands

2025-01-10 15:47:30
active_job?
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::ProcessController

active_job?(job) Instance Public methods

2025-01-10 15:47:30
foreach
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::CommandProcessor

foreach(path, record_separator) â Enumeratorforeach(path, record_separator) { block } Instance Public methods

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::Void

new(sh, *opts) Class Public methods

2025-01-10 15:47:30
def_system_command
  • References/Ruby on Rails/Ruby/Classes/Shell

def_system_command(command, path = command) Class Public methods Convenience

2025-01-10 15:47:30
default_record_separator=
  • References/Ruby on Rails/Ruby/Classes/Shell

default_record_separator=(rs) Class Public methods

2025-01-10 15:47:30
rmdir
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::CommandProcessor

rmdir(path) Instance Public methods Same as

2025-01-10 15:47:30
install_system_commands
  • References/Ruby on Rails/Ruby/Classes/Shell

install_system_commands(pre = "sys_") Class Public methods Convenience method

2025-01-10 15:47:30
active?
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::BuiltInCommand

active?() Instance Public methods

2025-01-10 15:47:30
active_process_controllers
  • References/Ruby on Rails/Ruby/Classes/Shell/Shell::ProcessController

active_process_controllers() Class Public methods

2025-01-10 15:47:30