diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..45438f42 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,30 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="cat-command", description="cat shell command in python " +) + +parser.add_argument("-n", action="store_true", help="Display all lines numbers") +parser.add_argument("-b", action="store_true", help="Display numbers non-empty lines") +parser.add_argument("path", nargs="+", help="The file to search") + +args = parser.parse_args() +number = 1 + +for file_path in args.path: + with open(file_path, "r") as f: + content = f.readlines() + + if args.n: + for line in content: + print(f"{number}\t{line.strip()}") + number += 1 + elif args.b: + for line in content: + if line.strip() != "": + print(f"{number}\t{line.strip()}") + number += 1 + else: + print("") + else: + print("".join(content)) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 00000000..5813ef95 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,57 @@ +import argparse +import os +import stat +import pwd +import grp +import time + +parser = argparse.ArgumentParser( + prog="ls-command", description="ls shell command on python" +) + +parser.add_argument("-1", action="store_true", dest="one", help="One file per line") +parser.add_argument( + "-l", action="store_true", help="Display long format description files" +) +parser.add_argument( + "-a", action="store_true", help="Display hidden files along with visible" +) +parser.add_argument("path", nargs="*", default=["."], help="The file to search") + + +args = parser.parse_args() + + +def long_format(path, file): + info = os.stat(path) + permissions = stat.filemode(info.st_mode) + size_file = info.st_size + owner = pwd.getpwuid(info.st_uid).pw_name + group = grp.getgrgid(info.st_gid).gr_name + mtime = time.strftime("%b %d %H:%M", time.localtime(info.st_mtime)) + print(permissions, size_file, owner, group, mtime, file) + + +for path in args.path: + if os.path.isfile(path): + file = os.path.basename(path) + if args.l: + long_format(path, file) + else: + print(file) + elif os.path.isdir(path): + files = os.listdir(path) + + if not args.a: + files = [file for file in files if not file.startswith(".")] + + for file in files: + full_file_path = os.path.join(path, file) + + if args.l: + long_format(full_file_path, file) + else: + if args.one: + print(file) + else: + print(file, "\n") diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 00000000..cd69d4c9 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,53 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="wc-count words, lines, characters", description="wc shell command on python" +) + +parser.add_argument("-l", action="store_true", help="Count lines") +parser.add_argument("-w", action="store_true", help="Count words") +parser.add_argument("-c", action="store_true", help="Counts bytes") +parser.add_argument("path", nargs="+", help="The file to search") + +args = parser.parse_args() + +def calculate_counts(content): + + count_lines = content.count("\n") + count_words = len(content.split()) + count_bytes = len(content.encode("utf-8")) + + return count_lines, count_words, count_bytes + +def format_output(count_lines, count_words, count_bytes, file_path): + if not (args.l or args.w or args.c): + print(count_lines, count_words, count_bytes, file_path) + else: + line = "" + if args.l: + line += f"{count_lines} " + if args.w: + line += f"{count_words} " + if args.c: + line += f"{count_bytes} " + line += f"{file_path}" + print(line) + +count_total_lines = 0 +count_total_words = 0 +count_total_bytes = 0 + +for file_path in args.path: + with open(file_path, "r") as file: + content = file.read() + + count_lines, count_words, count_bytes = calculate_counts(content) + + count_total_lines += count_lines + count_total_words += count_words + count_total_bytes += count_bytes + + format_output(count_lines, count_words, count_bytes, file_path) + +if len(args.path) > 1: + format_output(count_total_lines, count_total_words, count_total_bytes, " total")