Skip to content

Commit 6020eef

Browse files
committed
return exit code 1 for any failed file read
1 parent 0591f3f commit 6020eef

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

  • implement-shell-tools/cat

‎implement-shell-tools/cat/cat.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,28 @@ def cat_file(path):
1919
content = f.read()
2020
except FileNotFoundError:
2121
print(f"cat: {path}: No such file or directory", file=sys.stderr)
22-
return
22+
return False
2323
except IsADirectoryError:
2424
print(f"cat: {path}: Is a directory", file=sys.stderr)
25-
return
25+
return False
2626
except PermissionError:
2727
print(f"cat: {path}: Permission denied", file=sys.stderr)
28-
return
28+
return False
2929

3030
print(content)
3131

3232

3333

34+
# cat exits with error code 1 if any file read fails
35+
file_error = False
36+
3437
for path in args.paths:
3538
line_num = 1
36-
cat_file(path)
39+
is_success = cat_file(path)
40+
41+
if not is_success:
42+
file_error = True
43+
44+
# if at any point, file reading failed file error is set to True,
45+
# and program exist with code 1 after all tasks completed
46+
sys.exit(1 if file_error else 0)

0 commit comments

Comments
 (0)