#Day6 Advanced Linux Shell Scripting for DevOps Engineers

Here we are Day 6 of #90daysofdevops

Topics for #day6

  • How to create multiple directories by making use of Shell scripting?

  • How important is that to take Backups as a DevOps practitioner?

  • Sample Script to backup a directory

  • What are Cron and CronTab?

  • What is User Management?

  • How to create multiple directories by making use of Shell scripting?

    Method 1:

    This code creates directories named after the value of the "anytime" variable followed by a number from 1 to 90. The loop is set to run 90 times using the syntax "for ((i=1; i<=90; i++))", and the "mkdir" command is used to create a directory with a name composed of the "${anyname}" variable followed by the current value of the loop variable, denoted by "${i}".

    Method 2:

    It is similar to the previous one, but it uses two additional variables "start" and "end" to specify the range of directories to be created and to justify

    ./creatDirectories.sh day '1' '90'

    But the scripts which you have seen so far have smaller mistakes and should use the "end" variable instead of a hardcoded value of 90. Here's a corrected version of the code:

    This script takes three arguments when called: the desired prefix for the directory names, the starting number of the range, and the ending number of the range. It uses a for loop with the "start" and "end" variables to loop through the desired range of numbers and creates directories named after the value of the "anytime" variable followed by the current value of the loop variable.

    similarly, I have written another script but here I change the name of the directory and specify the range number:

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

    Zero Trust Security - Cisco

    • It prompts the user with a message "Do you want to create a backup?" using the "echo" command and reads the user's response using the "read" command, which stores the response in the "answer" variable.

    • It then echoes back the user's response with the message "as your response is $answer, we will take backup".

    • It takes the path to the file or directory to be backed up as an argument passed to the script, which is stored in the "path" variable.

    • Finally, it creates a tar archive of the specified file or directory named "backup.tar.gz" using the "tar" command with the option "-cvzf".

      Finally, It creates a backup as shown below:

This is a Bash script that performs file and directory operations based on the user input. The script takes four command line arguments: operation, name1, name2, and file1. If the operation argument is addDir, it creates two directories named name1 and name2. If the operation argument is addfile, it creates an empty file named file1 in the directory specified by name1. If the operation argument is addcont, it prompts the user to enter content, and appends it to the file specified by name1 and file1. If the operation argument is delDir, it deletes the directory specified by name1. The script uses conditional statements to determine which operation to perform and outputs a message indicating which operation is being performed.

a) To create new directory ./new.sh addDir abhijeet Devops

b) To create a file in directory ./new.sh addfile abhijeet file.txt

c) To add Content to a file ./script.sh addcont name1 file1

d) To delete a file ./script.sh delDir name1

Cron and Crontab, to automate the backup Script:

In Linux, the "cron" daemon is a service that allows users to schedule tasks (i.e., shell commands or scripts) to be run at specific times or intervals. The "crontab" command is used to create, modify, and view the user-specific cron schedules, called "crontabs". Here's an overview of the "cron" and "crontab" commands in Linux:

Cron: The "cron" daemon is a background process that runs continuously on the system and checks the "crontabs" of all users to see if any commands need to be executed at a particular time. Cron uses a configuration file called "crontab" to manage the schedule of commands.

Some steps are written below:

  1. Create the backup script: First, create the backup script that you want to automate. For example, let's create a script called "backup. sh" that backs up a specific directory:

  2. Schedule the backup script with Cron: To schedule this script to run automatically using "cron", follow these steps:

    • Open the crontab editor by running the command "crontab -e" in your terminal.

    • If prompted to select an editor, choose your preferred text editor.

      Add a new line to the crontab file with the following syntax:

The string 0 2 * is a cron expression that specifies when to run the command. In this case, it means "run the command at 2:00 AM every day".

0: The minute when the command should run (in this case, "0" means "at the start of the hour").

2: The hour when the command should run (in this case, "2" means "2:00 AM"). : The day of the month when the command should run (in this case, "" means "every day"). : The month when the command should run (in this case, "" means "every month").

: The day of the week when the command should run (in this case, "" means "every day of the week"). The command that follows the cron expression is /home/ubuntu/backup.sh /home/ubuntu /home/ubuntu/backup_directory. This means that the script located at /home/ubuntu/backup.sh will be executed, with /home/ubuntu as the source directory to be backed up and /home/ubuntu/backup_directory as the destination directory where the backup will be stored.

So the overall meaning of this cron job is to run the backup. sh script at 2:00 AM every day, using the specified source and destination directories.

User Management:

The Top 7 Maintenance Tips for Commercial Laundry Machines | B&C ...

In Linux, user management involves creating, modifying, and deleting user accounts and groups. Here are some commonly used commands for user management in Linux:

  1. useradd: The "useradd" command is used to create a new user account.

  2. passwd: The "passwd" command is used to set or change a user's password.

    Here are some common options for the "passwd" command:

    • "-l": Lock the user's account.

    • "-u": Unlock the user's account.

    • "-d": Remove the user's password.

Create 2 users and just display their Usernames:

HOW TO: Install macOS Catalina on Linux

If you are an Ubuntu user and do not have permission to add a new user you can see it as a user, I don't have permission to create a user because I don't have the power of Superman, like a super user called root.

To edit the visudo file, you need to have administrative privileges on the system. This means you must be a user with the sudoer's permissions or the root user.

Assuming you have the necessary permissions, you can edit the visudo file by following these steps:

  1. Open the terminal on your system.

  2. Type the following command and press Enter:

    COPY

    COPY

      sudo visudo
    
  3. Make the necessary changes to the file. Be very careful while editing this file, as incorrect changes can cause serious problems with your system.

  4. Once you have made the necessary changes, save the file and exit the editor.

Now , try to create the user via ubuntu user.

Optionally, you can add the new user to any additional groups, such as the sudo group, by running the following command:

COPY

COPY

    usermod -aG groupname username

Now, If anyone wants to see all users:

Replace "groupname" with the name of the group you want to add the user to, and "username" with the name of the user you just created.

This command will prompt a new window with all user details: