diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..e3f73283 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,35 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="cat", + description="Concatenate and print files", +) + +parser.add_argument( + "-n", action="store_true", help="Number the output lines, starting at 1" +) +parser.add_argument( + "-b", action="store_true", help="Number the non-blank output lines, starting at 1" +) +parser.add_argument("path", nargs="+", help="The path to search") + +args = parser.parse_args() +for path in args.path: + with open(path) as file: + lines = file.readlines() + line_num = 1 + for line in lines: + lin = line.rstrip("\n") + + if args.b: + if lin == "": + print() + else: + print(line_num, lin) + line_num += 1 + elif args.n: + print(line_num, lin) + line_num += 1 + + else: + print(lin) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 00000000..c45ecd81 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,41 @@ +import os +import argparse + +parser = argparse.ArgumentParser( + prog="ls", + description="list directory contents", +) + +parser.add_argument("-1",dest="one",action='store_true', help="list one file per line") +parser.add_argument("-a", action='store_true', help="Used to list all files, including hidden files, in the current directory") +parser.add_argument("path", nargs="?", default=".", help="The path to search") + +args = parser.parse_args() + +def arguments_proceeding(files): + data_to_proceed = files + + if args.a: + data_to_proceed = [f for f in files] + data_to_proceed.sort() + data_to_proceed.insert(0, "..") + data_to_proceed.insert(0, ".") + else: + data_to_proceed = [f for f in files if not f.startswith('.')] + data_to_proceed.sort(key=str.lower) + if args.one: + output = [f for f in data_to_proceed] + for f in output: + print(f) + else: + print(" ".join(data_to_proceed)) + + +def path_proceeding(path_argument): + if os.path.isfile(path_argument): + print(path_argument) + elif os.path.isdir(path_argument): + files = os.listdir(path_argument) + arguments_proceeding(files) + +path_proceeding(args.path) \ 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 00000000..d91d133b --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,54 @@ +import os +import argparse + +parser = argparse.ArgumentParser( + prog="wc", + description="Counts words in a file that contain a particular character", +) + +parser.add_argument("-l",action='store_true', help="The number of lines in each input file is written to the standard output.") +parser.add_argument("-w", action='store_true', help="The number of words in each input file is written to the standard output.") +parser.add_argument("-c", action='store_true', help="The number of bytes in each input file is written to the standard output.") +parser.add_argument("path", nargs="+", help="The path to search") + +args = parser.parse_args() + +if (not args.w and not args.c and not args.l) : + args.w = args.c = args.l = True +total = [] +for path in args.path: + output = [] + if args.l or args.w: + with open(path) as file: + lines = file.readlines() + # lines count + if args.l: + num_lines = len(lines) + output.append(num_lines) + # word count + if args.w: + word_count = 0 + for line in lines: + lin = line.rstrip() + wds = lin.split() + word_count += len(wds) + + output.append(word_count) + + + if args.c: + file_size = os.path.getsize(path) + output.append(file_size) + + if len(args.path) > 1: + total.append(output.copy()) + output.append(path) + string_list = map(str, output) + print(" ".join(string_list)) +if len(args.path) > 1: + result = [sum(i) for i in zip(*total)] + string_result_list = map(str, result) + print(" ".join(string_result_list), " total") + + + \ No newline at end of file