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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.venv
41 changes: 41 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys

show_all_numbers = "-n" in sys.argv # this check will return true if found the flag
show_non_blank_numbers = "-b" in sys.argv # this check will return true if found the flag

# 2. using filter get only the filenames everything except the script name and flags
files = [arg for arg in sys.argv[1:] if arg not in ["-n", "-b"]]

if not files:
print("Usage: python3 cat.py [-n] [-b] <filenames>")
sys.exit()

line_count = 1
# 3. Process each file
for filename in files:
try:
with open(filename, "r") as file:
for line in file:
# Logic for -b
if show_non_blank_numbers:
if line.strip(): # If line is not empty
print(f"{line_count:>6}\t{line}", end="")
line_count += 1
else:
# Standard cat no flags
print(line, end="")

# Logic for -n
elif show_all_numbers:
print(f"{line_count:>6}\t{line}", end="")
line_count += 1

# Standard cat no flags
else:
print(line, end="")

# throw clear errors
except FileNotFoundError:
print(f"Error: {filename}: No such file or directory")
except IsADirectoryError:
print(f"Error: {filename}: Is a directory")
45 changes: 45 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import sys

# assign flags
show_all = "-a" in sys.argv
one_column = "-1" in sys.argv

# Argument Filtering
args = [arg for arg in sys.argv[1:] if not arg.startswith("-")]
# if no path given we use the current path
path = args[0] if args else "."

try:
# Get directory contents
entries = os.listdir(path)

# Handle the -a flag
if show_all:
entries.extend([".", ".."])

# 5. Sort alphabetically, should used in ls.js as well
entries.sort()

# Printing Logic
for entry in entries:
# Skip hidden files unless -a is passed
if not show_all and entry.startswith("."):
continue

if one_column:
# -1 flag: Print vertically
print(entry)
else:
# Standard: Print horizontally with spaces
print(entry, end=" ")

# newline only if we not print horizontally
if not one_column:
print()

except FileNotFoundError:
print(f"ls: cannot access '{path}': No such file or directory")
except NotADirectoryError:
# if a file ls just prints the filename
print(path)
54 changes: 54 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import os

# Flag Detection
show_lines = "-l" in sys.argv
show_words = "-w" in sys.argv
show_chars = "-c" in sys.argv

# If no flags then show all.
show_all = not (show_lines or show_words or show_chars)

# Filter files excluding flags
files = [arg for arg in sys.argv[1:] if not arg.startswith("-")]

# Total counters
total_l, total_w, total_c = 0, 0, 0

for filename in files:
try:
with open(filename, 'r') as f:
lines = f.readlines()

# files calculation
l_count = len(lines)
w_count = sum(len(line.split()) for line in lines)
# os.path.getsize for bytes, in .js I used const bytes = Buffer.byteLength(content);
c_count = os.path.getsize(filename)

# Update totals
total_l += l_count
total_w += w_count
total_c += c_count

# Printing Logic
output = []
if show_lines or show_all: output.append(f"{l_count:>8}")
if show_words or show_all: output.append(f"{w_count:>8}")
if show_chars or show_all: output.append(f"{c_count:>8}")

print(f"{''.join(output)} {filename}")

except FileNotFoundError:
print(f"wc: {filename}: No such file or directory")
except IsADirectoryError:
print(f"wc: {filename}: Is a directory")
print(f"{'0':>8} {'0':>8} {'0':>8} {filename}")

# Print Total if there were multiple files
if len(files) > 1:
output_total = []
if show_lines or show_all: output_total.append(f"{total_l:>8}")
if show_words or show_all: output_total.append(f"{total_w:>8}")
if show_chars or show_all: output_total.append(f"{total_c:>8}")
print(f"{''.join(output_total)} total")
Loading