Skip to main content

Command Palette

Search for a command to run...

Advanced Linux Shell Scripting for DevOps

Published
β€’2 min readβ€’View as Markdown
Advanced Linux Shell Scripting for DevOps
N

Nikhil Patil

πŸ‘¨β€πŸ’» I love Linux & DevOps! πŸ’»

🐧 LinuxπŸ‘¨β€πŸ’» | DevOps & Cloud Enthusiast | 🌍 Azure Admin | πŸ› οΈ Ansible | πŸ“‚ Git | 🐳 Docker | πŸ”‘ OpenLDAP | πŸ–₯️ System Admin | | πŸš€ Jenkins πŸ› οΈ

🏠 Pune

🌟 I'm really into using and managing computers with Linux. I also like making things work smoothly and quickly.

πŸ“– I work with Microsoft Azure, use tools like Ansible and Git to automate tasks, and make software run in containers with Docker.

πŸ”’ I handle access and security using OpenLDAP, and I'm all about keeping systems running smoothly.

🌐 Exploring opportunities in cloud and DevOps.

πŸ“§ Get in touch: nikrpatil1997@gmail.com

πŸ“ Introduction

This blog will cover the automation of creating multiple directories, the process of taking backups using cron jobs, and the management of users in Linux.

πŸ“Œ Task - 1 Create Multiple Directories by Passing Arguments πŸ“πŸ“

Example -1

#! /bin/bash

name="$1"
first_number="$2"
last_number="$3"

for ((i=$2;i<=$3;i++))

do
mkdir -p "/home/nik/Directory/$1$i"
done

Output:

Example - 2

Output :

πŸ“Œ Task - 2 Script to Backup Your Work πŸ“œπŸ’Ύ


#! /bin/bash

# source and destination path
source_path=/home/nik/Directory
Dest_path=/home/nik/backup

# current timestamp
day=$(date "+%Y-%m-%d_%H-%M-%S")

# tar folder
tar -cvf  $Dest_path/Directory.tar.gz$day $source_path
echo "Backup completed successfully"

πŸ“Œ Task - 3 What are Cron and Crontab πŸ•°οΈπŸ—’οΈ

The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. It allows users to schedule tasks or commands to run at specific intervals or at predefined times. These scheduled tasks are referred to as corn jobs.

Entry in the crontab file to schedule the above script for daily execution at 11:30. /dev/null This refers to the null device, and writing to it is a way to discard the output.

30 11 * * * /home/nikhil/backup.sh && /dev/null

πŸ“Œ Task - 4: User Management in Linux πŸ‘₯πŸ”§

User management in Linux involves creating, managing, and controlling user accounts on the system. Each user account represents a separate identity that can access the system's resources, files, and applications.

# How to create users in linux

sudo useradd <username>
sudo adduser <username>

πŸ“Œ Task - 5 Creating Users and Displaying Usernames πŸ‘€πŸ‘€

sudo useradd test1
sudo useradd test2
# To check usernames 

id
cat /etc/passwd
awk -F: '{print $1}' /etc/passwd