diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..3a4a6f928 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,39 @@ +import argparse + +def print_file(filename, mode): + with open(filename, "r") as file: + line_number = 1 + + for line in file: + if mode == "number_all": + print(f"{line_number} {line}", end="") + line_number += 1 + + elif mode == "number_nonempty": + if line.strip() == "": + print(line, end="") + else: + print(f"{line_number} {line}", end="") + line_number += 1 + + else: + print(line, end="") + + +parser = argparse.ArgumentParser() +group = parser.add_mutually_exclusive_group() +group.add_argument("-n", action="store_true", help="Number all lines") +group.add_argument("-b", action="store_true", help="Number non-empty lines") +parser.add_argument("filenames", nargs="+") + +args = parser.parse_args() + +if args.n: + mode = "number_all" +elif args.b: + mode = "number_nonempty" +else: + mode = "none" + +for filename in args.filenames: + print_file(filename, mode) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..c89352008 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,65 @@ +import sys +import os + + +# Get arguments from the command line +arguments = sys.argv[1:] + + +# Check if -1 flag exists +show_one_per_line = False + +if "-1" in arguments: + show_one_per_line = True + arguments.remove("-1") + + +# Check if -a flag exists +show_all_files = False + +if "-a" in arguments: + show_all_files = True + arguments.remove("-a") + + +# If no path is given, use current directory +if len(arguments) == 0: + path = "." +else: + path = arguments[0] + + +# Check if the path exists +if not os.path.exists(path): + print(f"ls: cannot access '{path}': No such file or directory") + sys.exit() + + +# If the path is a file, just print the file name +if os.path.isfile(path): + print(path) + + +# If the path is a folder, list its contents +else: + + files = os.listdir(path) + + + # Remove hidden files unless -a was used + if not show_all_files: + files = [ + file for file in files + if not file.startswith(".") + ] + + + # Print one file per line + if show_one_per_line: + for file in files: + print(file) + + + # Normal ls behaviour + 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..9cb6589d9 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,60 @@ + +import sys + +arguments = sys.argv[1:] +count_lines = False +count_words = False +count_characters = False +filenames = [] + +for argument in arguments: + if argument == "-l": + count_lines = True + elif argument == "-w": + count_words = True + elif argument == "-c": + count_characters = True + else: + filenames.append(argument) + +if not count_lines and not count_words and not count_characters: + count_lines = True + count_words = True + count_characters = True + +total_lines = 0 +total_words = 0 +total_characters = 0 + +for filename in filenames: + with open(filename, "r") as file: + content = file.read() + + lines = len(content.splitlines()) + words = len(content.split()) + characters = len(content) + + total_lines += lines + total_words += words + total_characters += characters + + answer = "" + if count_lines: + answer += str(lines) + " " + if count_words: + answer += str(words) + " " + if count_characters: + answer += str(characters) + " " + + print(answer + filename) + +if len(filenames) > 1: + total = "" + if count_lines: + total += str(total_lines) + " " + if count_words: + total += str(total_words) + " " + if count_characters: + total += str(total_characters) + " " + + print(total + "total")