Linux System Administration With Advanced Shell Scripting

Linux System Administration With Advanced Shell Scripting

#Day5 of #90daysofdevopschallenge together to help you understand the functionality of Linux admin in day-to-day activities.

What is Shell script?

A shell script is an executable file containing multiple shell commands executed sequentially.

  • Shell (#!/bin/bash)

  • comments (#comments)

  • Commands (echo, cp, grep, awk, etc.)

  • statements (if, while, for, etc.)

The shell script should have executable permissions (example - rwx r-x r-x)
The shell script should be called from an absolute path (example: /home/ubuntu/script.bash)
If called from the current directory/location then use ./script.bash

Basic Shell script example: to get the output on the screen:

The above example shows I have created a file name create directories shell file, however, to demonstrate I have written a few basic commands and run the script from the current location.

But In the daily activities of Linux admin, we need to automate the tasks and therefore we need to use loops. Below I am going to demonstrate the example of "for loop"

Example 2: When the script is executed as

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

Example 3: Creating a Backup of directories using the tar command

Step 1: Created a file called create_backup.sh with the below commands:

#what needs to be backed up

src=/home/ubuntu/myscripts

#where to backup the directory

tgt=/home/ubuntu/backup_tgt

#created a variable backupfile where called the $tgt as destination

backupfile=$tgt/new_backup.tar.gz

#command used to take the backuptar -czvf $backupfile $src

tar -czvf $backupfile $src

#output of the script:

Automate your scripts and tasks using Cron and Crontab

Cron is a service in Linux that helps users in scheduling their tasks which could be run at specific intervals like hourly, daily, or on weekly basis.

We will use the crontab command to edit the file and automate our backup script

Use the command Crontab -e and it will open an editor:

I used the above command to run the job at every minute followed by the absolute path of my shell script.

Expression of Crontab scheduled task:

  • */1 represents a minute

  • * represents hour

  • * represents day

  • * represents month

  • * represents week

You can modify the scheduled task according to your task requirements

What is User Management?

In Linux, we can manage users by adding, modifying, and deleting user accounts and groups. Below are the commands used respectively for each task:

  1. Creating User: To create a new user we will use useradd command

As a normal user does not have superuser privileges hence sudo is being used to add the user

  1. Password: To create a password for the user we will use passwd command. we can also use the below command to perform password-related tasks
    \> passwd -l : To lock the user's account
    \> passwd -u : To unlock the user's account
    \> passwd - d: To remove the user's password

Display the names of the users you have created:

  • we will use cat /etc/passwd

  • User, I created avnish and Kunal

  1. We can use sudo useradd username -m to create the directory of the user

  1. We can Sudo Visudo command to add the user to get superuser privileges:

In the next lesson, we will be learning permission and Access Control List