Advanced Linux Shell Scripting for DevOps Engineers with User Management

In this short blog I will share some information with examples about using "for loops" to create multiple directories; creating backups; Cron; and User Management.

Shell Script for creating multiple Directories

I created a Shell Script file "createDirectories.sh" which allows the user to enter up to 3 arguments (directory name; start number of directories; & end number of directories). I used a "for loop" in the shell script to create this.

For loops are useful for iterating over a set of values or a list of files in a directory.

  • The for loop starts by initializing a counter variable "i" with the value of "start_num".

  • The loop will continue to run as long as "i" is less than or equal to "end_num".

  • In each iteration of the loop, a new directory is created with the "mkdir" command. The name of the directory is constructed using the concatenation of "dir_name" and the current value of "i".

  • Finally, the loop increments the value of "i" by 1 at the end of each iteration using the "i++" notation. The loop ends when it reaches the "end_num".

Shell Script to create a Backup

I created a backup of my "/my_scripts" folder which contains all the scripts I have created so far. For this task, I created a script that saves my backups in a "/backup" folder.

  • This script includes the source location, target location & file name format.

  • For the file naming convention, I used the following: "Backup_YYYY-MM-DD-HH-MM-SS.tar.gz - time included in the file name if multiple backups are required on the same day.

  • I also learned a new command printf "\n" - this prints out an empty line.

  • See the picture below for the script and the output.

Cron & Crontab

Cron is a time-based job scheduler. It allows users to automate repetitive tasks by scheduling them to run at specific times, dates, or intervals. Cron executes jobs in the background without any user intervention.

Crontab is a configuration file that specifies the commands or scripts to be executed by cron. Each user has their crontab file. The file contains a set of instructions for cron to execute. Each line in the file represents a separate job and includes information such as the time and date when the job should run and the command or script to be executed.

Crontab syntax consists of six fields that specify when a job should be executed: minute, hour, day of the month, month, day of the week, and the command or script to be executed.

An example of a crontab entry that will run a backup script every day at 2 AM would be: 0 2 * /path/to/backup-script.sh

Useful Crontab commands:

CommandsDescription
crontab -eopens the current user's crontab file in the default text editor, allowing the user to edit or add new cron jobs
crontab -llists all the cron jobs for the current user
crontab -rremoves all the cron jobs for the current user
crontab -uused to specify a different user's crontab file to be edited or listed

User Management

User management involves creating, modifying, and deleting user accounts, as well as setting permissions and access control for each user. User Management is an essential aspect of Linux administration, as it helps to ensure that the system remains secure and organized.

Example of 2 Users Created

Below shows using the "sudo" command to add 2 users. The command to see the users in the system is "cat /etc/passwd" - this will list out all the users.


I appreciate your busy time reading this short blog. As I continue with my journey to learn and acquire the skill set of a DevOps Engineer, I will share what I learn. Thank you.

Happy Learning!


Sam Samarullah

LinkedIn

Previous Blog

Instagram