Skip to content
39 changes: 39 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys

args = sys.argv[1:]

option = "none"
paths = []

# parse args
for arg in args:
if arg == "-n":
option = "n"
elif arg == "-b":
option = "b"
else:
paths.append(arg)

for path in paths:
with open(path, "r") as file:
lines = file.readlines()

line_number = 1

for line in lines:

line = line.rstrip("\n")

if option == "n":
print(f"{line_number} {line}")
line_number += 1

elif option == "b":
if line != "":
print(f"{line_number} {line}")
line_number += 1
else:
print("")

else:
print(line)
19 changes: 19 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--all", action="store_true")
parser.add_argument("-1", dest="one_per_line", action="store_true")
parser.add_argument("path", nargs="?", default=".")

args = parser.parse_args()

files = sorted(os.listdir(args.path))

files = [f for f in files if args.all or not f.startswith(".")]

if args.one_per_line:
for f in files:
print(f)
else:
print(" ".join(files))
33 changes: 33 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys

args = sys.argv[1:]

option = "all"
paths = []

for arg in args:
if arg == "-l":
option = "l"
elif arg == "-w":
option = "w"
elif arg == "-c":
option = "c"
else:
paths.append(arg)

for path in paths:
with open(path, "r") as file:
content = file.read()

lines = len(content.split("\n"))
words = len(content.split(" "))
chars = len(content)

if option == "l":
print(lines, path)
elif option == "w":
print(words, path)
elif option == "c":
print(chars, path)
else:
print(lines, words, chars, path)