Welcome to my Shell Scripting Learning Journey! ๐ This repository documents my exciting exploration of shell scripting, from the basics to advanced automation projects. Each script in this journey represents a step forward, filled with learnings and aha moments! ๐
๐ Firstly, Let's Look at the Mistakes I Encountered and How I Debugged Them , Before Starting the Roadmap Journey
During my shell scripting journey, I faced issues like scripts working manually but failing in crontab due to environment differencesโespecially with the PATH variable and command locations. ๐ By analyzing error messages, checking environment variables, and adjusting script placement and permissions, I was able to resolve these problems. ๐ ๏ธ After overcoming these challenges, I continued to build my skills, which shaped the roadmap of my learning journey in shell scripting. ๐
Recently, I developed a script to create backups of files and scheduled it in crontab. While the script executed successfully when run manually, it failed with a "command not found" error in crontab.
You can find the script here: 21-backup.sh
For log cleanup, see: 20-delete-old-logs.sh
-
Initial Setup
- The script was created and tested:
sudo sh 21-backup.sh ../source-dir ../dest-dir
- Output:
You are running with root access Files to zip are: ../source-dir/cat.log ../source-dir/catalogue.log ../source-dir/shipping.log
- The script was created and tested:
-
Error in Crontab โ
- Crontab output:
Jun 5 15:16:01 CROND[16399]: CMDOUT (/bin/sh: line 1: sudo: command not found)
- Crontab output:
-
Analysis ๐
- PATH Issue: Crontab uses a minimal environment. The
echo $PATHcommand revealed that/usr/local/binwas missing from the crontab PATH:CMDOUT (/usr/bin:/bin)
- PATH Issue: Crontab uses a minimal environment. The
-
Solution ๐ ๏ธ
- Moved the script to
/usr/binfor better compatibility:sudo cp [21-backup.sh](http://_vscodecontentref_/0) /usr/bin/backup sudo chmod +x /usr/bin/backup
- Updated the crontab to run the command:
* * * * * sudo backup /home/ec2-user/source-dir /home/ec2-user/dest-dir
- Moved the script to
-
Crontab Environment
- Default
PATHfor crontab is minimal:/usr/bin:/bin. - Commands/scripts must be placed in directories accessible within this path.
- Default
-
Path Debugging
- Use
echo $PATHto verify the environment for:- Normal User:
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin - Root User: Same as above.
- Crontab: Minimal paths
/usr/bin:/bin.
- Normal User:
- Use
-
Conversion to Command
- Scripts in
/usr/local/binmay not work in crontab. - Moving the script to
/usr/binensures compatibility:sudo cp script.sh /usr/bin/command-name sudo chmod +x /usr/bin/command-name
- Scripts in
-
Using
visudofor PATH Modification- Add
/usr/local/bintosecure_pathinsudosettings:Add:sudo visudo
Defaults secure_path = /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
- Add
- Schedule the backup to run daily at 3:00 AM:
- The script successfully creates backups and cleans up old files.
- Logs are updated dynamically, and the crontab runs seamlessly without errors.
sudo cp [21-backup.sh](http://_vscodecontentref_/1) /usr/bin/backup
sudo chmod +x /usr/bin/backup
sudo visudo
echo $PATHMistake: When using $FILES, I failed to enclose it in double quotes. This led to a "too many arguments" error when handling filenames with spaces or special characters.
Resolution:
- Enclosed
$FILESin double quotes to ensure it's treated as a single string:if [ ! -z "$FILES" ];
Then improved robustness using tr to replace spaces with newlines:
echo "$FILES" | tr ' ' '\n' | zip -@ "$ZIP_FILE"Starting with the fundamentals, these scripts introduce essential concepts:
Read user input interactively.
Key Concepts: read, input validation.
Display the current date and time.
Key Concepts: date.
Perform basic arithmetic operations.
Key Concepts: expr, let.
Building on the basics, this phase dives into loops, arrays, and conditions:
Learn how to work with arrays.
Key Concepts: Array declaration, accessing elements.
Compare variables and make decisions.
Key Concepts: if, -eq, -ne.
Pass arguments to scripts.
Key Concepts:
Master loops for repetitive tasks.
Key Concepts: while, for.
This script explores the special shell variables that provide runtime information about the script and its environment.
Key Concepts:
$@: All arguments passed to the script.
$#: Number of arguments passed.
$0: Name of the script.
$PWD: Present working directory.
$HOME: User's home directory.
$USER: Username of the script runner.
$$: PID of the script.
$!: PID of the last background command.
Sample Usage:
./09-sv.sh arg1 arg2 arg3
Explore more powerful features of shell scripting, including functions, logging, and colors!
Reusable functions for modular code.
Key Concepts: Defining functions, returning values.
Add colorful logging to your scripts.
Key Concepts: tee, ANSI color codes.
Automate log cleanup.
Key Concepts: find, mtime.
Create backups with scheduled tasks.
Key Concepts: crontab, tar archives.
Time to apply the skills to real-world scenarios:
Automate cloud instance creation.
Key Concepts: EC2 setup, scripting for infrastructure.
Monitor and manage disk usage.
Key Concepts: df, du.
โจ My First Script! โจ
This script was my first-ever script, where I printed my first message:
echo "hi svr"
๐ก Sometimes, a simple "Hello, World!" is all you need to kickstart your journey.
๐ What I Learned Along the Way ๐ ๐ ๏ธ How to automate repetitive tasks using shell commands.
๐ Debugging techniques to troubleshoot errors.
๐ Creating real-world solutions for file management and system monitoring.
โจ Combining creativity with scripting for colorful and dynamic outputs!
Clone the repository:
git clone https://github.com/MAHALAKSHMImahalakshmi/autoShellRoboshop.git
cd autoShellRoboshop
Make the script executable (if not already):
chmod +x <script-name>.sh
Run the script:
sudo sh <script-name>.sh
๐ก Using sudo ensures proper permissions, especially for scripts requiring root access.
Ready to level up your shell scripting and automation game? Explore my key repositories that showcase a progressive journey from basics to cloud-grade automation:
- ๐ LEARN-SHELLSCRIPT-PRACTICE โ Start here! Fundamentals, practice scripts, and troubleshooting gems for shell scripting newbies and pros alike. ๐๐ก
- โ๏ธ Roboshop-Automation-Scripts-AWS โ Jump into real-world, cloud-ready automation scripts that power robust microservices on AWS! ๐๐ฅ
- ๐ค Automated-Roboshop-Setup-AWS.git โ Hands-on integrated infrastructure setup scripts for a seamless Roboshop deployment experience. ๐โ๏ธ
Inspired by cloud-native, production-grade DevOps workflows and automation excellence.
Crafted with passion and dedication by Mahalakshmi ๐ฉโ๐ปโจ
Shell scripting has been a game changer in my DevOps career โ it taught me the true power of automation, modularity, and smart troubleshooting. ๐ช๐ ๏ธ
Letโs continue to build, automate, and innovateโone script at a time!
Join me on this journey, and together weโll unlock greater efficiencies and creative solutions in automation. ๐ฏ๐กโจ
Happy scripting! ๐๐๐ฆ