From 2d6dd408a4c6f361ddaa4e674ae998ad6bd320d3 Mon Sep 17 00:00:00 2001 From: pathywang Date: Thu, 23 Jul 2026 23:06:18 +0100 Subject: [PATCH 1/9] done --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index c4234ab74..5bb64fa54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ + node_modules testoutput.txt + +.venv/ +__pycache__/ +*.pyc From 308d2ac6d0e384a2d797d08d16f834d58cedd25d Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 29 Jul 2026 16:11:59 +0100 Subject: [PATCH 2/9] complete cat.py --- implement-shell-tools/cat/cat.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..d64bcb06c --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,32 @@ +import sys + +args = sys.argv[1:] + +number_all = "-n" in args +number_nonblank = "-b" in args + +files = [arg for arg in args if arg not in ("-n", "-b")] + +line_number = 1 + +for filename in files: + try: + with open(filename, "r") as file: + content = file.read() + + lines = content[:-1].split("\n") if content.endswith("\n") else content.split("\n") + + for line in lines: + if number_all: + print(f"{line_number:6}\t{line}") + line_number += 1 + + elif number_nonblank and line != "": + print(f"{line_number:6}\t{line}") + line_number += 1 + + else: + print(line) + + except Exception as err: + print(f"cat: {filename}: {err}") \ No newline at end of file From 4f8d6becbc2bbfb3526075906c69c0629e00409b Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 29 Jul 2026 16:17:43 +0100 Subject: [PATCH 3/9] update --- implement-shell-tools/cat/cat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index d64bcb06c..7dee05867 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -29,4 +29,5 @@ print(line) except Exception as err: - print(f"cat: {filename}: {err}") \ No newline at end of file + print(f"cat: {filename}: {err}") + \ No newline at end of file From 7e4b6557fefbf4cfc8df6f7293456e6136e9f3c0 Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 29 Jul 2026 16:39:25 +0100 Subject: [PATCH 4/9] complete ls.py --- implement-shell-tools/ls/ls.py | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 implement-shell-tools/ls/ls.py diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..100673d4f --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,48 @@ +import sys +import os + +args = sys.argv[1:] + +one_line = False +show_hidden = False +targets = [] + +for arg in args: + if arg == "-1": + one_line = True + elif arg == "-a": + show_hidden = True + else: + targets.append(arg) + +if len(targets) == 0: + targets.append(".") + + +def list_directory(directory): + files = os.listdir(directory) + + if not show_hidden: + files = [file for file in files if not file.startswith(".")] + + files.sort() + + if one_line: + print("\n".join(files)) + else: + print(" ".join(files)) + + +def list_target(target): + try: + if os.path.isdir(target): + list_directory(target) + else: + print(target) + + except Exception: + print(f"ls: cannot access '{target}': No such file or directory") + + +for target in targets: + list_target(target) \ No newline at end of file From 84f27ee893020349b78576d5e94a901b991d0c8d Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 29 Jul 2026 16:48:02 +0100 Subject: [PATCH 5/9] complete wc --- implement-shell-tools/wc/wc.py | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..6a6e2c22b --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,92 @@ +import sys + +args = sys.argv[1:] + +count_lines = False +count_words = False +count_bytes = False +files = [] + +for arg in args: + if arg == "-l": + count_lines = True + elif arg == "-w": + count_words = True + elif arg == "-c": + count_bytes = True + else: + files.append(arg) + +# If no flags are given, wc shows all three +if not count_lines and not count_words and not count_bytes: + count_lines = True + count_words = True + count_bytes = True + + +def count_file(filename): + try: + with open(filename, "r") as file: + content = file.read() + + lines = content.count("\n") + + words = 0 if content.strip() == "" else len(content.split()) + + # UTF-8 bytes, same idea as Buffer.byteLength() + bytes_count = len(content.encode("utf-8")) + + output = [] + + if count_lines: + output.append(str(lines)) + + if count_words: + output.append(str(words)) + + if count_bytes: + output.append(str(bytes_count)) + + output.append(filename) + + print(" ".join(output)) + + return { + "lines": lines, + "words": words, + "bytes": bytes_count + } + + except Exception as err: + print(f"wc: {filename}: {err}") + return None + + +total_lines = 0 +total_words = 0 +total_bytes = 0 + +for file in files: + counts = count_file(file) + + if counts: + total_lines += counts["lines"] + total_words += counts["words"] + total_bytes += counts["bytes"] + + +if len(files) > 1: + output = [] + + if count_lines: + output.append(str(total_lines)) + + if count_words: + output.append(str(total_words)) + + if count_bytes: + output.append(str(total_bytes)) + + output.append("total") + + print(" ".join(output)) \ No newline at end of file From 360587a5e0eacda6aa15105e13e3eb073115ee10 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 1 Aug 2026 14:15:09 +0100 Subject: [PATCH 6/9] remove gitignore --- .gitignore | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 5bb64fa54..000000000 --- a/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ - -node_modules -testoutput.txt - -.venv/ -__pycache__/ -*.pyc From 884a186125850e89dae6a333859aaa565e8f0878 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 1 Aug 2026 14:15:58 +0100 Subject: [PATCH 7/9] restore --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..5bb64fa54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ + +node_modules +testoutput.txt + +.venv/ +__pycache__/ +*.pyc From 484d5dbf4beba7a3a304ffdab4f8066c3de57282 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 1 Aug 2026 14:17:11 +0100 Subject: [PATCH 8/9] fixup --- .gitignore | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5bb64fa54..c4234ab74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,2 @@ - node_modules testoutput.txt - -.venv/ -__pycache__/ -*.pyc From bb30375a7fe39ca8e824dc61af87efc924572e6a Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 3 Aug 2026 23:15:24 +0100 Subject: [PATCH 9/9] some amendment --- implement-shell-tools/wc/wc.py | 48 ++++++++++++++-------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 6a6e2c22b..384707252 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -23,6 +23,22 @@ count_words = True count_bytes = True +def print_output(lines, words, bytes_count, label): + output = [] + + if count_lines: + output.append(f"{lines:8}") + + if count_words: + output.append(f"{words:8}") + + if count_bytes: + output.append(f"{bytes_count:8}") + + output.append(f" {label}") + + print("".join(output)) + def count_file(filename): try: @@ -36,20 +52,7 @@ def count_file(filename): # UTF-8 bytes, same idea as Buffer.byteLength() bytes_count = len(content.encode("utf-8")) - output = [] - - if count_lines: - output.append(str(lines)) - - if count_words: - output.append(str(words)) - - if count_bytes: - output.append(str(bytes_count)) - - output.append(filename) - - print(" ".join(output)) + print_output(lines, words, bytes_count, filename) return { "lines": lines, @@ -66,6 +69,8 @@ def count_file(filename): total_words = 0 total_bytes = 0 + + for file in files: counts = count_file(file) @@ -76,17 +81,4 @@ def count_file(filename): if len(files) > 1: - output = [] - - if count_lines: - output.append(str(total_lines)) - - if count_words: - output.append(str(total_words)) - - if count_bytes: - output.append(str(total_bytes)) - - output.append("total") - - print(" ".join(output)) \ No newline at end of file + print_output(total_lines, total_words, total_bytes, "total") \ No newline at end of file