Day 5 Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to discuss advanced shell scripting for DevOps with User Management.

Tasks:

  1. When the script is executed as

./file.sh day 1 90

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

When the script is executed as

./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

#!/bin/bash

######################################
# Author- Kritika Shaw
# Date- 28-10-2023
# Creating directories using loop
#######################################

echo "Enter the Directory name: "
read name
echo "Enter the starting number :"
read start_num
echo "Enter the ending number: "
read end_num

# for i in {$start..$end}
for (( i = start_num; i <= end_num; i++ ));
do
    mkdir $name$i
done

echo "Directories are Created"

  1. Create a Script to backup all your work done till now.

     #!/bin/bash
    
     ##########################
     # Author- Kritika Shaw
     # Date- 28-10-2023
     # Script for Backup
     ##########################
     # Source 
     src_dir=/home/kritika/90daysofdevops
     # Destination
     tgt_dir=/home/kritika/backup_folder
    
     curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
     backup_file=$tgt_dir/$curr_timestamp.tgz
    
     echo "Taking backup on $curr_timestamp"
     tar czf $backup_file --absolute-name $src_dir
    
     echo "Backup Complete"
    

  2. Read About Cron and Crontab, to automate the backup Script

    These are the services that allow to schedule and automate in a particular interval. 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.

    There are some different commands for crontab:

    • crontab -l - To list the current crontab file.

    • crontab -e - To edit your crontab file.

    • crontab -r - To remove your current crontab file.

    • crontab -d - To delete a specific user crontab.

    • cron -l -u - To list a specific user crontab.

I have taken the same script for backup in crontest.sh and copy it to sudo cp crontest.sh /etc/cron.daily/, then list the content ls /etc/cron.daily.

Next, run the command crontab -e.

Run crontab -l and see output.

You can see the backup was created at 20:02, in the below image you can find it.

  1. Read about User Management

    In a Linux system, users refer to individuals or entities that interact with the operating system by logging in and performing various tasks. User management plays a crucial role in ensuring secure access control, resource allocation, and system administration.

    A user in Linux is associated with a user account, which consists of several properties defining their identity and privileges within the system. These properties are a username, UID (User ID), GID (Group ID), home directory, default shell, and password.

    Type of users in Linux

    Linux supports two types of users: system users and regular users.

    System users are created by the system during installation and are used to run system services and applications.

    Regular users are created by the administrator and can access the system and its resources based on their permissions.

What three things must you do to manage user accounts?

  • Create accounts- useradd

  • Modify accounts- usermod

  • Delete accounts- userdel

  1. Create 2 users and just display their Usernames

     useradd Devloper
     useradd Tester
    
     # Display detailed user information
     id Devloper
     id Tester
    

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: linkedin.com/in/kritikashaw

Follow my Blog channel: hashnode.com/@kritikashaw