diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..462251a2 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,38 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="display-file-content", + description="Output the content of a file to the terminal", +) + +parser.add_argument("-n", help="Number the output lines", action="store_true") +parser.add_argument("-b", help="Number the non-blank output lines", action="store_true") +parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+") + +args = parser.parse_args() + +line_number = 1 + +for path in args.paths: + with open(path, "r") as f: + for line in f: + line = line.rstrip("\n") + if args.n: + print(f"{line_number:>6} {line}") + line_number += 1 + elif args.b: + if line != "": + print(f"{line_number} {line}") + line_number += 1 + else: + print(f"{line}") + else: + print(f"{line}") + + + + + + + + diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 00000000..30bcf101 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,42 @@ +import argparse +import os + +parser = argparse.ArgumentParser( + prog="list-files-in-directory", + description="List all files and directories in a directory", +) + +parser.add_argument("-1", "--one", dest="one", help="Output one entry per line", action="store_true") +parser.add_argument("-a", help="List all files & directories, including hidden ones", action="store_true") +parser.add_argument("paths", nargs="*", default=["."], help="The file(s) or directories to list") + +args = parser.parse_args() + +for path in args.paths: + if os.path.isdir(path): + items = os.listdir(path) + + if args.a: + items = ['.', '..'] + items + else: + items = [item for item in items if not item.startswith(".")] + + items.sort() + + for item in items: + if args.one: + print(item) + else: + print(item, end="\t") + if not args.one: + print() + else: + print(path) + + + + + + + + diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 00000000..5e15c66c --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,76 @@ + +import argparse +import os + +parser = argparse.ArgumentParser( + prog="counter", + description="Counts lines, words or characters in a file (or all files) inside a directory", +) + +parser.add_argument("-l", "--line", dest="line", help="The number of lines in each file", action="store_true") +parser.add_argument("-w", "--word", dest="word", help="The number of words in each file", action="store_true") +parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file", action="store_true") +parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+") + +args = parser.parse_args() + +total = {'lines': 0, 'words': 0, 'characters': 0, 'files': 0} + +def counter(item): + lines = len(item.strip().split("\n")) + words = len(item.split()) + characters = len(item) + return {"lines": lines, "words": words, "characters": characters} + + +def process_file(file_path, total): + with open(file_path, "r") as f: + content = f.read() + + stats = counter(content) + + if args.line: + print(f"{stats['lines']} {file_path}") + elif args.word: + print(f"{stats['words']} {file_path}") + elif args.char: + print(f"{stats['characters']} {file_path}") + else: + print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}") + + total['lines'] += stats['lines'] + total['words'] += stats['words'] + total['characters'] += stats['characters'] + total['files'] += 1 + + + +for path in args.paths: + if os.path.isfile(path): + process_file(path, total) + elif os.path.isdir(path): + for file in os.listdir(path): + file_path = os.path.join(path, file) + if os.path.isfile(file_path): + process_file(file_path, total) + + + + + + + + +if total['files'] > 1: + if args.line: + print(f"{total['lines']} total") + elif args.word: + print(f"{total['words']} total") + elif args.char: + print(f"{total['characters']} total") + else: + print(f"{total['lines']} {total['words']} {total['characters']} total") + + + +