Day5 Advanced Linux Shell Scripting

Day5 Advanced Linux Shell Scripting

Today's blog will cover advanced shell scripting with reference to the previous blog we have already covered the Linux shell scripting basic. The Bash Shell Script that is written with #!/bin/bash (Shebang) specifies that the script should be interpreted and executed by the Bash Shell.

Variables in Shell Script

Variables are used to store data temporarily. To get the value of a variable, the variable name is prefixed with a dollar sign ($).

Arguments in Shell Script

Shell Script can take the arguments that are passed when executing the script from the command prompt.

Syntax -

./<scriptfile_name> arg1 arg2

special variables are $0, $1, $2

  • $0 - The name of the scripts

  • $1 - First argument that is passed from the command line.

  • $2 - Represents the second argument.

Task 1 -

The script is executed with three given arguments (one is the directory name and 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.

After the execution of the script, will create 90 directories.

Task 2 - Backup all the works

Backups are an important part of DevOps Engineers' day to Day activities. To save the file from unnecessary failure, backup ensures data recovery and automates data safety.

We will take backup by compressing and archiving a source and target directory location. tar the command helps to compress and archive the file with options

-c -Creates Archive -v : Displays Verbose Information -f : creates archive with given filename

The backup is stored in the target directory.

Cron and Crontab, to automate the backup Script

Cron is a time-based task scheduler. We will automate the above backup script to run at specific intervals of time.

crontab is a file containing all schedules of the cron jobs a user wants to run regularly.

  • First, use the crontab command to create your first crontab

Enter the values based on below values

5 * * * * cat /path/to/your/backup.sh  - Replace you script path with actual backup.sh path

The backup will automatically run at a given specific time.

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.

Useradd - To create a User.

userdel - To delete a user

usermod - To modify user properties

id - To display user information

In the below task, we will create 2 users and display their names.

The command will fetch the usernames from /etc/passwd

Thank you for reading!

Let's continue this exciting journey of DevOps together.

Happy Learning!