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..9feabcb06 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,24 @@ +import sys +import os + +args = sys.argv[1:] + +show_all = False +path = "." + +for arg in args: + + if arg == "-a": + show_all = True + + elif arg != "-1": + path = arg + +files = os.listdir(path) + +for file in files: + + if not show_all and file.startswith("."): + continue + + print(file) \ 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