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

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

1.shell script to create directories using loop

Here is a Bash shell script that creates a specified number of directories with a dynamic name, based on user input:

#!/bin/bash

# Check if three arguments are provided
if [ $# -ne 3 ]; then
  echo "Usage: $0 <directory-name> <start-number> <end-number>"
  exit 1
fi

# Assign input arguments to variables
dir_name=$1
start_num=$2
end_num=$3

# Create directories with dynamic names
for (( i=start_num; i<=end_num; i++ )) 
do
  mkdir "${dir_name}${i}"
  echo "Directory ${dir_name}${i} created."
done

In this script, the first section checks if exactly three arguments are provided when the script is executed. If not, it prints a usage message and exits.

Then, the three input arguments are assigned to variables named "dir_name", "start_num", and "end_num".

The last section creates a loop that runs through all values from "start_num" to "end_num", and creates a directory with a dynamic name using the "mkdir" command and the variable values. The loop also prints a message to confirm that each directory was successfully created.

To run this script, you can execute it with the following command, using your own desired directory name, start number, and end number values:

./createDirectories.sh <directory-name> <start-number> <end-number>

For example, if you want to create directories named "mydir1", "mydir2", "mydir3", you can run:

./createDirectories.sh mydir 1 3

This will create three directories named "mydir1", "mydir2", and "mydir3".

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

Here is a Bash shell script that creates a backup of all the files in the current directory and its subdirectories:

#!/bin/bash

# Assign a name and location for the backup file
backup_file="mybackup_$(date +%Y-%m-%d_%H:%M:%S).tar.gz"

# Create the backup file
tar -czvf "$backup_file" .

# Print a message to confirm that backup is complete
echo "Backup created: $backup_file"

In this script, the "date" command is used to generate a timestamp for the backup file. This timestamp is added to the backup filename, which includes the extension ".tar.gz" to indicate that the backup is a compressed archive.

The "tar" command is used to create the backup file, using the options "-czvf" to create a compressed archive of all files in the current directory and its subdirectories.

Once the backup is complete, a message is printed to confirm the name and location of the backup file.

To run this script, you can save it to a shell script file (e.g., "backup.sh") and make it executable using the "chmod" command:

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

Cron is a time-based job scheduler in Unix-based operating systems. It allows you to schedule and automate the execution of commands or scripts at specific dates and times, or with a regular frequency, such as daily, weekly, or monthly.

Crontab is a simple text file that contains a list of commands or scripts to be executed by the cron daemon, along with the scheduling information for each command. Each user on a Unix-based system can have their own crontab file, which can be edited using the "crontab" command.

To automate the backup script using cron and crontab, you can follow these steps:

Open your crontab file using the command crontab -e.

  1. Add a new line at the bottom of the file with the scheduling information and the command you want to run. For example, if you want to run the backup script every day at 3am, you can add the following line:
0 3 * * * /path/to/backup.sh

This line specifies that the command /path/to/backup.sh should be executed every day at 3am (00:00 hours), using the 0 3 * * * scheduling information.

Here's what each field means:

  • 0 refers to the minutes. In this case, we want the script to run at minute 0 (i.e. at the start of the hour).

  • 3 refers to the hour. In this case, we want the script to run at 3am.

  • * refers to every day of the month.

  • * refers to every month of the year.

  • * refers to every day of the week.

  1. Save the crontab file and exit the editor.

Your backup script will now run automatically every day at the specified time. You can check the crontab logs to ensure that the script has been executed successfully at the scheduled time.

Note: Make sure that the backup script is executable (chmod +x backup.sh) and that the path to the script is correct in the cron command.Read about User Management .

4.Read about User Management

User management involves creating, modifying, and deleting user accounts on a system, as well as assigning user permissions, groups, and other settings. Here are some common commands and methods used for user management in Unix-based operating systems:

  • Creating a user account: You can create a new user account using the useradd command. For example, to create a new user named "john", you can run:
  1.  sudo useradd mehboob
    
    • Setting a password for a user: You can set a password for a user using the passwd command. For example, to set a password for the user "john", you can run:
    sudo passwd mehboob
  • Modifying user account properties: You can modify a user account, such as changing the user's password, full name, or home directory, using the usermod command. For example, to change the home directory of the user "mehboob" to /home/mehboob, you can run:
    sudo usermod -d /home/mehboob mehboob
  • Deleting a user account: You can delete a user account using the userdel command. For example, to delete the user "mehboob" and his home directory, you can run:
    sudo userdel -r mehboob
  • Creating a user group: You can create a new user group using the groupadd command. For example, to create a new group named "staff", you can run:
    sudo groupadd staff
  • Adding a user to a group: You can add a user to a group using the usermod command, with the -aG option to append the user to the specified group. For example, to add the user "mehboob" to the "staff" group, you can run:
    sudo usermod -aG staff mehboob

These are just a few examples of the commands and methods used for user management in Unix-based operating system.

5.create 2 users and just display their Usernames

  • added 2 users by using useradd command

$sudo useradd mehboob

$sudo useradd shaikh

To display their name use cat /etc/passwd and redirected the output to the tail command to see only the last two lines from the file :

Thankyou,