-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from Bhavin213/master
Day 11 Task Answers.
- Loading branch information
Showing
16 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Day 11 Answers: Error Handling in Shell Scripting | ||
|
||
## Learning Objectives | ||
Understanding how to handle errors in shell scripts is crucial for creating robust and reliable scripts. Today, you'll learn how to use various techniques to handle errors effectively in your bash scripts. | ||
|
||
## Topics to Cover | ||
1. **Understanding Exit Status**: Every command returns an exit status (0 for success and non-zero for failure). Learn how to check and use exit statuses. | ||
2. **Using `if` Statements for Error Checking**: Learn how to use `if` statements to handle errors. | ||
3. **Using `trap` for Cleanup**: Understand how to use the `trap` command to handle unexpected errors and perform cleanup. | ||
4. **Redirecting Errors**: Learn how to redirect errors to a file or `/dev/null`. | ||
5. **Creating Custom Error Messages**: Understand how to create meaningful error messages for debugging and user information. | ||
|
||
## Tasks with Answers | ||
|
||
### 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. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day11/image/task1.png) | ||
|
||
### 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. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day11/image/task2.png) | ||
|
||
### 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. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day11/image/task3.png) | ||
|
||
### 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`. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day11/image/task4.png) | ||
|
||
### 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. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day11/image/task5.png) | ||
|
||
- **I also intentionally created an error by not creating the file, so it showed me this error. I did this for reference.** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day11/image/task5ka1.png) | ||
|
||
## Example Scripts | ||
|
||
### Example 1: Checking Exit Status | ||
```bash | ||
#!/bin/bash | ||
mkdir /tmp/mydir | ||
if [ $? -ne 0 ]; then | ||
echo "Failed to create directory /tmp/mydir" | ||
fi | ||
``` | ||
|
||
### Example 2: Trap | ||
```bash | ||
#!/bin/bash | ||
tempfile=$(mktemp) | ||
trap "rm -f $tempfile" EXIT | ||
|
||
echo "This is a temporary file." > $tempfile | ||
cat $tempfile | ||
# Simulate an error | ||
exit 1 | ||
``` | ||
|
||
### Example 3: Redirecting Errors | ||
```bash | ||
#!/bin/bash | ||
cat non_existent_file.txt 2> error.log | ||
``` | ||
|
||
### Example 4: Custom Error Messages | ||
```bash | ||
#!/bin/bash | ||
mkdir /tmp/mydir | ||
if [ $? -ne 0 ]; then | ||
echo "Error: Directory /tmp/mydir could not be created. Check if you have the necessary permissions." | ||
fi | ||
``` | ||
|
||
[LinkedIn](https://www.linkedin.com/in/bhavin-savaliya/) |
Binary file added
BIN
+50.1 KB
2024/day12/image/connect_your_local_repository_to_the_repository_on_github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Day 12 Answers: Deep Dive in Git & GitHub for DevOps Engineers | ||
|
||
## Find the answers by your understandings (Shouldn't be copied from the internet & use hand-made diagrams) of the questions below and write a blog on it. | ||
|
||
1. What is Git and why is it important? | ||
- **Git** is a distributed version control system that allows multiple developers to work on a project simultaneously without overwriting each other's changes. It helps track changes in source code during software development, enabling collaboration, version control, and efficient management of code changes. | ||
|
||
**Importance of Git:** | ||
- **Version Control:** Keeps track of changes, allowing you to revert to previous versions if needed. | ||
- **Collaboration:** Multiple developers can work on the same project simultaneously. | ||
- **Branching:** Allows you to work on different features or fixes in isolation. | ||
- **Backup::** Acts as a backup of your codebase. | ||
|
||
2. What is the difference between Main Branch and Master Branch? | ||
- Traditionally, **master** was the default branch name in Git repositories. However, many communities have moved to using **main** as the default branch name to be more inclusive and avoid potentially offensive terminology. | ||
|
||
- Main Branch vs. Master Branch: | ||
- **Main Branch:** The new default branch name used in many modern repositories. | ||
- **Master Branch:** The traditional default branch name used in older repositories. | ||
|
||
The traditional default branch name used in older repositories. | ||
|
||
|
||
3. Can you explain the difference between Git and GitHub? | ||
- **Git** is a version control system, while **GitHub** is a web-based platform that uses Git for version control and adds collaboration features like pull requests, issue tracking, and project management. | ||
- Git: | ||
- Command-line tool. | ||
- Manages local repositories. | ||
- GitHub: | ||
- Hosting service for Git repositories. | ||
- Adds collaboration tools and user interfaces. | ||
|
||
4. How do you create a new repository on GitHub? | ||
1. Go to GitHub. | ||
2. Click on the + icon in the top right corner. | ||
3. Select New repository. | ||
4. Enter a repository name (e.g., "DevOps"). | ||
5. Click Create repository. | ||
|
||
5. What is the difference between a local & remote repository? How to connect local to remote? | ||
- Local Repository: | ||
- Stored on your local machine. | ||
- Contains your working directory and Git database. | ||
- Remote Repository: | ||
- Hosted on a server (e.g., GitHub). | ||
- Allows collaboration with other developers. | ||
- Connecting Local to Remote: | ||
1. Initialize a local repository: `git init` | ||
2. Add a remote: `git remote add origin <URL>` | ||
|
||
## Tasks with Answers | ||
|
||
### Task 1: | ||
- Set your user name and email address, which will be associated with your commits. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/set_user_name_and_email_address.png) | ||
|
||
### Task 2: | ||
- Create a repository named "DevOps" on GitHub. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/create_a_new_repository.png) | ||
|
||
- Connect your local repository to the repository on GitHub. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/connect_your_local_repository_to_the_repository_on_github.png) | ||
|
||
- Create a new file in Devops/Git/Day-12.txt & add some content to it. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/create_a_new_file.png) | ||
|
||
- Push your local commits to the repository on GitHub. | ||
|
||
**Answer** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/push_repository.png) | ||
|
||
**After that if you check it on GitHub then it's output will look like this** | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/gitui.png) | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/gitui1.png) | ||
|
||
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day12/image/gitui2.png) | ||
|
||
|
||
[LinkedIn](https://www.linkedin.com/in/bhavin-savaliya/) |