From 0cad0a6253523112953e31a8d5be81ad7607d695 Mon Sep 17 00:00:00 2001 From: shaghayeghfar <146011477+shaghayeghfar@users.noreply.github.com> Date: Fri, 7 Aug 2026 15:14:44 +0100 Subject: [PATCH] implement shell tools with python --- implement-shell-tools/cat/cat.py | 43 ++++++++++++++++ implement-shell-tools/ls/ls.py | 65 +++++++++++++++++++++++ implement-shell-tools/wc/wc.py | 88 ++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py create mode 100644 implement-shell-tools/ls/ls.py create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..d48989b70 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,43 @@ +import sys + +# Check if the user used a flag +if len(sys.argv) > 1 and sys.argv[1] == "-n": + + # Start from the second argument because the first is "-n" + for filename in sys.argv[2:]: + + with open(filename, "r") as file: + + line_number = 1 + + for line in file: + print(f"{line_number} {line}", end="") + line_number += 1 + + +elif len(sys.argv) > 1 and sys.argv[1] == "-b": + + # Start from the second argument because the first is "-b" + for filename in sys.argv[2:]: + + with open(filename, "r") as file: + + line_number = 1 + + for line in file: + + # Only number non-empty lines + if line.strip() == "": + print(line, end="") + else: + print(f"{line_number} {line}", end="") + line_number += 1 + + +else: + + # No flag, just print every file + for filename in sys.argv[1:]: + + with open(filename, "r") as file: + print(file.read(), 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..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..5b64dee6b --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,88 @@ +import sys + + +# Get everything the user typed after wc.py +arguments = sys.argv[1:] + + +# These are our options +count_lines = False +count_words = False +count_characters = False + + +# Store file names here +filenames = [] + + +# Check every argument +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 the user did not give any flag, +# show all counts like normal wc +if (not count_lines + and not count_words + and not count_characters): + + count_lines = True + count_words = True + count_characters = True + + + +# Go through every file +for filename in filenames: + + # Open the file + with open(filename, "r") as file: + + # Read the whole file + content = file.read() + + + # Count lines + lines = len(content.splitlines()) + + + # Count words + words = len(content.split()) + + + # Count characters + characters = len(content) + + + + # Prepare the answer + answer = "" + + + if count_lines: + answer += str(lines) + " " + + + if count_words: + answer += str(words) + " " + + + if count_characters: + answer += str(characters) + " " + + + + # Print the result + print(answer + filename) \ No newline at end of file