Skip to main content

Command Palette

Search for a command to run...

(Day-5) :Advanced Linux Shell Scripting for DevOps Engineers with User Management

Updated
4 min read
(Day-5) :Advanced Linux Shell Scripting for DevOps Engineers with User Management
S

Motivated and goal-oriented aspiring DevOps engineer with a strong passion for optimizing software development and deployment processes

Entering into the advanced scripting concepts.
In the below blog let's do some hands-on:-
❇ Scenario-based scripts.
❇ Automation of scripts.
❇ User management in Linux

Automation of scripts used to create Directories :

Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is a directory name, 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.

  1. Example 1: When the script is executed as

    ./createDirectories.sh day 1 90

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

  • Let's create a shell script in vim editor with the name createDirectories.sh.

  • Note that $1, $2 and $3 are the arguments that are to be passed while executing the shell script in our terminal. Please keep in mind that before executing the .sh file we need to change the permissions of the file and make it executable by using the chmod command. For a better understanding refer to below image :

  1. Example 2: When the script is executed as
  • ./createDirectories.sh Movie 20 50

    -creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

  • Let's execute the same file again and provide the above inputs. See below for output in the terminal:


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

  • Let us remove all the files except createDirectories.sh and use this file to take a backup. Let us also create a directory - dir1 and move the file createDirectories.sh to dir1. Now, let's create a directory for backup and name it as backup. Here, we will be storing our backup file.

  • Now let's create the backup_script.sh, a file which we can run and create/ generate the backup of the file createDirectories.sh👇🏻

    We are using the tar command here to archive and also compress the file to create a backup file with a small size.

  • In the command tar -czvf, c indicates create, z is used to zip/compress the file,v= enables verbose mode which means tar will display detailed information about the files being archived.

  • -f "${backup_destination}/${backup_filename}": The -f option is used to specify the filename of the archive that will be created. ${backup_destination}/${backup_filename} is the full path to the backup file, which combines the backup_destination (destination directory) with the backup_filename (the name of the backup file).

  • "backup_source": This is the source directory that we want to back up. This has the path /home/ubuntu/dir which contains our source file to be backed up - createDirectories.sh.

Upon execution of the backup_script.sh, we get the following result :) 👇🏻


Cron Job 🕰️ & Crontab🗓️📅:

  • Cron Job: A cron job is a scheduled task that allows you to automate repetitive tasks by scheduling them to run at specified intervals or times.

  • Crontab: Each scheduled task, known as a "cron job," is defined in a crontab (cron table) file. The crontab file contains a list of commands or scripts along with the timing information for when each command should be executed.

Syntax for a crontab:
* * * * * command_to_be_executed

  • Some useful commands crontab commands to use in the terminal :

    • crontab -e: Edit the crontab file, or create one if it doesn’t already exist.

    • crontab -l: Display crontab file contents.

    • crontab -r: Remove your current crontab file.

    • crontab -i: Remove your current crontab file with a prompt before removal.


👤 User Management in Linux📝 :

  1. Linux user management is a crucial aspect of system administration, allowing you to create, modify, and remove user accounts on your Linux-based system.

  2. To create a new user, use the useradd command. For example, let's create a couple of users named "Sarthak" and "Binod":

root@ip-172-31-94-99:/home# sudo useradd Sarthak 
root@ip-172-31-94-99:/home# sudo useradd Binod

To view whether the users were created type cat /etc/passwd 👇🏻

💻🚀In this blog, we explored a range of powerful commands that empower DevOps practitioners to streamline processes, improve productivity, and enhance system performance. On Day6 we will be talking about File Permissions and ACL. Thanks! Ciao!