Basic Linux Shell Scripting for DevOps Engineers.

Basic Linux Shell Scripting for DevOps Engineers.

Day 4&5 of 90daysofdevops


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?

Essentially it tells your terminal that when you run the script it should use bash to execute it. It can be vital since you may be using a different shell in your machine (zsh, fish, sh, etc.), but you designed the script to work specifically with bash.

Write a Shell Script that prints I will complete #90DaysOofDevOps challenge

to create a main.sh file and write code

#!/bin/bash
echo "I will complete #90DaysofDevOps challenge"

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 has accepted 90DaysofDevOps challange"

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

create a file with the script as:

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

run this file and the result will be

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 directory as nano createDirectories.sh and put I code as

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

for (( i=start; i<=end; i++))
do
 mkdir "$dir_name$i"
done
ubuntu@ip-172-31-2-150:~/Day5$ chmod 777 createDirectories.sh
ubuntu@ip-172-31-2-150:~/Day5$ ./createDirectories.sh dir 1 90
ubuntu@ip-172-31-2-150:~/Day5$ ls
createDirectories.sh  dir19  dir29  dir39  dir49  dir59  dir69  dir79  dir89
dir1                  dir2   dir3   dir4   dir5   dir6   dir7   dir8   dir9
dir10                 dir20  dir30  dir40  dir50  dir60  dir70  dir80  dir90
dir11                 dir21  dir31  dir41  dir51  dir61  dir71  dir81
dir12                 dir22  dir32  dir42  dir52  dir62  dir72  dir82
dir13                 dir23  dir33  dir43  dir53  dir63  dir73  dir83
dir14                 dir24  dir34  dir44  dir54  dir64  dir74  dir84
dir15                 dir25  dir35  dir45  dir55  dir65  dir75  dir85
dir16                 dir26  dir36  dir46  dir56  dir66  dir76  dir86
dir17                 dir27  dir37  dir47  dir57  dir67  dir77  dir87
dir18                 dir28  dir38  dir48  dir58  dir68  dir78  dir88

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"
ubuntu@ip-172-31-2-150:~$ chmod 777 backup.sh
ubuntu@ip-172-31-2-150:~$ ./backup.sh
Taking backup on 2023-03-24-12-43-39
/home/user/Day5/
/home/user/Day5/createDirectories.sh
Backup complete

ubuntu@ip-172-31-2-150:~$ cd backups
ubuntu@ip-172-31-2-150:~/backups$ ls
2023-03-24-12-43-39.tgz

ubuntu@ip-172-31-2-150:~/backups$ tar xf 2023-03-24-12-43-39.tgz

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 fro 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.

Create 2 users and just display their Usernames

ubuntu@ip-172-31-2-150:~$ sudo useradd Ritul-user1
ubuntu@ip-172-31-2-150:~$ sudo useradd Ritul-user2
ubuntu@ip-172-31-2-150:~$ tail -n 2 /etc/passwd | cut -d: -f1
Ritul-user1
Ritul-user2

#Or we can use this
ubuntu@ip-172-31-2-150:~$ cut -d: -f1 /etc/passwd | grep -E 'Ritul-user1|Ritul-user2'
Ritul-user1
Ritul-user2

Thank you for visiting my Blog!! (●'◡'●)✌️