Day 5: Mastering Advanced Linux Shell Scripting for DevOps

Day 5: Mastering Advanced Linux Shell Scripting for DevOps


3 min read

Play this article

Create multiple directories using Shell Script:

To create multiple directories using a shell script, you can use a simple loop to iterate through a list of directory names and use the mkdir command to create them. The steps are given below:

  • First, create the bash file using nano or vim editor with name directories. eg. nano and starts the bash file with #!/bin/bash.

In this Script:

  • In the if statement, we check whether the two arguments are provided or not. If not it will exit automatically.

  • After that, we provide the arguments for the variables.

  • Use mkdir command in the for loop to create directories.

  • At the echo the message indicates that directories are created.

  • Then change the permissions of the file to make it executable by using chmod command.

  • Run the bash you created and provide the arguments of the number of directories you want to create.

  • It will create five directories with the name mydir. e.g. mydir1, mydir2, mydir3, mydir4 and mydir5.

Create a Script to backup all your work done till now:

Creating a backup for your work is a good practice of shell script. Here are the steps to take a backup with a shell script:

  • Create a bash file named backup to write a shell script and start the bash file with #!/bin/bash.

In this Script:

  • Define the source directory and target directory for your backup file.

  • Create a timestamp which defines the exact date and time for your backup.

  • Check whether the source directory you provide exists or not. If not it will show the error and exit.

  • Use rsync which is a powerful tool for copying files and directories.

  • If the backup is successful it will show the completion message and if not it will show the backup failed.

Cron and Crontab:

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks (jobs) to run automatically at specified intervals or times.

Crontab (short form of cron table) is a command used with the cron daemon. It stores a list of scheduled tasks and their execution times. Users can edit their crontab to schedule recurring tasks, such as backups or automated scripts.

User Management:

A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users hence the IDs for local user begins from 1000 onwards.

Create 2 users and display their names:

To create users and display their names, you can use the useradd command to create the user and use the cut command to display their names form /etc/passwd .

In this Script:

  • Creates users by using useradd command with names user1 and user2.

  • The cut command is used to display the names of the users from the /etc/passwd file. tail -n 2 is used to display the last 2 users you created.