Skip to content
Closed
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
import argparse

parser = argparse.ArgumentParser(
prog="cat",
description="Reading the file and print the content",
)

parser.add_argument("-n", help="Number all lines")
parser.add_argument("-b", help="Number only lines with content")
parser.add_argument("paths", nargs="+", help="The file path to process")

args = parser.parse_args()
paths = args.paths

for path in paths:
try:
with open(path, "r") as f:
content = f.read()
lines = content.split("\n")
lines.pop()

line_num = 1

for i in range(len(lines)):
line = lines[i]
if args.n:
print(f"{line_num} {line}")
line_num += 1

elif args.b:
if line != "":
print(f"{line_num} {line}")
line_num += 1
else:
print(line)

except Exception as e:
print(f"Error reading {path}: {e}")
27 changes: 27 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
import argparse
import os

parser = argparse.ArgumentParser(
prog="ls",
description="List all files name",
)

parser.add_argument("-1", dest="one", action="store_true", help="A new line for each file")
parser.add_argument("-a", action="store_true", help="Show hidden files")
parser.add_argument("path", nargs="?", default=".", help="The directory to list")

args = parser.parse_args()
path = args.path

try:
files = os.listdir(path)
if not args.a:
files = [file for file in files if not file.startswith(".")]
if args.one:
print("\n".join(files))
else:
print(" ".join(files))

except Exception as e:
print(f"Error reading {path}: {e}")
54 changes: 54 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import argparse
import os

parser = argparse.ArgumentParser(
prog="wc",
description="Word count",
)

parser.add_argument("-l", action="store_true", help="Count lines")
parser.add_argument("-w", action="store_true", help="Count words")
parser.add_argument("-c", action="store_true", help="Count bytes")
parser.add_argument("paths", nargs="+", help="The files to process")

args = parser.parse_args()

totalLines = 0
totalWords = 0
totalBytes = 0

for path in args.paths:
try:
with open(path, "rb") as f:
buffer = f.read()
content = buffer.decode("utf-8") #, errors="ignore"

lines = len(content.split("\n")) - 1
wordCount = len(content.strip().split())
bytes = len(buffer)

totalLines += lines
totalWords += wordCount
totalBytes += bytes

if args.l:
print(f"{lines} {path}")
elif args.w:
print(f"{wordCount} {path}")
elif args.c:
print(f"{bytes} {path}")
else:
print(f"{lines} {wordCount} {bytes} {path}")
except Exception as e:
print(f"Error reading {path}: {e}")

if len(args.paths) > 1:
if args.l:
print(f"{totalLines} total")
elif args.w:
print(f"{totalWords} total")
elif args.c:
print(f"{totalBytes} total")
else:
print(f"{totalLines} {totalWords} {totalBytes} total")
Loading