Advanced Linux Shell Scripting for DevOps Engineers with user management -Day5/90 #90daysofdevops

In today's blog, we will write a bash script to create directories.sh that when the script is executed with three given arguments (one is the directory name and the second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

Example 1: When the script is executed as

./createDirectories.sh day 1 90

then it creates 90 directories as day1 day2 day3 .... day90

Sure! Let's explain the shell script using a day-to-day life example that everyone can relate to planning a week-long vacation.

Imagine you want to plan a vacation from July 15th to July 20th, and you need to figure out which dates fall within that period. Instead of manually checking each date, you can use the shell script as a tool to do that for you.

Here's how you can relate the shell script to planning your vacation:

1. Making the "date_range.sh" Script:

- The "date_range.sh" script is like writing a set of vacation planning instructions on a piece of paper.

2. Understanding the Script:

- The script will help you find all the dates between a start date (e.g., July 15th) and an end date (e.g., July 20th).

3. Using the Script:

- To use the script, you need to open your computer's terminal (imagine it as a helpful tour guide).

- You provide the start date (July 15th) and the end date (July 20th) as arguments to the script.

- Just like telling the tour guide where you want to go and when.

4. Getting the Results:

- The script runs, and your computer (the tour guide) lists all the dates between July 15th and July 20th, which are the days you'll be on vacation.

- This is like your tour guide giving you a detailed itinerary with all the dates of your vacation.

5. Enjoying Your Vacation:

- With the list of dates provided by the script, you now know exactly when your vacation will start and end.

- You can make plans, book hotels, and organize activities for each day of your vacation.

By using the shell script, you save time and effort, just like having a tour guide plan your vacation for you. Instead of manually going through the calendar and figuring out the dates, the script automates the process and gives you the information you need.

Of course, the script can be used for various other purposes as well, not just for vacation planning. It's a versatile tool that can help you with any task that involves working with dates and lists of dates in day-to-day life.

Save the above code in a file named createDirectories.sh, and then give it execute permission using the following command:

Save the above code in a file named createDirectories.sh, and then give it execute permission using the following command:

COPY

chmod 777 createDirectories.sh

you can now execute the script with the desired argument to create the directories.

and then give it execute permission using the following command:

COPY

./creatDirectoriies.sh day 1 8

output:-

root@Episodes:/home/Episodes/script# nano createDirectories.sh
root@Episodes:/home/Episodes/script# ./createDirectories.sh day 2 8
Directories Successfully created
root@Episodes:/home/Episodes/script# ls 
createDirectories.sh day2 day3 day4 day5 day6 day7 day8 disk_space.sh

Create a Script to back up all your work done till now.

Step 1: Open a Terminal or Command Prompt

  • To create the backup script, you'll need to open a terminal or command prompt on your computer.

Step 2: Navigate to the Directory

  • Use the cd command to navigate to the directory where you want to create the script. For example:

    COPY

        bashCopy codecd /path/to/your/backup_directory
    

Step 3: Create the Backup Script

  • Use a text editor to create the script. For Linux and macOS, you can use nano, vim, or gedit. For Windows, you can use Notepad or any code editor like Visual Studio Code.

  • For Linux/macOS:

    COPY

        bashCopy codenano backup.sh
    
  • For Windows:

    COPY

        Copy codenotepad backup.sh
    

Step 4: Write the Backup Script

  • Inside the text editor, add the following lines for the backup script:
  • Replace /home/Episodes/script with the actual path to the directory, you want to back up.

  • Replace /home/Episodes/backup with the path to the location where you want to save the backup.

Step 5: Save the Script

  • Save the script and close the text editor.

Step 6: Set Permissions

  • Before running the script, ensure it has executable permissions. In the terminal, navigate to the directory where the script is located and run:

    COPY

        chmod 777 backup.sh
    

About Cron and Crontab, to automate the backup Script:

Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.

Creating a Cron Job with Crontab:

    • To create a cron job to automate your backup script, you'll need to add an entry to your crontab.

      • Open your crontab in edit mode by running the following command:

        COPY

           crontab -e
        
      • If this is the first time you're editing the crontab, you might be asked to choose an editor. Select your preferred editor (e.g., nano, vim).

      • Add the following line at the end of the crontab file to schedule your backup script to run daily at 2:00 AM:

        COPY

           0 2 * * * /path/to/your/backup_script.sh
        

        This cron schedule translates to:

        • Minute: 0 (0th minute)

        • Hour: 2 (2 AM)

        • Day of the Month: * (every day)

        • Month: * (every month)

        • Day of the Week: * (every day of the week)

  1. Saving and Exiting the Crontab:

    • After adding the cron job, save the changes and exit the editor according to the editor's instructions.

    • The new cron job is now scheduled to run your backup script daily at 2:00 AM.

  2. Listing Crontab Jobs:

    • To list your existing crontab entries, use the following command:

      COPY

          codecrontab -l
      
    • It will display the contents of your crontab file.

      example:

    • COPY

          53 8 * * * bash /home/Episodes/script/disk_space.sh /home/Episodes/show_space.txt
      

output:-

Episodes@Episodes:~$ cat show_space.txt
CRITICAL for 95
CRITICAL for 95

Create 2 users and just display their Usernames:

Create the Users: Use the add user command to create the users. For example, to create users "user1" and "user2," run the following commands:

COPY

sudo adduser user1 
sudo adduser user2

Display Usernames with Awk: Use the awk command to extract and display the usernames from the /etc/passwd file:

COPY

 awk -F: '{print $1}' /etc/passwd

The output will list all the usernames, including "user1" and "user2."

The script will create two users and display their usernames as "User 1: user1" and "User 2: user2." Note that you need administrative privileges (e.g., using sudo) to create users on most systems.

And that's it for the Blog on Advanced Linux Shell Scripting for DevOps Engineers with user management. Remember practise makes perfect, and a bit of research and study too :)

Thanks for reading :)