Advanced Linux Shell Scripting for DevOps with User management

Advanced Linux Shell Scripting for DevOps with User management

Article: Shell Scripting with examples

  1. Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.

    Here's a bash script named createDirectories.sh that takes three arguments as input and creates a specified number of directories with dynamic names:

     #!/bin/bash
    
     # Check if the correct number of arguments were provided
     if [ $# -ne 3 ]; then
       echo "Usage: $0 <directory name> <start number> <end number>"
       exit 1
     fi
    
     # Assign arguments to variables
     dir_name=$1
     start_num=$2
     end_num=$3
    
     # Loop to create directories with dynamic names
     for (( i=$start_num; i<=$end_num; i++ ))
     do
       mkdir -p "${dir_name}_${i}"
     done
    
     echo "Directories created successfully!"
    

    Here's how the script works:

    The script checks if the correct number of arguments were provided. If not, it prints an error message and exits.

    The script assigns the input arguments to variables for ease of use.

    The script uses a loop to create directories with dynamic names. The loop starts at the start_num value and continues until the end_num value is reached. It creates directories with names in the format <directory name>_<number> using the mkdir command.

    The script prints a success message once all directories have been created.

    To run the script, save the above code in a file named createDirectories.sh, make the file executable using the command chmod +x createDirectories.sh, and then execute the script with the three required arguments as follows:

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

    For example, to create 5 directories named "example_dir_1" through "example_dir_5", you would run:

     ./createDirectories.sh example_dir 1 5
    
  2. Create a Script to backup all your work done till now

To create a script that backs up all your work done till now, you can use the tar command to create a compressed archive of your entire home directory. o automate this process, you can create a bash script that runs the above commands. Here's an example script:

#!/bin/bash

# Create backups directory if it doesn't exist
mkdir -p ~/backups

# Create backup archive
tar -czvf ~/backups/home_backup_$(date +%Y-%m-%d).tar.gz .

# Print success message
echo "Backup completed successfully!"

Save this code in a file named backup_script.sh and make the file executable using the command chmod +x backup_script.sh. Then, you can run the script by executing ./backup_script.sh in a terminal window.

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

    Cron is a utility that allows you to schedule tasks to run automatically at specific intervals. Cron is used on Unix-based operating systems such as Linux to schedule tasks that run periodically, such as backups, updates, and maintenance tasks.

    Crontab is the configuration file used by the cron utility to schedule tasks. The crontab file contains a list of commands to be executed at specified intervals. Each line of the crontab file specifies a task to be run, along with the time and frequency at which it should be run.

    To automate the backup script using cron, you can add an entry to your crontab file that runs the backup script at a specific time or interval. Here's an example crontab entry that runs the backup script every day at 2:00 AM:

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

    The fields in the crontab entry represent the following:

    • Minute (0-59)

    • Hour (0-23)

    • Day of the month (1-31)

    • Month (1-12)

    • Day of the week (0-6, with 0 representing Sunday)

In the above example, the "0 2 *" fields represent "0 minutes past 2:00 AM every day, every month, and every day of the week".

To edit your crontab file, you can use the command crontab -e. This will open the crontab file in the default editor for your system (usually vi or nano). Add the crontab entry shown above to the file, save the file, and exit the editor.

Once the crontab entry is added, the backup script will run automatically at the specified time or interval. You can check the status of your cron jobs using the command crontab -l, which will list all the entries in your crontab file.

  1. Create 2 users and just display their Usernames

    To create two users in Linux, you can use the adduser command. Here's an example of how to create two users named "user1" and "user2":

     sudo adduser user1
     sudo adduser user2
    

    After running these commands, you will be prompted to enter a password and other information for each user.

    To display the usernames of these two users, you can use the cut command to extract the first field (the username) from the output of the cat command on the /etc/passwd file, like this:

     cat /etc/passwd | cut -d: -f1 | grep 'user'
    

    This command will display the usernames of all users that have "user" in their name. If you want to display only the usernames of "user1" and "user2", you can modify the command like this:

     cat /etc/passwd | cut -d: -f1 | grep -E 'user1|user2'
    

    This command will display only the usernames of "user1" and "user2".

#Linux #DevOps #Shellscript

Please free to write your thoughts and can connect with me on LinkedLn