Skip to content

mAHA6-STAR/learningShellscript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

109 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒŸ My Shell Scripting Learning Journey ๐Ÿ’ปโœจ

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. ๐Ÿš€

๐Ÿ› ๏ธ Backup Script Debugging Journey ๐Ÿš€

Scenario

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


Debugging Process ๐Ÿž

  1. 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
      
  2. Error in Crontab โŒ

    • Crontab output:
      Jun 5 15:16:01 CROND[16399]: CMDOUT (/bin/sh: line 1: sudo: command not found)
      
  3. Analysis ๐Ÿ”

    • PATH Issue: Crontab uses a minimal environment. The echo $PATH command revealed that /usr/local/bin was missing from the crontab PATH:
      CMDOUT (/usr/bin:/bin)
      
  4. Solution ๐Ÿ› ๏ธ

    • Moved the script to /usr/bin for 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
      

Key Learnings ๐Ÿ’ก

  • Crontab Environment

    • Default PATH for crontab is minimal: /usr/bin:/bin.
    • Commands/scripts must be placed in directories accessible within this path.
  • Path Debugging

    • Use echo $PATH to 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.
  • Conversion to Command

    • Scripts in /usr/local/bin may not work in crontab.
    • Moving the script to /usr/bin ensures compatibility:
      sudo cp script.sh /usr/bin/command-name
      sudo chmod +x /usr/bin/command-name
  • Using visudo for PATH Modification

    • Add /usr/local/bin to secure_path in sudo settings:
      sudo visudo
      Add:
      Defaults secure_path = /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
      

Final Working Crontab ๐Ÿ•’

  • Schedule the backup to run daily at 3:00 AM:

Outcome ๐ŸŽ‰

  • The script successfully creates backups and cleans up old files.
  • Logs are updated dynamically, and the crontab runs seamlessly without errors.

Key Commands Used ๐Ÿ› ๏ธ

sudo cp [21-backup.sh](http://_vscodecontentref_/1) /usr/bin/backup
sudo chmod +x /usr/bin/backup
sudo visudo
echo $PATH

๐Ÿ’ก Handling Variables Without Quotes

Mistake: 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 $FILES in 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"

๐Ÿ›ฃ๏ธ Roadmap to Learning Shell Scripting ๐Ÿงญ

๐ŸŸข Phase 1: Basics of Shell Scripting ๐Ÿฃ

Starting with the fundamentals, these scripts introduce essential concepts:

05-input.sh ๐Ÿ“

Read user input interactively.

Key Concepts: read, input validation.

#06-timestamp.sh ๐Ÿ•’

Display the current date and time.

Key Concepts: date.

07-arithmetic-op.sh โž•โž–โœ–๏ธโž—

Perform basic arithmetic operations.

Key Concepts: expr, let.

๐ŸŸก Phase 2: Intermediate Concepts ๐ŸŒŸ

Building on the basics, this phase dives into loops, arrays, and conditions:

08-array.sh ๐Ÿงฎ

Learn how to work with arrays.

Key Concepts: Array declaration, accessing elements.

10-compare.sh ๐Ÿง

Compare variables and make decisions.

Key Concepts: if, -eq, -ne.

04-arguments.sh ๐ŸŽฏ

Pass arguments to scripts.

Key Concepts: $1, $@.

17-loop.sh ๐Ÿ”

Master loops for repetitive tasks.

Key Concepts: while, for.

09-sv.sh ๐Ÿ”‘ Understanding Special Variables

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  

๐ŸŸ  Phase 3: Advanced Scripting Techniques ๐Ÿง™โ€โ™‚๏ธโœจ

Explore more powerful features of shell scripting, including functions, logging, and colors!

12-function.sh ๐Ÿ› ๏ธ

Reusable functions for modular code.

Key Concepts: Defining functions, returning values.

15-log-fun-col.sh ๐ŸŒˆ๐Ÿ“œ

Add colorful logging to your scripts.

Key Concepts: tee, ANSI color codes.

20-delete-old-logs.sh ๐Ÿ—‘๏ธ

Automate log cleanup.

Key Concepts: find, mtime.

21-backup.sh ๐Ÿ’พ

Create backups with scheduled tasks.

Key Concepts: crontab, tar archives.

๐Ÿ”ด Phase 4: Real-World Applications ๐ŸŒ๐Ÿš€

Time to apply the skills to real-world scenarios:

robhoshop.sh ๐Ÿค–

Automate cloud instance creation.

Key Concepts: EC2 setup, scripting for infrastructure.

22-disk-usage.sh ๐Ÿ“Š

Monitor and manage disk usage.

Key Concepts: df, du.

srivenkata.sh ๐ŸŽ‰

โœจ My First Script! โœจ

This script was my first-ever script, where I printed my first message:


echo "hi svr"

Key Concepts: Basics of echo.

๐Ÿ’ก 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!

๐Ÿ› ๏ธ How to Run the Scripts ๐Ÿš€

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.

๐ŸŒŸ My Top Picks ๐Ÿ’–

โœจ 15-log-fun-col.sh: Fun with colors and logs! ๐ŸŒˆ

โœจ robhoshop.sh: A real-world automation adventure. ๐Ÿค–

โœจ srivenkata.sh: My nostalgic first print script. ๐ŸŽ‰

๐Ÿ› ๏ธ Related Repos & Continuous Learning ๐Ÿ“šโœจ

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. ๐ŸŒโš™๏ธ

๐Ÿค Credits & Connect ๐Ÿ’ฌโค๏ธ

Inspired by cloud-native, production-grade DevOps workflows and automation excellence.
Crafted with passion and dedication by Mahalakshmi ๐Ÿ‘ฉโ€๐Ÿ’ปโœจ


๐ŸŒฑ Final Note ๐Ÿš€๐ŸŒŸ

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! ๐ŸŽ‰๐Ÿš๐Ÿšฆ

About

This repository documents a comprehensive journey into shell scripting, ranging from basic concepts to real-world automation applications. It includes scripts for file management, system monitoring, infrastructure automation, and colorful logging techniques. Each script represents a step forward in learning and implementing shell scripting solution

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors