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..0b93e511 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,27 @@ +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 != "") or args.n: + print(f"{line_number}\t{line_content}") + line_number += 1 + else: + print(line_content) \ 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 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..4834ee7a --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,42 @@ +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() + +def output(line_count ,word_count ,char_count,filename,args): + if not(args.lines or args.words or args.chars): + return f"{line_count}\t{word_count}\t{char_count}\t{filename}" + 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(filename) + return "\t".join(output) + +def count_file(path): + with open(path) as f: + text = f.read() + return ( + len(text.splitlines()), + len(text.split()), + len(text), + ) + +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): + line_count, word_count, char_count = count_file(path) + print(output(line_count, word_count, char_count, path, args)) +else: + line_count, word_count, char_count = count_file(args.file) + print(output(line_count, word_count, char_count, args.file, args)) \ No newline at end of file