diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..7af2320c2 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,27 @@ +import argparse +import sys + +parser = argparse.ArgumentParser(prog="cat") +parser.add_argument("-n", action="store_true") +parser.add_argument("-b", action="store_true") +parser.add_argument("files", nargs="+") + +args = parser.parse_args() + + + +for file in args.files: + line_number = 1 + with open(file) as f: + for line in f: + if args.b: + if line.strip(): + print(f"{line_number:6}\t{line}", end="") + line_number += 1 + else: + print(line, end="") + elif args.n: + print(f"{line_number:6} {line}", end="") + line_number += 1 + else: + print(line, end="") \ 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..42b7afb7b --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,38 @@ +import argparse +import os + +parser = argparse.ArgumentParser(prog="ls") +parser.add_argument("-1", dest="one", action="store_true") +parser.add_argument("-a", action="store_true") +parser.add_argument("paths", nargs="*", default=["."]) + +args = parser.parse_args() + +# Print all files first +for path in args.paths: + if not os.path.isdir(path): + if args.one: + print(path) + else: + print(path, end=" ") +print() + +# Print directories +for path in args.paths: + if os.path.isdir(path): + + if len(args.paths) > 1: + print(f"\n{path}:") + + files = sorted(os.listdir(path)) + + if args.a: + files = [".", ".."] + files + else: + files = [f for f in files if not f.startswith(".")] + + if args.one: + for file in files: + print(file) + else: + print(" ".join(files)) \ 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..6488932df --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,58 @@ +import argparse + +parser = argparse.ArgumentParser(prog="wc") +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("files", nargs="+") + +args = parser.parse_args() + +total_lines = 0 +total_words = 0 +total_chars = 0 + +for file in args.files: + with open(file, "r") as f: + text = f.read() + + lines = text.count("\n") + + stripped = text.strip() + words = len(stripped.split()) if stripped else 0 + + chars = len(text.encode()) + + total_lines += lines + total_words += words + total_chars += chars + + output = "" + + if not args.l and not args.w and not args.c: + output += f"{lines:8}{words:8}{chars:8}" + else: + if args.l: + output += f"{lines:8}" + if args.w: + output += f"{words:8}" + if args.c: + output += f"{chars:8}" + + print(output, file) + + +if len(args.files) > 1: + output = "" + + if not args.l and not args.w and not args.c: + output += f"{total_lines:8}{total_words:8}{total_chars:8}" + else: + if args.l: + output += f"{total_lines:8}" + if args.w: + output += f"{total_words:8}" + if args.c: + output += f"{total_chars:8}" + + print(output, "total") \ No newline at end of file