diff --git a/implement-shell-tools/cat/catFile.py b/implement-shell-tools/cat/catFile.py new file mode 100644 index 000000000..79f31fa0b --- /dev/null +++ b/implement-shell-tools/cat/catFile.py @@ -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() \ No newline at end of file diff --git a/implement-shell-tools/ls/lsFile.py b/implement-shell-tools/ls/lsFile.py new file mode 100644 index 000000000..cb0c03483 --- /dev/null +++ b/implement-shell-tools/ls/lsFile.py @@ -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() + + \ No newline at end of file diff --git a/implement-shell-tools/wc/wcFile.py b/implement-shell-tools/wc/wcFile.py new file mode 100644 index 000000000..cb5ad6b33 --- /dev/null +++ b/implement-shell-tools/wc/wcFile.py @@ -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() +