Day 4: Basic Linux Shell Scripting for DevOps engineers

Hi, I’m Rakshita. A Cloud, DevOps, AI, and Python enthusiast passionate about learning and simplifying technology for others. I love exploring how modern tools and automation can make systems smarter and more efficient. Here, I write about: ☁️ Cloud & DevOps practices 🤖 AI in the world of automation 🐍 Python for real-world problem-solving 💡 Growth, consistency, and the learner’s mindset My goal is to bridge the gap between learning and doing, and help others grow confidently in the evolving tech landscape.
What is Linux Shell Scripting?
Shell scripting is a text file with a list of commands that instructs an operating system to perform certain tasks. A shell is an interface that interprets, processes, and executes these commands from the shell script. It can be particularly helpful to automate repetitive tasks, helping to save time and reduce human error. Each shell script is saved with .sh file extension e.g., myscript.sh.
What Shell Scripting means for DevOps?
Shell Scripting in DevOps means using scripts to automate repetitive tasks and make system management easier and more efficient. This helps DevOps engineers save time and ensure tasks are done consistently.
What Does Shell Scripting Do in DevOps?
Automates Tasks: Instead of doing tasks manually, you write a script to do them for you.
Saves Time: Once a script is written, you can run it anytime to perform the same task quickly.
Reduces Errors: Automating tasks minimizes the chances of making mistakes.
Ensures Consistency: Scripts perform tasks the same way every time, ensuring uniform results.
What is
#!/bin/bash? Can we write#!/bin/shas well?
The line "#! /bin/bash" is called a shebang. It indicates the path to the interpreter that should be used to execute the script, in this case, "/bin/bash" for the Bash shell. It ensures that the script is interpreted by the specified shell, in this case, Bash. This line is placed at the beginning of a script to define the interpreter for proper execution.
Yes, we can write #!/bin/sh, but there are some differences to keep in mind:
Compatibility:
#!/bin/sh scripts are more portable because sh is available on all Unix-like systems.
Some advanced features of Bash may not be available or may behave differently in sh.
Features:
#!/bin/bash gives you access to all Bash-specific features, such as arrays, certain string operations, and more advanced scripting capabilities.
#!/bin/sh ensures that the script uses only the more basic features of the shell language, which can help make the script more portable across different systems.
Write a Shell Script that prints
I will complete #90DaysOfDevOps challenge.
Open a text editor and create a file script.sh:
vim script.sh
Press i, add shebang and the script to be printed:
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge."
To save and exit, press 'esc', ':wq' and 'enter'.
Make the script executable for user/owner only:
chmod 700 script.sh
Run the script:
./script.sh
Write a Shell Script that takes user input, input from arguments, and prints the variables?
Create a arguments.sh file:
vim arguments.sh
Enter the script:
#!/bin/bash
# Take_user_input
echo "Enter your name:"
read user_input
# Input_from_argumemnts
arg1=$1
arg2=$2
# Print_variables
echo "User input: $user_input"
echo "First argument: $arg1"
echo "Second argument: $arg2"
Save and quit the file. Then change file permissions to make it executable by the user:
chmod 700 arguments.sh
Run the script:
./arguments.sh 1 2
Here;
1= argument 1 value.
2= argument 2 value.
Provide an example of an If-Else statement in Shell Scripting by comparing two numbers?
#!/bin/bash
# Define two numbers
num1=5
num2=10
# Compare the numbers
if [ $num1 -gt $num2 ]; then
echo "Number 1 ($num1) is greater than Number 2 ($num2)"
elif [ $num1 -lt $num2 ]; then
echo "Number 1 ($num1) is less than Number 2 ($num2)"
else
echo "Number 1 ($num1) is equal to Number 2 ($num2)"
fi
Run the script.
Conclusion
We learned about basic Linux Shell Scripting and its fundamentals, which are essential to strengthen our foundation in Shell Scripting. We can write and practice more Shell commands and scripting on our own and by taking references from the internet.
Stay tuned for more in my #90daysdevops journey!
Happy Learning!✨




