Sunday, March 2, 2025

Linux Shell Scripting: Automate Like a Pro! 🚀

 

Linux Shell Scripting: Automate Like a Pro! 🚀


Now that you’ve mastered the core Linux concepts, it’s time to take the next big leapShell Scripting! 🖥️💡

🔹 Why Learn Shell Scripting?
Shell scripting is a must-have skill for DevOps, automation, system administration, and cloud computing. It helps you:
✅ Automate repetitive tasks
✅ Manage system configurations efficiently
✅ Perform bulk operations in a single command
✅ Improve productivity & minimize human errors




🌍 Shape Your Future with AI & Infinite Knowledge...!!

🌐 Want to Generate Text-to-Voice, Images & Videos? 👉 http://www.ai.skyinfinitetech.com 📚 Read In-Depth Tech & Self-Improvement Blogs 👉 http://www.skyinfinitetech.com ▶ Watch Life-Changing Videos on YouTube 👉 https://www.youtube.com/@SkyInfinite-Learning 🔥 Transform Your Skills, Business & Productivity – Join Us Today! 🔥



1️⃣ What is Shell Scripting?

A shell script is a file containing a series of commands that the Linux shell executes sequentially. Instead of running commands one by one, you can put them in a script and run them automatically!


📌 Basic Structure of a Shell Script:


#!/bin/bash echo "Hello, DevOps World!" # Prints a message
  • #!/bin/bash → Shebang, tells Linux to use the Bash shell
  • echo → Prints text to the screen


🔹 Running a Shell Script:
1️⃣ Create a script file: nano myscript.sh
2️⃣ Make it executable: chmod +x myscript.sh
3️⃣ Run the script: ./myscript.sh



2️⃣ Variables & User Input in Shell Scripts

🔹 Defining Variables:


name="DevOps Engineer" echo "Hello, $name!"


🔹 User Input:


echo "Enter your name:" read user_name echo "Welcome, $user_name!"

🔹 Environment Variables:


echo "Your home directory is: $HOME"


3️⃣ Conditional Statements: Decision Making in Shell


🔹 If-Else Condition:


#!/bin/bash echo "Enter your age:" read age if [ $age -ge 18 ]; then echo "You are eligible to vote!" else echo "Sorry, you are not eligible." fi


🔹 Case Statements:


echo "Enter a number between 1-3:" read num case $num in 1) echo "You chose One!";; 2) echo "You chose Two!";; 3) echo "You chose Three!";; *) echo "Invalid choice!";; esac


4️⃣ Loops: Automating Repetitive Tasks

🔹 For Loop:


for i in 1 2 3 4 5 do echo "Number: $i" done


🔹 While Loop:


count=1 while [ $count -le 5 ] do echo "Iteration: $count" ((count++)) done


🔹 Until Loop:


x=1 until [ $x -gt 5 ] do echo "Number: $x" ((x++)) done


5️⃣ Functions in Shell Scripting

Functions allow code reuse and better script structure.


#!/bin/bash function greet() { echo "Hello, $1! Welcome to Linux Scripting!" } greet "DevOps Engineer"


6️⃣ File Handling in Shell Scripts

🔹 Checking if a file exists:


if [ -f /etc/passwd ]; then echo "File exists!" else echo "File does not exist!" fi


🔹 Reading a file line by line:


while read line; do echo "$line" done < myfile.txt


7️⃣ Process Management in Shell Scripts

🔹 Killing a process:


#!/bin/bash echo "Enter process name to kill:" read pname pkill $pname


🔹 Monitoring CPU & Memory Usage:


echo "Top 5 CPU-consuming processes:" ps -eo pid,comm,%cpu --sort=-%cpu | head -6


8️⃣ Scheduling Jobs with Crontab

Want to run scripts automatically at a scheduled time? Use Crontab!
🔹 Opening Crontab Editor:


crontab -e


🔹 Scheduling a script to run every day at midnight:


0 0 * * * /path/to/script.sh


9️⃣ Real-World Shell Scripting Examples

🔹 Automating Backup Process:


#!/bin/bash tar -czf backup_$(date +%F).tar.gz /home/user/documents echo "Backup completed!"


🔹 Checking Server Uptime & Health:


#!/bin/bash echo "System Uptime:" uptime echo "Disk Usage:" df -h echo "Memory Usage:" free -m

No comments:

Post a Comment

Terraform State Deep Dive: Why it's Crucial and How to Manage It

Terraform State Deep Dive: Why it's Crucial and How to Manage It ...