From d86d2f8528c67af722250dff18da741f811d3a48 Mon Sep 17 00:00:00 2001 From: chunyang-wen Date: Tue, 22 Apr 2014 13:14:48 +0800 Subject: [PATCH] update the script with more commands and a frustrating msg. --- Ruby/sim-linux-shell.rb | 108 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/Ruby/sim-linux-shell.rb b/Ruby/sim-linux-shell.rb index cb965ec..e8f2436 100644 --- a/Ruby/sim-linux-shell.rb +++ b/Ruby/sim-linux-shell.rb @@ -3,6 +3,9 @@ # usage # in irb, include(Path_of_file+File::SEPATATOR+"sim-linux-shell.rb") +# As ruby 1.9 DO NOT support multi-parameters with space between +# parameters and function name, there should be double quotes to wrap +# paramters and a comma between paramters. # This ruby scipt is to simulate linux shell commands in irb environment. # When we are in irb, we cannot access shell commands. So this file provides @@ -14,11 +17,15 @@ # rm # mv # cp +# mkdir +# rmdir +# pwd # @author: chunyang wen # @email: chunyang.wen@gmail.com # @TODO: Add options command +# @TODO: Find a way to solve multi-paramters on one-line problem. # @history: # in case for name pollution, wrapped all them methods in @@ -61,8 +68,109 @@ def self.ls(*args) } end + def self.cd(entry) + if !File.directory?(entry) then + puts "#{entry} not a dirtory" + else + Dir.chdir(entry) + end + end + + def self.pwd + puts Dir.pwd + end + + def self.cp(src, des) + + # make sure to overide + puts "Overrided when exists?(y/n)" + input = $stdin.gets.chomp!.downcase + if input == 'y' then + is_override = true + else + is_override = false + end + back_up(src,des,is_override) + end + + def self.rename(old_name, new_name) + if File.exists?(old_name) then + File.rename(old_name, new_name) + else + puts "Not a valid name" + end + end + + def self.mv(src, des) + if File.directory?(src) then + self.back_up(src,des,true) + Dir.rmdir(src) + else + self.back_up(src,des,true) + File.delete(src) + end + end + + def self.mkdir(dir) + Dir.mkdir(dir) + end + + def self.rmdir(dir) + Dir.rmdir(dir) + end + + def self.touch(filename, pes="w+") + File.new(filename,pes) + end private + # backup files + def self.backup_file(source, destination, is_over) + + out_file_path = destination + File::SEPARATOR + File.basename(source) + + if File.exist?(out_file_path) && !is_over + raise "File #{File.basename(out_file_path)} alread exists", caller + end + p out_file_path + + begin + out_file = File.new(out_file_path, "w+") + rescue + raise "Can not create file" + File.basename(source),caller + end + open(source) { |f| + p "copying" + while record = f.read(256) + out_file.write(record) + end + } + end + + def self.back_up(source, destination, is_over) + if File.directory?(source) then + # back up directory + destination = destination + File::SEPARATOR + File.basename(source) + p "Creating DIR: " + destination + + if !Dir.exist?(destination) then + # directory does not exist + Dir.mkdir(destination) + end + Dir.foreach(source) {|entry| + if entry != "." && entry != ".." then + p "DIR NAME: " + File.dirname(entry) + p "BASE NAME: " + File.basename(entry) + basename = File.basename(entry) + back_up(source+File::SEPARATOR+entry,destination, is_over) + end + } + else + # back up files + backup_file(source, destination, is_over) + end + end + def self.ls_assist(entry) is_dir = File.directory?(entry) ? "d":"-" r_able = File.readable?(entry) ? "r":"-"