Day 3 Shell Scripting - #90 Days of DevOps
Day 3 Shell Scripting
TABLE OF CONTENTS
Show more
🤔nbsp;What is Shell Scripting?
At its core, shell scripting is a way to write and execute a series of commands in a script or program. These commands are typically written in a shell language like Bash. Shell scripts empower us to automate, streamline, and orchestrate various tasks within our DevOps workflows. 🤖🎯
💼nbsp;Uses as a DevOps Engineer:
Here are some cool ways we DevOps engineers leverage shell scripting:
1️⃣ Automation, Automation, Automation: 🤖🤖
2️⃣ System Monitoring and Maintenance: 📊🔍
3️⃣ Error Handling and Recovery: 🚨💥
4️⃣ Custom Tools and Utilities: 🛠️🔨
5️⃣ CI/CD Pipelines: 🛣️🚚
6️⃣ Reporting and Logging: 📈📜
By using shell scripts, we boost efficiency, reduce human error, and have more time to focus on the strategic aspects of our DevOps work. It's like having a trusty sidekick 🦸♂️ that handles the repetitive tasks while we tackle the exciting challenges. 🙌🌟
So, what are your favorite shell scripting tricks or use cases in the world of DevOps? Let's share knowledge, insights, and lots of smiley emojis! 😄🤝 #DevOps #ShellScripting #Automation #Efficiency 💡🔗
🔒nbsp;Step-by-Step Guide: Creating Backups in Shell Scriptsnbsp;📂📦
🚀nbsp;Mastering Basic Shell Scripting Commandsnbsp;📜🔧
Are you ready to embark on your journey into the world of shell scripting? 🐚✨ In this blog, we'll explore some fundamental shell commands and provide practical examples to get you started on your scripting adventure.
1. echo - Lets Start Talking
The echo command is like your script's voice. It displays messages or variables to the terminal.
Example:
COPY
COPY
bashCopy codeecho "Hello, World!"
2. if - Conditional Execution
The if statement allows you to execute commands conditionally based on the result of a test.
Example:
COPY
COPY
bashCopy codeif [ $age -ge 18 ]; then
echo "You are an adult."
fi
3. for - Looping Like a Pro
With the for loop, you can iterate through a list of items.
Example:
COPY
COPY
bashCopy codefruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"; do
echo "I like $fruit."
done
4. while - Repeated Action
The while loop allows you to execute a set of commands repeatedly as long as a condition is true.
Example:
COPY
COPY
bashCopy codecount=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
5. read - User Interaction
The read command lets you read input from the user.
Example:
COPY
COPY
bashCopy codeecho "Enter your name: "
read name
echo "Hello, $name!"
6. grep - Searching for Patterns
Use the grep command to search for patterns in text.
Recommended by LinkedIn
Example:
COPY
COPY
bashCopy codecat file.txt | grep "keyword"
7. sed - Text Manipulation Magic
The sed command is a stream editor for text manipulation.
Example:
COPY
COPY
bashCopy codesed 's/old_string/new_string/g' input.txt > output.txt
8. awk - Text Processing Wizardry
awk is a text processing tool for data extraction and reporting.
Example:
COPY
COPY
bashCopy codecat data.txt | awk '{print $1, $3}'
9. cut - Trimming Text
Use cut to remove sections from lines of files.
Example:
COPY
COPY
bashCopy codecut -d "," -f 1,3 file.csv
10. mv - Moving and Renaming
The mv command moves or renames files and directories.
Example:
COPY
COPY
bashCopy codemv file.txt new_location/
mv old_name.txt new_name.txt
11. rm - Removing Files
The rm command deletes files or directories.
Example:
COPY
COPY
bashCopy coderm file.txt
rm -r directory/
12. touch - Creating Empty Files
Create empty files using the touch command.
Example:
COPY
COPY
bashCopy codetouch new_file.txt
13. chmod - Changing Permissions
The chmod command is for changing file permissions.
Example:
COPY
COPY
bashCopy codechmod +x script.sh
14. cp - Copying Files and Directories
cp is used for copying files and directories.
Example:
COPY
COPY
bashCopy codecp file.txt backup/
15. mkdir - Creating Directories
Use mkdir to create directories.
Example:
COPY
COPY
bashCopy codemkdir new_directory
With these basic shell commands in your toolkit, you're well on your way to becoming a shell scripting pro. 🌟 Whether you're automating tasks, processing data, or managing files, the possibilities are endless! 💡📦🔍