Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions implement-shell-tools/cat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv/
33 changes: 33 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -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)


20 changes: 20 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -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))
66 changes: 66 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -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))