# Day 11 - Task: Error Handling in Shell Scripting

# Tasks

## Task 1: Checking Exit Status

* Write a script that attempts to create a directory and checks if the command was successful. If not, print an error message.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749316841997/26469219-44a7-4053-b221-c4dc392392ee.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749316872586/a17e967b-4de0-4775-b743-d4632b2fb42e.png align="center")

## Task 2: Using `if` Statements for Error Checking

* Modify the script from Task 1 to include more commands (e.g., creating a file inside the directory) and use `if` statements to handle errors at each step.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749316905971/6fa6caae-dbf5-4d1e-8138-4e627ab06a50.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749316931136/673d7f26-e26b-4751-9f21-8c2ebdf3fe8f.png align="center")

## Task 3: Using `trap` for Cleanup

* Write a script that creates a temporary file and sets a `trap` to delete the file if the script exits unexpectedly.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749316960044/3d999c44-70ec-4f2d-be5f-e45ed826f4dd.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749316976481/79813f48-2038-44c5-82c1-e8d8665c7315.png align="center")

## Task 4: Redirecting Errors

* Write a script that tries to read a non-existent file and redirects the error message to a file called `error.log`.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749317082635/b61fd892-4e0e-43fd-ba89-fc91073a03d9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749317097248/409a437c-93aa-42d6-905d-6b921b45c02f.png align="center")

## Task 5: Creating Custom Error Messages

* Modify one of the previous scripts to include custom error messages that provide more context about what went wrong.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749317309821/41ecf348-0691-442f-8462-f6428ce1067e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749317134571/5aea85bf-b518-4794-8bf8-47ea46c891ab.png align="center")

Thank you for Reading:)
