Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Comments and Explanations to Shell Script for Day 8 of 90DaysOfDevOps Challenge with images #340

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Binary file removed 2024/day03/image/task 1.png
Binary file not shown.
Binary file removed 2024/day03/image/task 10.png
Binary file not shown.
Binary file removed 2024/day03/image/task 11.png
Binary file not shown.
Binary file removed 2024/day03/image/task 12.png
Binary file not shown.
Binary file removed 2024/day03/image/task 2.png
Binary file not shown.
Binary file removed 2024/day03/image/task 3.png
Binary file not shown.
Binary file removed 2024/day03/image/task 4.png
Binary file not shown.
Binary file removed 2024/day03/image/task 5.png
Binary file not shown.
Binary file removed 2024/day03/image/task 6.png
Binary file not shown.
Binary file removed 2024/day03/image/task 66.png
Binary file not shown.
Binary file removed 2024/day03/image/task 7.png
Binary file not shown.
Binary file removed 2024/day03/image/task 8.png
Binary file not shown.
Binary file removed 2024/day03/image/task 9.png
Binary file not shown.
Binary file added 2024/day03/image/task1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task7.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task7.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task8.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/day03/image/task8.2.png
Binary file added 2024/day03/image/task9.png
38 changes: 25 additions & 13 deletions 2024/day03/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,61 @@

Task 1: View the content of a file and display line numbers.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%201.png)
![task1](image/task1.png)

Task 2: Change the access permissions of files to make them readable, writable, and executable by the owner only.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%202.png)
![task2](image/task2.png)

Task 3: Check the last 10 commands you have run.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%203.png)
![task3](image/task3.png)

Task 4: Remove a directory and all its contents.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%204.png)
![task4](image/task4.png)

Task 5: Create a `fruits.txt` file, add content (one fruit per line), and display the content.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%205.png)
![task5](image/task5.png)

Task 6: Add content in `devops.txt` (one in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava. Then, append "Pineapple" to the end of the file.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%206.png)
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%2066.png)
![task6](image/task6.png)

Task 7: Show the first three fruits from the file in reverse order.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%207.png)
Two ways to do :

1. ![task7.1](image/task7.1.png)

2. ![task7.2](image/task7.2.png)


Task 8: Show the bottom three fruits from the file, and then sort them alphabetically.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%208.png)
Two ways to do :

1. ![task8.1](image/task8.1.png)

2. ![task8.2](image/task8.2.png)


Task 9: Create another file `Colors.txt`, add content (one color per line), and display the content.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%209.png)
![task9](image/task9.png)

Task 10: Add content in `Colors.txt` (one in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey. Then, prepend "Yellow" to the beginning of the file.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%2010.png)
![task10](image/task10.png)

Task 11: Find and display the lines that are common between `fruits.txt` and `Colors.txt`.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%2011.png)
![task11](image/task11.png)

Task 12: Count the number of lines, words, and characters in both `fruits.txt` and `Colors.txt`.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day03/image/task%2012.png)
![task12](image/task12.png)


hello world
Binary file removed 2024/day04/image/task 1.png
Diff not rendered.
Binary file removed 2024/day04/image/task 11.png
Diff not rendered.
Binary file removed 2024/day04/image/task 2.png
Diff not rendered.
Binary file removed 2024/day04/image/task 3.png
Diff not rendered.
Binary file removed 2024/day04/image/task 4.png
Diff not rendered.
Binary file removed 2024/day04/image/task 5.png
Diff not rendered.
Binary file added 2024/day04/image/task3.1.png
Binary file added 2024/day04/image/task3.2.png
Binary file added 2024/day04/image/task4.1.png
Binary file added 2024/day04/image/task4.2.png
Binary file added 2024/day04/image/task5.1.png
Binary file added 2024/day04/image/task5.2.png
18 changes: 9 additions & 9 deletions 2024/day04/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
Task 1: Explain in your own words and with examples what Shell Scripting means for DevOps.
- 'Shell Scripting is writing a series of commands in a script file to automate tasks in the Unix/Linux shell. For DevOps, shell scripting is crucial for automating repetitive tasks, managing system configurations, deploying applications, and integrating various tools and processes in a CI/CD pipeline. It enhances efficiency, reduces errors, and saves time.'

Example: Automating server setup
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day04/image/task%201.png)
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day04/image/task%2011.png)

Task 2: What is `#!/bin/bash`? Can we write `#!/bin/sh` as well?
- `#!/bin/bash` is called a "shebang" line. It indicates that the script should be run using the Bash shell.
Comment on lines 5 to 8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add security best practices for shell scripts

Consider adding important security considerations when writing shell scripts:

  • Setting restrictive permissions (chmod 700)
  • Using set -euo pipefail for safer execution
  • Quoting variables to prevent word splitting

- `#!/bin/bash`: Uses Bash as the interpreter. It supports advanced features like arrays, associative arrays, and functions.
- `#!/bin/sh`: Uses the Bourne shell. It’s more POSIX-compliant and is generally compatible with different Unix shells.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day04/image/task%202.png)

Task 3: Write a Shell Script that prints `I will complete #90DaysOfDevOps challenge`.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day04/image/task%203.png)
![task3.1](image/task3.1.png)

![task3.2](image/task3.2.png)

Task 4: Write a Shell Script that takes user input, input from arguments, and prints the variables.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day04/image/task%204.png)
![task4.1](image/task4.1.png)

![task4.2](image/task4.2.png)

Task 5: Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day04/image/task%205.png)
![task5.1](image/task5.1.png)

![task5.2](image/task5.2.png)
Binary file removed 2024/day05/image/task 1-2.png
Diff not rendered.
Binary file removed 2024/day05/image/task 1-3.png
Diff not rendered.
Binary file removed 2024/day05/image/task 1.png
Diff not rendered.
Binary file removed 2024/day05/image/task 2-1.png
Diff not rendered.
Binary file removed 2024/day05/image/task 2.png
Diff not rendered.
Binary file removed 2024/day05/image/task 3-1.png
Diff not rendered.
Binary file removed 2024/day05/image/task 3.png
Diff not rendered.
Binary file removed 2024/day05/image/task 4.png
Diff not rendered.
Binary file added 2024/day05/image/task1.1.png
Binary file added 2024/day05/image/task1.2.png
Binary file added 2024/day05/image/task2.1.png
Binary file added 2024/day05/image/task2.2.png
Binary file added 2024/day05/image/task2.3.png
Binary file added 2024/day05/image/task3.1.png
Binary file added 2024/day05/image/task3.2.png
Binary file added 2024/day05/image/task3.3.png
Binary file added 2024/day05/image/task3.4.png
Binary file added 2024/day05/image/task3.5.png
Binary file added 2024/day05/image/task3.6.png
Binary file added 2024/day05/image/task4.1.png
Binary file added 2024/day05/image/task4.2.png
125 changes: 115 additions & 10 deletions 2024/day05/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,137 @@

**Answer**

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%201.png)
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%201-2.png)
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%201-3.png)
- Creating createDirectories.sh and adding below content :

``` bash

#!/bin/bash

<<Info
Author : Amitabh Soni
Date : 25/11/24
Description : This script takes three arguments and creates a specified number of directories at once.

Example : When executed as ./createDirectories.sh day 1 90, it creates 90 directories as day1, day2, day3, ... day90.
Info

# For loop to create directories iteratively
for i in $(seq $2 $3); do
mkdir "$1$i"
done
Comment on lines +27 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add argument validation to createDirectories.sh

The script needs to validate:

  1. Number of arguments
  2. Whether arguments 2 and 3 are valid numbers
  3. Whether argument 3 is greater than argument 2
+# Validate number of arguments
+if [ "$#" -ne 3 ]; then
+    echo "Error: Three arguments required"
+    echo "Usage: $0 directory_name start_num end_num"
+    exit 1
+fi
+
+# Validate numeric arguments
+if ! [[ "$2" =~ ^[0-9]+$ ]] || ! [[ "$3" =~ ^[0-9]+$ ]]; then
+    echo "Error: Second and third arguments must be numbers"
+    exit 1
+fi
+
+# Validate range
+if [ "$3" -lt "$2" ]; then
+    echo "Error: End number must be greater than start number"
+    exit 1
+fi

# For loop to create directories iteratively
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# For loop to create directories iteratively
for i in $(seq $2 $3); do
mkdir "$1$i"
done
# Validate number of arguments
if [ "$#" -ne 3 ]; then
echo "Error: Three arguments required"
echo "Usage: $0 directory_name start_num end_num"
exit 1
fi
# Validate numeric arguments
if ! [[ "$2" =~ ^[0-9]+$ ]] || ! [[ "$3" =~ ^[0-9]+$ ]]; then
echo "Error: Second and third arguments must be numbers"
exit 1
fi
# Validate range
if [ "$3" -lt "$2" ]; then
echo "Error: End number must be greater than start number"
exit 1
fi
# For loop to create directories iteratively
for i in $(seq $2 $3); do
mkdir "$1$i"
done


```

![task1.1](image/task1.1.png)


- createDirectories.sh

![task1.2](image/task1.2.png)

2. **Create a Script to Backup All Your Work:**
- Backups are an important part of a DevOps Engineer's day-to-day activities. The video in the references will help you understand how a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, nothing is impossible).

**Answer**

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%202.png)
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%202-1.png)

- Creating a `backup.sh` file and executing after adding the below content:

```bash
#!/bin/bash

<<Info
Author : Amitabh Soni
Date : 25/11/25
Description : This script will take backup of any directory or file and store it in the backup directory: /home/ubuntu/Day-05/backup
Info

# Creating a function for backup creation
function create_backup() {
# Timestamp for backup naming
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')

# Target directory where backups are stored
target_dir="/home/ubuntu/Day-05/backup"

# Backup filename
backup_file="${target_dir}/backup_${timestamp}.zip"

# Create zip backup
zip -r "$backup_file" "$1"
Comment on lines +63 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Make backup directory configurable and ensure it exists

The backup directory path should be configurable and the script should ensure it exists before creating backups.

-target_dir="/home/ubuntu/Day-05/backup"
+# Allow override through environment variable
+target_dir="${BACKUP_DIR:-/home/ubuntu/Day-05/backup}"
+
+# Ensure backup directory exists
+mkdir -p "$target_dir" || {
+    echo "Error: Cannot create backup directory"
+    exit 1
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
target_dir="/home/ubuntu/Day-05/backup"
# Backup filename
backup_file="${target_dir}/backup_${timestamp}.zip"
# Create zip backup
zip -r "$backup_file" "$1"
# Allow override through environment variable
target_dir="${BACKUP_DIR:-/home/ubuntu/Day-05/backup}"
# Ensure backup directory exists
mkdir -p "$target_dir" || {
echo "Error: Cannot create backup directory"
exit 1
}
# Backup filename
backup_file="${target_dir}/backup_${timestamp}.zip"
# Create zip backup
zip -r "$backup_file" "$1"


# Checking if the previous command successfully ran or not
if [ $? -eq 0 ]; then
echo "Backup created: ${backup_file}"
else
echo "Error: Failed to create backup."
return 1
fi
}

create_backup "$1"

```

![task2.1](image/task2.1.png)


- backup.sh :

![task2.2](image/task2.2.png)


- Output image :

![task2.3](image/task2.3.png)

3. **Read About Cron and Crontab to Automate the Backup Script:**
- Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit, or delete entries to cron. A crontab file is a user file that holds the scheduling information.

**Answer**

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%203.png)
![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%203-1.png)
- Editing crontab for auto backup :

![task3.1](image/task3.1.png)


- setting a backup of Day-04 in every minute :

![task3.2](image/task3.2.png)


- Ensuring whether it creates backup in one minute or not :

![task3.3](image/task3.3.png)


- checking realtime update in backup dir through 'watch ls' command :

![task3.4](image/task3.4.png)


- It works for every minute :

![task3.5](image/task3.5.png)


- Final check for backup :

![task3.6](image/task3.6.png)

4. **Read About User Management:**
- A user is an entity in a Linux operating system that can manipulate files and perform several other operations. Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.
- Create 2 users and display their usernames.

**Answer**

![image](https://github.com/Bhavin213/90DaysOfDevOps/blob/master/2024/day05/image/task%204.png)
- Creating users and printing there names :

![task4.1](image/task4.1.png)


- Setting up password the new users :

![task4.2](image/task4.2.png)


[LinkedIn](https://www.linkedin.com/in/bhavin-savaliya/).
[LinkedIn](https://www.linkedin.com/in/amitabh-devops/).
Binary file added 2024/day06/image/task1.1.png
Binary file added 2024/day06/image/task1.2.png
Binary file added 2024/day06/image/task1.3.png
Binary file added 2024/day06/image/task1.4.png
Binary file removed 2024/day06/image/task1.png
Diff not rendered.
Binary file added 2024/day06/image/task3.1.png
Binary file removed 2024/day06/image/task3.png
Diff not rendered.
Binary file removed 2024/day06/image/task4-1.png
Diff not rendered.
Binary file added 2024/day06/image/task4.1.1.png
Binary file added 2024/day06/image/task4.1.2.png
Binary file added 2024/day06/image/task4.2.1.png
Binary file removed 2024/day06/image/task4.png
Diff not rendered.
Binary file removed 2024/day06/image/task5-1.png
Diff not rendered.
Binary file removed 2024/day06/image/task5-2.png
Diff not rendered.
Binary file added 2024/day06/image/task5.1.png
Binary file added 2024/day06/image/task5.2.1.png
Binary file added 2024/day06/image/task5.2.2.png
Binary file added 2024/day06/image/task5.3.png
Binary file removed 2024/day06/image/task5.png
Diff not rendered.
Binary file removed 2024/day06/image/task6-1.png
Diff not rendered.
Binary file added 2024/day06/image/task6.1.png
Binary file added 2024/day06/image/task6.2.png
Binary file removed 2024/day06/image/task6.png
Diff not rendered.
Loading