diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..105aeb7c1 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,39 @@ +import sys + +args = sys.argv[1:] + +option = "none" +paths = [] + +# parse args +for arg in args: + if arg == "-n": + option = "n" + elif arg == "-b": + option = "b" + else: + paths.append(arg) + +for path in paths: + with open(path, "r") as file: + lines = file.readlines() + + line_number = 1 + + for line in lines: + + line = line.rstrip("\n") + + if option == "n": + print(f"{line_number} {line}") + line_number += 1 + + elif option == "b": + if line != "": + print(f"{line_number} {line}") + line_number += 1 + else: + print("") + + else: + print(line) \ 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..2e60b64c5 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,19 @@ +import argparse +import os + +parser = argparse.ArgumentParser() +parser.add_argument("-a", "--all", action="store_true") +parser.add_argument("-1", dest="one_per_line", action="store_true") +parser.add_argument("path", nargs="?", default=".") + +args = parser.parse_args() + +files = sorted(os.listdir(args.path)) + +files = [f for f in files if args.all or not f.startswith(".")] + +if args.one_per_line: + for f in files: + print(f) +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..e7c7fdad5 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,33 @@ +import sys + +args = sys.argv[1:] + +option = "all" +paths = [] + +for arg in args: + if arg == "-l": + option = "l" + elif arg == "-w": + option = "w" + elif arg == "-c": + option = "c" + else: + paths.append(arg) + +for path in paths: + with open(path, "r") as file: + content = file.read() + + lines = len(content.split("\n")) + words = len(content.split(" ")) + chars = len(content) + + if option == "l": + print(lines, path) + elif option == "w": + print(words, path) + elif option == "c": + print(chars, path) + else: + print(lines, words, chars, path) \ No newline at end of file