diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..07b3e9a02 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,29 @@ +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument("files", nargs="+") +parser.add_argument("-n", action="store_true") +parser.add_argument("-b", action="store_true") + +args = parser.parse_args() + +line_number = 1 + +for file in args.files: + try: + f = open(file, "r") + + for line in f: + if args.b and line.strip() != "": + print(f" {line_number}", line, end="") + line_number += 1 + elif args.n and not args.b: + print(f" {line_number}", line, end="") + line_number += 1 + else: + print(line, end="") + + f.close() + + except: + print("cat:", file, "not found") \ 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..945014938 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,34 @@ +import argparse +import os + + +def get_files(directory): + files = os.listdir(directory) + files.extend([".", ".."]) + files.sort(key=str.lower) + return files + + +def display_files(files, show_all=False, one_per_line=False): + if not show_all: + files = [f for f in files if not f.startswith(".")] + + if one_per_line: + for file in files: + print(file) + else: + print(" ".join(files)) + + +parser = argparse.ArgumentParser() +parser.add_argument("directory", nargs="?", default=".") +parser.add_argument("-1", dest="one_per_line", action="store_true", help="list one file per line") +parser.add_argument("-a", dest="show_all", action="store_true", help="include hidden files") + +args = parser.parse_args() + +try: + files = get_files(args.directory) + display_files(files, show_all=args.show_all, one_per_line=args.one_per_line) +except OSError as e: + print(f"ls: {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..4353bc5f5 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,64 @@ +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument("files", nargs="+") +parser.add_argument("-l", action="store_true", help="show line count") +parser.add_argument("-w", action="store_true", help="show word count") +parser.add_argument("-c", action="store_true", help="show byte count") + +args = parser.parse_args() + +show_lines = args.l +show_words = args.w +show_bytes = args.c + +if not show_lines and not show_words and not show_bytes: + show_lines = True + show_words = True + show_bytes = True + +total_lines = 0 +total_words = 0 +total_bytes = 0 + +for file in args.files: + try: + f = open(file, "rb") + content = f.read() + f.close() + + line_count = content.count(b"\n") + word_count = len(content.split()) + byte_count = len(content) + + total_lines += line_count + total_words += word_count + total_bytes += byte_count + + output = [] + + if show_lines: + output.append(str(line_count)) + if show_words: + output.append(str(word_count)) + if show_bytes: + output.append(str(byte_count)) + + output.append(file) + print(" ".join(output)) + + except OSError: + print("wc:", file, "not found") + +if len(args.files) > 1: + output = [] + + if show_lines: + output.append(str(total_lines)) + if show_words: + output.append(str(total_words)) + if show_bytes: + output.append(str(total_bytes)) + + output.append("total") + print(" ".join(output)) \ No newline at end of file