diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..14df1dd5c --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,39 @@ +import argparse + +parser = argparse.ArgumentParser(prog="cat", description="Prints the output of a file to the console") +parser.add_argument("-n", "--number", help="Displays the lines along with their number", action="store_true") +parser.add_argument("-b", "--nonblank", help="Displays the lines along with their number skipping the blank lines", action="store_true") +parser.add_argument("path", help="The file path", nargs="+") + +args = parser.parse_args() + +show_lines = args.number +non_blank = args.nonblank + +if show_lines == True and non_blank == True: + print("Error: Cannot use -n and -b together. Please use only one flag at a time.") + exit() + +text = "" +i = 1 + +for file in args.path: + f = open(file) + text += f.read() + +text_list = text.split("\n") +text_list.pop(len(text_list) - 1) + +if (show_lines == False and non_blank == False): + print("\n".join(text_list)) + exit() + +for line in text_list: + if non_blank == True and line != "": + print(" " + str(i) + " " + line) + i += 1 + elif non_blank == False: + print(" " + str(i) + " " + line) + i += 1 + else: + print("") \ 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..ee4cc588d --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,29 @@ +import argparse +from os import listdir + +parser = argparse.ArgumentParser(prog="ls", description="List directory contents. Ignore files and directories starting with a '.' by default") +parser.add_argument("-1", dest="one", help="List one file per line.", action="store_true") +parser.add_argument("-a", help="Do not ignore hidden files (files with names that start with '.').", action="store_true") +parser.add_argument("path", help="The directory path (optional).", default=".", nargs="?") + +args = parser.parse_args() + +path = args.path + +show_hidden = args.a +one_per_line = args.one + +contents = [] + +for f in listdir(path): + if show_hidden == False and f[0] != ".": + contents.append(f) + if show_hidden == True: + contents.append(f) + +contents.sort() + +if one_per_line == True: + print("\n".join(contents)) +else: + print(" ".join(contents)) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..1b31aa20d --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,62 @@ +import argparse + +parser = argparse.ArgumentParser(prog="wc", description="Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.") +parser.add_argument("-w", "--words", help="print the word counts", action="store_true") +parser.add_argument("-l", "--line", help="print the newline counts", action="store_true") +parser.add_argument("-c", "--bytes", help="print the byte counts", action="store_true") +parser.add_argument("path", help="The file path", nargs="+") + +args = parser.parse_args() + +lines = 0 +words = 0 +bytes = 0 + +l = True +w = True +c = True + +if args.words == True or args.line == True or args.bytes == True: + l = args.line + w = args.words + c = args.bytes + +file_data = {} +file_data_with_newline = {} + +for file in args.path: + f = open(file) + file_data[file] = f.read().split("\n") + +for file in args.path: + f = open(file) + file_data_with_newline[file] = f.read() + +def print_helper(line, word, byte, file_name): + text = [" "] + if l == True: + text.append(str(line)) + text.append(" ") + if w == True: + text.append(str(word)) + text.append(" ") + if c == True: + text.append(str(byte)) + text.append(" ") + text.append(file_name) + print("".join(text)) + +for f in file_data: + word_per_line = 0 + byte_per_line = 0 + for line in file_data[f]: + word_per_line += len(line.split()) + for line in file_data_with_newline[f]: + byte_per_line += len(line.encode("utf-8")) + lines += len(file_data[f]) - 1 + words += word_per_line + bytes += byte_per_line + print_helper(len(file_data[f]) - 1, word_per_line, byte_per_line, f) + +if len(args.path) > 1: + print_helper(lines, words, bytes, "total") \ No newline at end of file