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
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)
24 changes: 24 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
import os

args = sys.argv[1:]

show_all = False
path = "."

for arg in args:

if arg == "-a":
show_all = True

elif arg != "-1":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any difference between when -1 is used and when not?

path = arg

files = os.listdir(path)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of this task is to see if you can handle the listing yourself.


for file in files:

if not show_all and file.startswith("."):
continue

print(file)
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)
Loading