Basic Linux Shell Scripting for DevOps Engineers.

·

4 min read

What is Kernel

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.

What is Shell

A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from a user and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal.

What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Shell scripting is a program to write a series of commands used to automate the tasks written in the .sh file. Shell scripting is an important part of process automation in Linux and it is used to automate repetitive tasks and streamline the development and deployment process. The scripting file starts with #!/bin/bash

A shell script involves the following basic elements

  • Shell Keywords – if, else, break, etc.

  • Shell commands – cd, ls, echo, pwd, touch etc.

  • Functions Control flow – if..then..else, case and shell loops, etc.

What is #!/bin/bash? Can we write #!/bin/sh as well?

to create a main.sh file and write code

echo " Diwakar Devops Engineer"

Now will execute the file with a command sh main.sh

Write a Shell Script to take user input, input from arguments, and print the variables.

Create a test file as test.sh and put I code as

#!/bin/bash 

echo "Enter you name"
read name
echo " $name Devops Engineer"

Write an Example of If else in Shell Scripting by comparing 2 numbers

create a file with the script as:

#!/bin/bash
a=10
b=20
if [$a -gt $b]
then
    echo "a is greatest"
else
    echo "b is greatest"
fi

Advanced Linux Shell Scripting for DevOps Engineers with User management

  1. Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is the directory name and the 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.

    Create a file createdirectories.sh

#!/bin/bash
dir_name=$1
start=$2
end=$3

for (( i=start; i<=end; i++))
do
 mkdir "$dir_name$i"
done

Create a Script to back up all your work done till now.

Create a backup file as nano backup.sh and put I code as

#!/bin/bash
src_dir=/home/user/Day5
tgt_dir=/home/user/backups
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_file=$tgt_dir/$curr_timestamp.tgz
echo "Taking backup on $curr_timestamp"

tar -cvzf $backup_file --absolute-names $src_dir
echo "Backup complete"

Read About Cron and Crontab, to automate the backup Script

Cron is an effective and popular command-line utility used to schedule tasks at a specified time and day without user interaction.

The scheduled tasks are known as cron jobs while the crontab is the list of the file containing the cron jobs.

Crontab is useful to perform various operations such as handling automated backups, rotating log files, syncing files between remote machines and clearing out temporary folders, etc.
The crond daemon is the background service that enables cron functionality.

Linux Crontab Syntax

USER_NAME COMMAND/SCRIPT-TO-EXECUTE
│ │ │ │ │
│ │ │ │ │
│ │ │ │
|_________ Day of Week (0 – 6) (0 is Sunday, or use names)
│ │ │ |**____________
Month (1 – 12), means every month
│ │ |______________ Day of Month (1 – 31),* means every day
│ |________________ Hour (0 – 23),* means every hour
|___________________ Minute (0 – 59), * means every minute

  • crontab -e: Edit crontab file, or create one if it doesn’t already exist.

  • crontab -l: Display crontab file contents.

  • crontab -r: Remove your current crontab file.

  • crontab -i: Remove your current crontab file with a prompt before removal.

  • crontab -u <username>: Edit other user crontab files. This option needs system administrator privileges.

Read about User Management

User management in Linux is the process of creating, modifying, and deleting user accounts on a Linux system. In Linux, each user has a unique username and password, which are used to log in and access the system.

adduser: add a user to the system.
passwd: set password for user
userdel: delete a user account and related files.
addgroup: add a group to the system.
delgroup: remove a group from the system.
usermod: modify a user account.
sudo: run one or more commands as superuser permissions.