Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
37 changes: 37 additions & 0 deletions implement-shell-tools/cat/my_cat.py

Choose a reason for hiding this comment

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

Try running the original cat program and comparing your output, do you see any differences?

Also, there is some code duplication in here, can you figure out how to clean that up?

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import argparse

parser = argparse.ArgumentParser(
prog="cat",
description="Read, display, and concatenate text files.",
)

parser.add_argument("-n", "--number", action="store_true", help="Number all output lines.")
parser.add_argument("-b", "--number-nonblank", action="store_true", help="Number non-blank output lines.")
parser.add_argument("paths", nargs="+", help="The file(s) to read.")

args = parser.parse_args()

for path in args.paths:
try:
with open(path, mode='r', encoding='utf-8') as f:
lines = f.readlines()
except Exception as err:
print(f"Error reading file '{path}': {err}")
continue

line_num = 1

for line in lines:
if args.number_nonblank:
if line.strip() != "":
print(f"{line_num:6}\t{line}", end="")
line_num += 1
else:
print(line, end="")
elif args.number:
print(f"{line_num:6}\t{line}", end="")
line_num += 1
else:
print(line, end="")


31 changes: 31 additions & 0 deletions implement-shell-tools/ls/my_ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import argparse

parser = argparse.ArgumentParser(
prog="ls",
description="List directory contents."
)

parser.add_argument("-1", "--one-per-line", action="store_true", help="List one file per line")
parser.add_argument("-a", "--all", action="store_true", help="Include hidden files (those starting with .)")
parser.add_argument("paths", nargs="*", default=["."], help="Directory path(s) to list")

args = parser.parse_args()

for path in args.paths:
try:
entries = os.listdir(path)
except Exception as e:
print(f"ls: cannot access '{path}': {e}")
continue

if not args.all:
entries = [entry for entry in entries if not entry.startswith(".")]

entries.sort()

if args.one_per_line:
for entry in entries:
print(entry)
else:
print(" ".join(entries))
52 changes: 52 additions & 0 deletions implement-shell-tools/wc/my_wc.py

Choose a reason for hiding this comment

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

There is some duplication of code here - can you think how to reduce this?

Copy link
Author

Choose a reason for hiding this comment

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

i have removed the duplicate code

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import argparse

parser = argparse.ArgumentParser(
prog="wc",
description="Counts lines, words, and bytes in text files."
)

parser.add_argument("-l", "--lines", action="store_true", help="Print the line counts")
parser.add_argument("-w", "--words", action="store_true", help="Print the word counts")
parser.add_argument("-c", "--bytes", action="store_true", help="Print the byte counts")
parser.add_argument("paths", nargs="+", help="One or more files to process")

args = parser.parse_args()

if not (args.lines or args.words or args.bytes):
args.lines = args.words = args.bytes = True

total_lines = total_words = total_bytes = 0

for path in args.paths:
try:
with open(path, "rb") as file:
content = file.read()
except Exception as e:
print(f"wc: {path}: {e}")
continue

lines = content.count(b'\n')
words = len(content.decode('utf-8', errors='ignore').split())
byte_count = len(content)

if args.lines:
print(f"{lines:>8}", end=" ")
if args.words:
print(f"{words:>8}", end=" ")
if args.bytes:
print(f"{byte_count:>8}", end=" ")

print(f"{path}")

total_lines += lines
total_words += words
total_bytes += byte_count

if len(args.paths) > 1:
if args.lines:
print(f"{total_lines:>8}", end=" ")
if args.words:
print(f"{total_words:>8}", end=" ")
if args.bytes:
print(f"{total_bytes:>8}", end=" ")
print("total")
1 change: 1 addition & 0 deletions individual-shell-tools/ls/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ fi

# TODO: Write a command to list the files and folders in this directory.
# The output should be a list of names including child-directory, script-01.sh, script-02.sh, and more.
ls
1 change: 1 addition & 0 deletions individual-shell-tools/ls/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ set -euo pipefail

# TODO: Write a command which lists all of the files in the directory named child-directory.
# The output should be a list of names: helper-1.txt, helper-2.txt, helper-3.txt.
ls chile