diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..b785a7688 Binary files /dev/null and b/.DS_Store differ diff --git a/implement-shell-tools-python-mansoor/.DS_Store b/implement-shell-tools-python-mansoor/.DS_Store new file mode 100644 index 000000000..834b99d3e Binary files /dev/null and b/implement-shell-tools-python-mansoor/.DS_Store differ diff --git a/implement-shell-tools-python-mansoor/1.txt b/implement-shell-tools-python-mansoor/1.txt new file mode 100644 index 000000000..987c01d27 --- /dev/null +++ b/implement-shell-tools-python-mansoor/1.txt @@ -0,0 +1 @@ +Hello world, Hi, ETC diff --git a/implement-shell-tools-python-mansoor/cat/.DS_Store b/implement-shell-tools-python-mansoor/cat/.DS_Store new file mode 100644 index 000000000..5008ddfcf Binary files /dev/null and b/implement-shell-tools-python-mansoor/cat/.DS_Store differ diff --git a/implement-shell-tools-python-mansoor/cat/cat.py b/implement-shell-tools-python-mansoor/cat/cat.py new file mode 100644 index 000000000..a85fc547e --- /dev/null +++ b/implement-shell-tools-python-mansoor/cat/cat.py @@ -0,0 +1,47 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="cat", + description="Concatenate and display files", +) + +parser.add_argument( + "-n", + action="store_true", + help="number all lines", +) + +parser.add_argument( + "-b", + action="store_true", + help="number non-empty lines", +) + +parser.add_argument( + "files", + nargs="+", + help="the files to read", +) + +args = parser.parse_args() + +line_number = 1 + +for filename in args.files: + try: + with open(filename) as file: + for line in file: + content = line.rstrip("\n") + + if args.b and content == "": + print() + continue + + if args.n or args.b: + print(f"{line_number}\t{content}") + line_number +=1 + else: + print(content) + + except OSError as error: + print(f"{error.strerror}") \ No newline at end of file diff --git a/implement-shell-tools-python-mansoor/ls/ls.py b/implement-shell-tools-python-mansoor/ls/ls.py new file mode 100644 index 000000000..3a4b3f947 --- /dev/null +++ b/implement-shell-tools-python-mansoor/ls/ls.py @@ -0,0 +1,52 @@ +import argparse +import os + +parser = argparse.ArgumentParser( + prog = "ls", + description = "List directory content" +) + +parser.add_argument( + "paths", + nargs = "*", + default=["."], + help= "the directory path to process", +) + +parser.add_argument( + "-1", + "--one", + action="store_true", + help="list one file per line", + +) + +parser.add_argument( + "-a", + "--all", + action="store_true", + help="List all files including hidden files such as '.' and '..' " +) + +args = parser.parse_args() + +for target_dir in args.paths: + try: + files = os.listdir(target_dir) + + if args.all: + file_list = sorted([".", ".."] + files) + else: + file_list = [] + for name in files: + if not name.startswith("."): + file_list.append(name) + file_list.sort() + + if args.one: + for name in file_list: + print(name) + else: + print(" ".join(file_list)) + except OSError as error: + print(f"{error.strerror}") \ No newline at end of file diff --git a/implement-shell-tools-python-mansoor/wc/wc.py b/implement-shell-tools-python-mansoor/wc/wc.py new file mode 100644 index 000000000..7450dc9a3 --- /dev/null +++ b/implement-shell-tools-python-mansoor/wc/wc.py @@ -0,0 +1,86 @@ +import argparse + +parser = argparse.ArgumentParser( + prog ="wc", + description ="my word, line and byte count", +) + +parser.add_argument( + "-l", + "--lines", + action="store_true", + help="count lines" +) + +parser.add_argument( + "-w", + "--words", + action="store_true", + help="count words", +) + +parser.add_argument( + "-c", + "--bytes", + action ="store_true", + help="count bytes", +) + +parser.add_argument( + "files", + nargs="+", + help=" the files to process", +) + +args = parser.parse_args() + +if not args.lines and not args.words and not args.bytes: + show_line = True + show_words = True + show_bytes = True +else: + show_line = args.lines + show_words = args.words + show_bytes = args.bytes + +total_lines = 0 +total_words = 0 +total_bytes = 0 + + +for filename in args.files: + try: + with open(filename, "rb") as f: + content = f.read() + + lines = content.count(b"\n") + words = len(content.split()) + bytes_count = len(content) + + total_lines += lines + total_words += words + total_bytes += bytes_count + + output = [] + if show_line: + output.append(str(lines)) + if show_words: + output.append(str(words)) + if show_bytes: + output.append(str(bytes_count)) + output.append(filename) + print("\t".join(output)) + + except OSError as error: + print(f"{error.strerror}") + +if len(args.files) > 1: + total_output = [] + if show_line: + total_output.append(str(total_lines)) + if show_words: + total_output.append(str(total_words)) + if show_bytes: + total_output.append(str(total_bytes)) + total_output.append("total") + print("\t".join(total_output)) \ No newline at end of file diff --git a/implement-shell-tools/.DS_Store b/implement-shell-tools/.DS_Store new file mode 100644 index 000000000..e506622ac Binary files /dev/null and b/implement-shell-tools/.DS_Store differ diff --git a/implement-shell-tools/cat/.DS_Store b/implement-shell-tools/cat/.DS_Store new file mode 100644 index 000000000..fe85be12e Binary files /dev/null and b/implement-shell-tools/cat/.DS_Store differ diff --git a/individual-shell-tools/.DS_Store b/individual-shell-tools/.DS_Store new file mode 100644 index 000000000..19d948fe8 Binary files /dev/null and b/individual-shell-tools/.DS_Store differ