diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..98b8b753 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,30 @@ +import argparse +parser = argparse.ArgumentParser( + prog="my-cat", + description="Simple cat clone with -n and -b options", +) + +counterNumber = 1 +parser.add_argument("-n", action="store_true", help="number all lines") +parser.add_argument("-b", action="store_true", help="The character to search for" ) +parser.add_argument("path", nargs="+", help="The file to search") + +args = parser.parse_args() + +for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + + if args.b: + for i in range(len(arrText )): + if arrText[i].strip() != "": + print(counterNumber,arrText[i]) + counterNumber += 1 + + elif args.n: + for i in range(len(arrText )): + print(counterNumber, arrText[i]) + counterNumber += 1 + else: + print(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..b5a9c9ab --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,39 @@ +import argparse +import os + +def show_unhidden_files(listDir): + for file in listDir: + if not file.startswith('.'): + print(file) +def show_all_files(listDir): + for file in listDir: + print(file) + + +parser = argparse.ArgumentParser( + prog="my-ls", + description="Simple ls clone with -a and -l options", +) + +parser.add_argument("-a", action="store_true", help="include hidden files") +parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format") +parser.add_argument("path", nargs="?", default=".", help="The directory to list") +args = parser.parse_args() + + + + + +fn = args.path +listDir = os.listdir(fn) +if fn!="": + if args.a and args.one: + show_all_files(listDir) + elif args.a: + show_all_files(listDir) + elif args.one: + show_unhidden_files(listDir) + + else: + show_unhidden_files(listDir) + diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 00000000..8e139ddc --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,66 @@ +import argparse +parser = argparse.ArgumentParser( + prog="my-wc", + description="Simple wc clone with -l and -w options", +) +lineCounter = 0 +wordCounter = 0 +charCounter = 0 +parser.add_argument("-l", action="store_true", help="count lines") +parser.add_argument("-w", action="store_true", help="count words") +parser.add_argument("-c", action="store_true", help="count characters") +parser.add_argument("path", nargs="+", default=".", help="The file to count") +args = parser.parse_args() + +if not args.l and not args.w and not args.c: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + arrWords = content.split() + wordCounter += len(arrWords) + charCounter += len(content) + print("Line count:", lineCounter,"lines") + print("Word count:", wordCounter,"words") + print("Character count:", charCounter,"characters") +elif args.l and args.w: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + arrWords = content.split() + wordCounter += len(arrWords) + print("Line count:", lineCounter,"lines") + print("Word count:", wordCounter,"words") +elif args.l and args.c: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + charCounter += len(content) + print("Line count:", lineCounter,"lines") + print("Character count:", charCounter,"characters") +elif args.l: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + print("Line count:", lineCounter,"lines") + +elif args.w: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split() + wordCounter += len(arrText) + print("Word count:", wordCounter,"words") +elif args.c: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + charCounter += len(content) + print("Character count:", charCounter,"characters") \ No newline at end of file