diff --git a/implement-shell-tools/cat/.gitignore b/implement-shell-tools/cat/.gitignore new file mode 100644 index 00000000..0cafc1cd --- /dev/null +++ b/implement-shell-tools/cat/.gitignore @@ -0,0 +1 @@ +.venv/ \ No newline at end of file diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..9a65d947 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,33 @@ +import argparse +import glob + + +parser = argparse.ArgumentParser(description="cat command") +parser.add_argument("files", nargs="+", help="Files to read") +parser.add_argument("-n", action="store_true", help="Number all lines") +parser.add_argument("-b", action="store_true", help="Number non-empty lines") +args = parser.parse_args() + + +files = [] +for pattern in args.files: + files.extend(glob.glob(pattern)) + + +line_number = 1 + +for file in files: + with open(file, "r") as f: + for line in f: + line_content = line.rstrip("\n") + + if args.b and line_content != "": + print(f"{line_number}\t{line_content}") + line_number += 1 + elif args.n: + print(f"{line_number}\t{line_content}") + line_number += 1 + else: + print(line_content) + + diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 00000000..713cde6c --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,20 @@ +import argparse +import os +parser=argparse.ArgumentParser(description="ls command") +parser.add_argument("-1",dest="one_per_line",action="store_true",help="List one file per line") +parser.add_argument("-a",dest="all", action="store_true",help="Include hidden files") +parser.add_argument("path", nargs="?", default=".", help="Directory to list") + +args=parser.parse_args() +files=os.listdir(args.path) +if not args.all: + new_files=[] + for f in files: + if not f.startswith("."): + new_files.append(f) + files=new_files +if args.one_per_line: + for f in files: + print(f) +else: + print(" ".join(files)) \ 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..feb0a5f1 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,66 @@ +import argparse +import os + +parser = argparse.ArgumentParser(description="wc command") +parser.add_argument("file", help="File or directory to read") +parser.add_argument("-l", dest="lines", action="store_true", help="count the number of lines") +parser.add_argument("-w", dest="words", action="store_true", help="count the number of words") +parser.add_argument("-c", dest="chars", action="store_true", help="count the number of characters") +args = parser.parse_args() + +if os.path.isdir(args.file): + files = os.listdir(args.file) + for f in files: + path = os.path.join(args.file, f) + if os.path.isfile(path): + with open(path, "r") as file_obj: + text = file_obj.read() + line_count = len(text.splitlines()) + word_count = len(text.split()) + char_count = len(text) + + if not (args.lines or args.words or args.chars): + print(f"{line_count}\t{word_count}\t{char_count}\t{path}") + continue + output = [] + if args.lines: + output.append(str(line_count)) + if args.words: + output.append(str(word_count)) + if args.chars: + output.append(str(char_count)) + output.append(path) + print("\t".join(output)) +else: + + with open(args.file, "r") as f: + text = f.read() + line_count = len(text.splitlines()) + word_count = len(text.split()) + char_count = len(text) + if not (args.lines or args.words or args.chars): + print(f"{line_count}\t{word_count}\t{char_count}\t{args.file}") + else: + output = [] + if args.lines: + output.append(str(line_count)) + if args.words: + output.append(str(word_count)) + if args.chars: + output.append(str(char_count)) + output.append(args.file) + print("\t".join(output)) + + + + + + + + + + + + + +