Skip to content

Commit bef6924

Browse files
London | 26-SDC-Mar | Beko | Sprint 4 | Implement Shell Tools
1 parent 4350f48 commit bef6924

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
import os
3+
4+
def main():
5+
argv = sys.argv[1:]
6+
7+
dash = [arg for arg in argv if arg.startswith('-')]
8+
file_paths = [arg for arg in argv if not arg.startswith('-')]
9+
10+
show_line_numbers = '-n' in dash
11+
show_non_blank = '-b' in dash
12+
13+
line_counter = 1
14+
15+
for file_path in file_paths:
16+
try:
17+
with open(file_path, 'r', encoding='utf-8') as fs:
18+
content = fs.read()
19+
except (FileNotFoundError, IsADirectoryError):
20+
print(f"cat: {file_path}: No file or directory exists", file=sys.stderr)
21+
sys.exit(1)
22+
23+
lines = content.split('\n')
24+
25+
if lines and lines[-1] == '':
26+
lines.pop()
27+
28+
for line in lines:
29+
is_blank = line.strip() == ''
30+
31+
if show_non_blank:
32+
if is_blank:
33+
print('')
34+
else:
35+
print(f"{str(line_counter).rjust(6)}\t{line}")
36+
line_counter += 1
37+
elif show_line_numbers:
38+
print(f"{str(line_counter).rjust(6)}\t{line}")
39+
line_counter += 1
40+
else:
41+
print(line)
42+
43+
if __name__ == "__main__":
44+
main()

implement-shell-tools/ls/lsFile.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
import os
3+
4+
def main():
5+
argv = sys.argv[1:]
6+
7+
dash = [arg for arg in argv if arg.startswith('-')]
8+
paths = [arg for arg in argv if not arg.startswith('-')]
9+
10+
show_all = '-a' in dash
11+
12+
target_dir = paths[0] if paths else '.'
13+
14+
try:
15+
entries = os.listdir(target_dir)
16+
except FileNotFoundError:
17+
print(f"ls: {target_dir}: No such file or directory", file=sys.stderr)
18+
sys.exit(1)
19+
20+
if show_all:
21+
result = ['.', '..'] + entries
22+
else:
23+
result = [e for e in entries if not e.startswith('.')]
24+
25+
for entry in result:
26+
print(entry)
27+
28+
if __name__ == "__main__":
29+
main()
30+
31+

implement-shell-tools/wc/wcFile.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sys
2+
import os
3+
4+
def main():
5+
argv = sys.argv[1:]
6+
7+
dash = [arg for arg in argv if arg.startswith('-')]
8+
file_paths = [arg for arg in argv if not arg.startswith('-')]
9+
10+
for_lines = '-l' in dash
11+
for_words = '-w' in dash
12+
for_bytes = '-c' in dash
13+
14+
total_lines = 0
15+
total_words = 0
16+
total_bytes = 0
17+
18+
for file_path in file_paths:
19+
try:
20+
with open(file_path, 'r', encoding='utf-8') as fs:
21+
content = fs.read()
22+
except (FileNotFoundError, IsADirectoryError):
23+
print(f"wc: {file_path}: No file or directory exists", file=sys.stderr)
24+
sys.exit(1)
25+
26+
lines = len(content.split('\n')) - 1
27+
words = 0 if content.strip() == '' else len(content.strip().split())
28+
bytes_count = len(content.encode('utf-8'))
29+
30+
total_lines += lines
31+
total_words += words
32+
total_bytes += bytes_count
33+
34+
if for_lines:
35+
print(f"{str(lines).rjust(8)} {file_path}")
36+
elif for_words:
37+
print(f"{str(words).rjust(8)} {file_path}")
38+
elif for_bytes:
39+
print(f"{str(bytes_count).rjust(8)} {file_path}")
40+
else:
41+
print(f"{str(lines).rjust(8)} {str(words).rjust(8)} {str(bytes_count).rjust(8)} {file_path}")
42+
43+
if len(file_paths) > 1:
44+
if for_lines:
45+
print(f"{str(total_lines).rjust(8)} total")
46+
elif for_words:
47+
print(f"{str(total_words).rjust(8)} total")
48+
elif for_bytes:
49+
print(f"{str(total_bytes).rjust(8)} total")
50+
else:
51+
print(f"{str(total_lines).rjust(8)} {str(total_words).rjust(8)} {str(total_bytes).rjust(8)} total")
52+
53+
if __name__ == "__main__":
54+
main()
55+

0 commit comments

Comments
 (0)