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

def main():
argv = sys.argv[1:]

dash = [arg for arg in argv if arg.startswith('-')]
file_paths = [arg for arg in argv if not arg.startswith('-')]

show_line_numbers = '-n' in dash
show_non_blank = '-b' in dash

line_counter = 1

for file_path in file_paths:
try:
with open(file_path, 'r', encoding='utf-8') as fs:
content = fs.read()
except (FileNotFoundError, IsADirectoryError):
print(f"cat: {file_path}: No file or directory exists", file=sys.stderr)
sys.exit(1)

lines = content.split('\n')

if lines and lines[-1] == '':
lines.pop()

for line in lines:
is_blank = line.strip() == ''

if show_non_blank:
if is_blank:
print('')
else:
print(f"{str(line_counter).rjust(6)}\t{line}")
line_counter += 1
elif show_line_numbers:
print(f"{str(line_counter).rjust(6)}\t{line}")
line_counter += 1
else:
print(line)

if __name__ == "__main__":
main()
31 changes: 31 additions & 0 deletions implement-shell-tools/ls/lsFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
import os

def main():
argv = sys.argv[1:]

dash = [arg for arg in argv if arg.startswith('-')]
paths = [arg for arg in argv if not arg.startswith('-')]

show_all = '-a' in dash

target_dir = paths[0] if paths else '.'

try:
entries = os.listdir(target_dir)
except FileNotFoundError:
print(f"ls: {target_dir}: No such file or directory", file=sys.stderr)
sys.exit(1)

if show_all:
result = ['.', '..'] + entries
else:
result = [e for e in entries if not e.startswith('.')]

for entry in result:
print(entry)

if __name__ == "__main__":
main()


55 changes: 55 additions & 0 deletions implement-shell-tools/wc/wcFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import sys
import os

def main():
argv = sys.argv[1:]

dash = [arg for arg in argv if arg.startswith('-')]
file_paths = [arg for arg in argv if not arg.startswith('-')]

for_lines = '-l' in dash
for_words = '-w' in dash
for_bytes = '-c' in dash

total_lines = 0
total_words = 0
total_bytes = 0

for file_path in file_paths:
try:
with open(file_path, 'r', encoding='utf-8') as fs:
content = fs.read()
except (FileNotFoundError, IsADirectoryError):
print(f"wc: {file_path}: No file or directory exists", file=sys.stderr)
sys.exit(1)

lines = len(content.split('\n')) - 1
words = 0 if content.strip() == '' else len(content.strip().split())
bytes_count = len(content.encode('utf-8'))

total_lines += lines
total_words += words
total_bytes += bytes_count

if for_lines:
print(f"{str(lines).rjust(8)} {file_path}")
elif for_words:
print(f"{str(words).rjust(8)} {file_path}")
elif for_bytes:
print(f"{str(bytes_count).rjust(8)} {file_path}")
else:
print(f"{str(lines).rjust(8)} {str(words).rjust(8)} {str(bytes_count).rjust(8)} {file_path}")

if len(file_paths) > 1:
if for_lines:
print(f"{str(total_lines).rjust(8)} total")
elif for_words:
print(f"{str(total_words).rjust(8)} total")
elif for_bytes:
print(f"{str(total_bytes).rjust(8)} total")
else:
print(f"{str(total_lines).rjust(8)} {str(total_words).rjust(8)} {str(total_bytes).rjust(8)} total")

if __name__ == "__main__":
main()

Loading