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

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

ยท

3 min read

Exploring Shell Scripting ๐Ÿš€

Welcome to Day 5 of our 90-day DevOps journey! Today, we're diving into shell scripting, an essential skill for automating tasks and managing systems efficiently. Shell scripting is a powerful tool that allows us to write scripts to execute commands sequentially, making our workflows smoother and more efficient. Let's explore some practical shell scripting tasks and learn how they can make our lives easier. ๐ŸŒŸ

Creating Directories Using Shell Script ๐Ÿ“‚

One of the most common tasks in DevOps is managing directories. Let's write a bash script called createDirectories.sh that, when executed with three arguments (directory name, start number, and end number), creates a specified number of directories with dynamic names.

A bash shell script have parameters. These parameters start from $1 to $9.

When we pass arguments into the command line interface, a positional parameter is assigned to these arguments through the shell.

FOR LOOP :
You can use for loop in multiple ways . One of the way used is shown below
Syntax :

The syntax is as follows:

The syntax is as follows:

for (( EXP1; EXP2; EXP3 ))
do
    shell-command-1
    shell-command-2
done

Here's the bash script createDirectories.sh:

OUTPUT :

Example 2: When the script is executed as

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

Task 2: Back Up Your Work ๐Ÿ’พ

Backups are crucial in DevOps to prevent data loss. Let's create a script to back up all your work.

The Backup Script ๐Ÿ“‚

Here's a simple backup script:

source_dir: This variable holds the path to the directory that contains the work you want to back up. Its input is been taken by the user
+"%Y-%m-%d-%H-%M%-S": This format string tells date to output the current date and time in the format YYYYMMDDHHMMSS. For example, 2024-07-08-12-34-56 represents July 8, 2024, at 12:34:56 PM.

Task 3: Automate Backups with Cron ๐Ÿ•ฐ๏ธ

Automating your backup script ensures regular backups without manual intervention. We'll use cron and crontab for this.

Setting Up a Cron Job ๐Ÿ“…

  1. Edit your crontab file: crontab -e.

  2. Add the following line to run the backup script every day every minute

Below backup files are generated every minute as per cron job

Task 4: User Management ๐Ÿ‘ฅ

In Linux, managing users is an essential skill. Users are entities that can perform various operations, including manipulating files and running programs. Let's write a bash script to automate the creation of two users and display their usernames.

The Script ๐Ÿ“œ

Here's the bash script createUsers.sh:

Conclusion ๐ŸŽ‰

Congratulations on completing Day 5 of your DevOps journey! Today, you learned how to create directories dynamically using a shell script, back up your work, automate backups with cron, and manage users in Linux. Keep experimenting and practicing these skills.

See you on Day 6! ๐Ÿš€


Feel free to reach out with any questions or thoughts in the comments below. Happy DevOps-ing! ๐Ÿš€

ย