-
-
Save flazz/122651 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
class BasicShell | |
attr_reader :env | |
def initialize | |
@env={ } | |
@env["prompt"] = "b> " | |
end | |
def ls p="*" | |
puts Dir[p] | |
end | |
def cd d | |
Dir.chdir d | |
end | |
def pwd | |
puts Dir.pwd | |
end | |
def cat f | |
open(f) { |io| print io.read } | |
end | |
def echo s | |
@env.each { |name,value| s.gsub! /\$#{name}/, value.to_s } | |
puts s | |
end | |
def let name, value | |
@env[name] = value | |
end | |
end | |
sh = BasicShell.new | |
if ARGV[0] | |
open(ARGV[0]) do |io| | |
io.each do |line| | |
sh.instance_eval line | |
end | |
end | |
else | |
puts "welcome" | |
loop do | |
print sh.env["prompt"] | |
input = $stdin.gets | |
break unless input | |
begin | |
sh.instance_eval input | |
rescue => e | |
puts "Bad Command #{e.message}" | |
end | |
end | |
puts "goodbye" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment