Day 4 :Linux shell scripting
What is Kernel?
A kernel is the core part of an operating system, acting like a bridge between the hardware (your computer's physical components) and the software (applications that you use). It allocates CPU, memory, and storage to programs, ensures smooth communication between applications and hardware, and allows multiple programs to run simultaneously without conflict.
What is Shell?
A shell is a program that acts as a way for you to communicate with the operating system. The shell takes commands from users in a readable form and translates them into instructions that the kernel can process. It acts as a command interpreter, executing commands from input devices like keyboards or from files. The shell starts running when a user logs in or opens a terminal.
What is Linux Shell Scripting?
Linux shell scripting is the process of writing a series of commands in a text file to automate tasks on a Linux system. These scripts are executed by the shell, which interprets and runs the commands one by one.
A simple shell script might look like this:
Copy
Copy
#!/bin/bash
echo "Starting backup..."
cp /home/user/documents/* /backup
echo "Backup completed!"
This script copies all files from a folder to a backup location and shows messages during the process.
Task 1: Explain in your own words and with examples what Shell Scripting means for DevOps.
Shell scripting in DevOps is a powerful way to automate repetitive tasks and streamline system operations. DevOps engineers often work on tasks like software deployment, server configuration, monitoring, and backups. Shell scripting helps by writing these tasks into scripts that can be executed automatically, saving time and reducing the chances of errors from manual input.
Steps to Automate Server Setup Through a Script
Create a New Script File:
- Open the terminal and create a new script file using the nano text editor:
Copy
Copy
nano setup_web_server.sh
Copy and Paste the Script Code:
In the nano editor, copy and paste the following script code:
Save the Script:
- After pasting the code, save the file in nano by pressing Ctrl + O, then hit Enter to confirm. Exit nano by pressing Ctrl + X.
Make the Script Executable:
- Change the permissions of the script to make it executable:
Copy
Copy
chmod +x setup_web_server.sh
Run the Script:
- Execute the script to set up the web server:
Copy
Copy
./setup_web_server.sh
Output:-
Task 2: What is #!/bin/bash? Can we write #!/bin/sh as well?
What is #!/bin/bash?
Shebang (#!): Indicates the script's interpreter.
/bin/bash: Specifies the Bash shell, which supports advanced features like arrays and functions.
Can We Use #!/bin/sh?
Yes, you can.
- #!/bin/sh: Points to the Bourne shell (or a compatible shell). It’s more portable but may lack some Bash features.
When to Use Each
Use #!/bin/bash: When using Bash-specific features or syntax.
Use #!/bin/sh: For maximum portability across Unix-like systems.
Task 3: Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.
Shell Script:-
Script Output:-
Task 4: Write a Shell Script that takes user input, input from arguments, and prints the variables.
Shell Script:-
Output:-
Task 5: Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.
Shell Script:-
Output:-
Subscribe to my newsletter
Read articles from Rohit Kumar Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rohit Kumar Yadav
Rohit Kumar Yadav
my self Rohit kumar yadav , I am tech learner . i am always curious about to learn new technology ,I have hard working capacity with smart work, i have good time management skill , and leadership. i have good experience in marketing industry.