Advance Linux shell scripting Day5 90daysofdevopschallenge

Here comes day 5 tasks today we will deep dive into advance Linux shell scripting.

Table of content:

Tasks1.

1) You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

So Write a bash script createdirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.

#!/bin/bash

if [ $# -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

directory_name=$1
start=$2
end=$3

for ((i = $start; i <= $end; i++)); do
  mkdir "${directory_name}${i}"
done

now save the file createdirectories.sh with :wq. after that give (rwx) permission to the file with chmod 700 createDirectories.sh.

Example 1: When the script is executed as:

./createDirectories.sh day 1 90

It will create the directories from day1 to day 90.

Example 2: when the script is executed as:

./createDirectories.sh Movies 20 50

It will create the directories from Movies 20 to 50.

Tasks 2:

  1. Create a Script to backup all your work done till now.
#!/bin/bash

src=/home/wsl/Newfolder
tgt=/home/wsl/Downloads/backups

echo "backup started"

tar -cvf $tgt/my_backup.tar.gz $src

echo "backup completed"

Through the script, we can take a backup of our work.

  1. Read about cron and crontab, to automate the backup script.

AUTOMATE THE BACKUPS USING CRON JOB:

To automate the backup script using a cron job, open the crontab for editing:

crontab -e

Crontab -e will list out the list in which we will add the following line at the end of the file to run the script daily at a specific time (e.g., 8:00 AM):

COPY

0 8 * * * /path/to/backup_script.sh

Make sure to replace /path/to/backup_script.sh with the actual path to your backup script.

Save the file and exit the editor. The cron job is now set to run daily at 8:00 AM, executing the backup script and creating a new backup archive.

  1. Create 2 users and just display their usernames

  2. A new user can be added using the “useradd” command

    We can confirm that the users have been added by accessing the user configuration file using “ cat /etc/passwd”

Conclusion:

Shell scripting plays a vital role in linux as it makes the automation of tasks. Saves time and make easy workflow.

If you find the information useful please drop a thump's up and share.

#90daysofdevops #trainwithshubham #devopsszerotohero

Thanks

Kavita Pant.