diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..4eb72d63f --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -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}") \ No newline at end of file diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..7f0682888 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -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}") \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..8394c9c8b --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -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")