Skip to main content

Command Palette

Search for a command to run...

90DaysOfDevops-Day5

Published
•2 min read•View as Markdown
90DaysOfDevops-Day5

Advanced Linux Shell Scripting for DevOps Engineers with User management

Tasks-

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 create directories.sh that when the script is executed with three given arguments (one is the directory name and second is start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic dirctory name.

Example 1: When the script is executed as

./createDirectories.shday 1 90

then it creates 90 directories as day1 day2 day3 .... day90

Example 2: When the script is executed as

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

#!/bin/bash
<< task
   create custom directory and 
   make new directories with the given range
task

echo "Enter the name of the root directory"
read root_dir
mkdir $root_dir && cd $root_dir
echo "Enter the Custom Directory Name"
read custom_dir
for ((i=$1; i<=$2; i++))
do
        mkdir  "${custom_dir}${i}"
done

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

#!/bin/bash

<< comment
   This Script is used for taking backups"
comment
src_dir="/home/ubuntu/Scripts"
tgt_dir="/home/ubuntu/Backups"
backup_filename="backup_$(date +%Y-%m-%d-%H-%M-%S).tar.gz"
echo "Backup Started"
echo "Backing up to $backup_filename........"
tar -czvf "${tgt_dir}/${backup_filename}" "$src_dir"
echo "Backup Complete"

Read About Cron and Crontab, to automate the backup Script

Cron: Think of Cron as a time-based job scheduler on your computer or server. It's like a digital alarm clock that can be set to run specific tasks automatically at predefined intervals. These tasks can include things like running scripts, performing backups, or executing any command you want at a scheduled time.

Crontab: Crontab is short for "cron table." It's a configuration file that allows you to create and manage the scheduled tasks (cron jobs) on your system. In simpler terms, crontab is where you set the schedule for when you want your automated tasks to run. You can define the timing, frequency, and the actual commands or scripts to be executed. It's like your to-do list for your computer's automated tasks.

Create 2 users and just display their Usernames

#!/bin/bash
sudo adduser windows
sudo adduser linux

For Displaying their names only we can use:

tail -n 2 /etc/passwd | awk -F: '{ print $1 }'

More from this blog

Ravi Kumar Srivastava's blog

17 posts

I am a Cloud and DevOps Engineer with over 3 years of experience using AWS Cloud and DevOps Tools. I am AWS Certified Solutions Architect Associate.