Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+")
parser.add_argument("-n", action="store_true")
parser.add_argument("-b", action="store_true")

args = parser.parse_args()

line_number = 1

for file in args.files:
try:
f = open(file, "r")

for line in f:
if args.b and line.strip() != "":
print(f" {line_number}", line, end="")
line_number += 1
elif args.n and not args.b:
print(f" {line_number}", line, end="")
line_number += 1
else:
print(line, end="")

f.close()

except:
print("cat:", file, "not found")
34 changes: 34 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import argparse
import os


def get_files(directory):
files = os.listdir(directory)
files.extend([".", ".."])
files.sort(key=str.lower)
return files


def display_files(files, show_all=False, one_per_line=False):
if not show_all:
files = [f for f in files if not f.startswith(".")]

if one_per_line:
for file in files:
print(file)
else:
print(" ".join(files))


parser = argparse.ArgumentParser()
parser.add_argument("directory", nargs="?", default=".")
parser.add_argument("-1", dest="one_per_line", action="store_true", help="list one file per line")
parser.add_argument("-a", dest="show_all", action="store_true", help="include hidden files")

args = parser.parse_args()

try:
files = get_files(args.directory)
display_files(files, show_all=args.show_all, one_per_line=args.one_per_line)
except OSError as e:
print(f"ls: {e}")
64 changes: 64 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+")
parser.add_argument("-l", action="store_true", help="show line count")
parser.add_argument("-w", action="store_true", help="show word count")
parser.add_argument("-c", action="store_true", help="show byte count")

args = parser.parse_args()

show_lines = args.l
show_words = args.w
show_bytes = args.c

if not show_lines and not show_words and not show_bytes:
show_lines = True
show_words = True
show_bytes = True

total_lines = 0
total_words = 0
total_bytes = 0

for file in args.files:
try:
f = open(file, "rb")
content = f.read()
f.close()

line_count = content.count(b"\n")
word_count = len(content.split())
byte_count = len(content)

total_lines += line_count
total_words += word_count
total_bytes += byte_count

output = []

if show_lines:
output.append(str(line_count))
if show_words:
output.append(str(word_count))
if show_bytes:
output.append(str(byte_count))

output.append(file)
print(" ".join(output))

except OSError:
print("wc:", file, "not found")

if len(args.files) > 1:
output = []

if show_lines:
output.append(str(total_lines))
if show_words:
output.append(str(total_words))
if show_bytes:
output.append(str(total_bytes))

output.append("total")
print(" ".join(output))
Loading