#90DaysOfDevOps - Day 5: Advanced Linux Shell Scripting for DevOps Engineers with User Management

Introduction

Today is the 5th day of our #90DaysOfDevOps challenge. We will learn about looping in programming, which means repeating certain actions in a sequence. We'll look at how to write looping instructions and see some easy-to-understand examples.

Next, we'll explore the "crontab" utility in Linux. It's a tool that helps schedule tasks to run at specific times automatically.

Lastly, we'll study user management in Linux. This involves controlling who can use the computer system, creating new user accounts, and managing existing ones.

Get ready for a fun and informative day of learning!

Loops in Linux scripting

Loops are fundamental programming constructs used to repeatedly execute a block of code until a certain condition is met. In Linux shell scripting, loops play a crucial role in automating tasks, processing data, and iterating over collections of elements. They allow you to efficiently perform repetitive operations without having to write redundant code.

In Linux, there are primarily two types of loops commonly used in shell scripting:

  1. For Loop:

    The "for" loop is used to iterate over a list of items, such as numbers, file names, or strings. It allows you to execute a set of commands for each item in the list. The basic syntax of the "for" loop is:

     for variable in list
     do
         # commands
     done
    
  2. While Loop:

    The "while" loop executes a set of commands repeatedly as long as a certain condition is true. The basic syntax of the "while" loop is:

     while condition
     do
         # commands
     done
    

Examples

  1. Create multiple directories

    Create a script that allows users to input the desired directory name, starting number, and ending number as arguments, enabling them to set these parameters dynamically.

       1 #!/bin/bash
       2 
       3 #name of the directory
       4 dirName=$1
       5 #starting number
       6 startNum=$2
       7 #ending number
       8 endNum=$3
       9 
      10 for ((num=startNum;num<=endNum;num++))
      11 do
      12     mkdir -p $dirName$num
      13     echo "$dirName$num created"
      14 done
      15 
      16 echo "All directories created successfully."
    

    line 10:

    We can write for loop condition in alternate three expression way. The first num is the initializer, the second num<=endingNum is the condition and the last one num++ is the counting expression.

    line 12:

    Use mkdir command to create the multiple directories

    Output:

     ubuntu@~/scripts$: ./createdirectories.sh prav/temp 1 10
     prav/temp1 created
     prav/temp2 created
     prav/temp3 created
     prav/temp4 created
     prav/temp5 created
     prav/temp6 created
     prav/temp7 created
     prav/temp8 created
     prav/temp9 created
     prav/temp10 created
     All directories created successfully.
     ubuntu@~/scripts$: ls prav/
     temp1  temp10  temp2  temp3  temp4  temp5  temp6  temp7  temp8  temp9
    
  2. Create script to read text from a file

    Create a script to read text from a file and display the test line-by-line.

       1 #!/bin/bash
       2 
       3 #name of the file
       4 filename=$1
       5 
       6 while IFS= read -r line; do
       7     echo "$line"
       8 done < $filename
    

    line 6:
    We use while loop to read the contents of the file $filename . The IFS keyword prevents leading/trailing whitespace from being trimmed.

    Output:

     ubuntu@~/scripts$: cat sample.txt 
     This is first line.
     This is second line.
     This is third line.
     This is fourth line.
     This is fifth line.
     ubuntu@~/scripts$: ./read-lines.sh sample.txt 
     This is first line.
     This is second line.
     This is third line.
     This is fourth line.
     This is fifth line.
    
  3. Create a backup script

    Create a script that will back-up your work into a compressed format in the form of tarball. The user should pass the source directory path that needs to be back-up and the destination directory where the back-up needs to be copied.

       1 #!/bin/bash
       2 
       3 #source back-up directory path
       4 sourcePath=$1
       5 
       6 #destination back-up directory path
       7 destinationPath=$2
       8 
       9 #generate current timestamp
      10 curr_timestamp=$(date +%Y-%m-%d_%H-%M-%S)
      11 
      12 #back-up filename along with the timestamp
      13 back_up_file="$destinationPath/$curr_timestamp.tar.gz"
      14 
      15 echo "Taking backup of $sourcePath at $curr_timestamp"
      16 
      17 #create the tar file
      18 tar cvzf $back_up_file $sourcePath
      19 
      20 echo "Backup completed. The back-up file is $back_up_file"
    

    line 10:

    Getting the current timestamp using the date command. This is used as a unique backup filename so that the user will know when the backup was taken.

    Output:

     ubuntu@~/scripts$: ./createdirectories.sh /home/ubuntu/scripts /home/ubuntu/back-up
     Taking backup of /home/ubuntu/scripts at 2023-08-03_10-21-02
     tar: Removing leading `/' from member names
     /home/ubuntu/scripts/
     /home/ubuntu/scripts/.print.sh.swp
     /home/ubuntu/scripts/sample.txt
     /home/ubuntu/scripts/copydir.sh
     /home/ubuntu/scripts/createdirectories.sh
     /home/ubuntu/scripts/read-lines.sh
     /home/ubuntu/scripts/prav/
     /home/ubuntu/scripts/prav/temp7/
     /home/ubuntu/scripts/prav/temp4/
     /home/ubuntu/scripts/prav/temp5/
     /home/ubuntu/scripts/prav/temp6/
     /home/ubuntu/scripts/prav/temp2/
     /home/ubuntu/scripts/prav/temp8/
     /home/ubuntu/scripts/prav/temp9/
     /home/ubuntu/scripts/prav/temp3/
     /home/ubuntu/scripts/prav/temp10/
     /home/ubuntu/scripts/prav/temp1/
     /home/ubuntu/scripts/print.sh
     /home/ubuntu/scripts/compare_numbers.sh
     /home/ubuntu/scripts/userinput.sh
     Backup completed. The back-up file is /home/ubuntu/back-up/2023-08-03_10-21-02.tar.gz
     ubuntu@~/scripts$: ls /home/ubuntu/back-up/
     2023-08-03_10-21-02.tar.gz
    

Cron and Crontab

Cron plays a vital role in every system by automating scheduled tasks. To manage these tasks, we use the "crontab" command. Making a single cron job is straightforward, but it can get more challenging when handling multiple users and environments. The good news is that the crontab syntax is quite consistent across Linux distributions and servers. Once you grasp it, you'll be able to schedule tasks effortlessly!

Each job is recorded in /etc/crontab on a single line that contains values in a specific order:

[Minute] [Hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]

Values

Minute 0 – 59
Hour 0 – 23
Day of month 1 – 31
Month of year 1 – 12
Day of week 0 – 7 ( 0 and 7 are used for Sunday)

These special symbols can be used as well:

SymbolDescription
*\= “every one”
,used to give multiple values
Indicate a range between two numbers
/Specify a periodicity/frequency using a slash

The crontab command can be used to edit the crontab file crontab -e
To view the crontab entries of current users use the crontab -l
To view the crontab entries of other user use the crontab -u username -l

Examples:

To schedule a cron to execute at 2 am daily
0 2 * * * /bin/sh backup.sh

To schedule a cron to execute twice a day at 5 AM and 5 PM daily
0 5,17 * * * /scripts/script.sh

To schedule a cron to execute every Sunday at 5 PM
0 17 * * sun /scripts/script.sh

Now let's create a cron job and add it to the crontab file to run the backup.sh script (created earlier) to run after every 2 minutes.

ubuntu@~$: crontab -e

The crontab file opens in edit mode and add the line */2 * * * * /home/ubuntu/scripts/backup.sh /home/ubuntu/scripts /home/ubuntu/back-up at the end of the crontab script.

Now, we can check /var/log/syslog whether the job is running every two minutes.

ubuntu@~$: sudo cat /var/log/syslog | grep backup.sh
Aug  3 14:18:01 ip-172-31-56-34 CRON[2905]: (ubuntu) CMD (/home/ubuntu/scripts/backup.sh)
Aug  3 14:20:01 ip-172-31-56-34 CRON[2916]: (ubuntu) CMD (/home/ubuntu/scripts/backup.sh)
Aug  3 14:22:01 ip-172-31-56-34 CRON[2925]: (ubuntu) CMD (/home/ubuntu/scripts/backup.sh)
Aug  3 14:24:01 ip-172-31-56-34 CRON[2933]: (ubuntu) CMD (/home/ubuntu/scripts/backup.sh)
Aug  3 14:26:01 ip-172-31-56-34 CRON[2947]: (ubuntu) CMD (/home/ubuntu/scripts/backup.sh)

User Management

User management in Linux is like being the boss of who can use the computer. You create new users with their own names and passwords, and you can change their info or what they're allowed to do. You can also check the list of all users to see who's using the computer. It's important to do this right to keep the computer safe and make sure everyone can use it nicely.

Create user

sudo useradd user1 -m

We use the useradd command with -m option to create user1 user with default /home/user1 home directory. Adding a new user or doing changes to user requires admin privileges. So use the command with sudo

Update the password of the user

sudo passwd user1

We use the passwd command to update the password of the user1 .

Delete user

sudo userdel user1

We use the userdel command to delete user1.

Let's create two users and display their usernames.

ubuntu@~$: sudo useradd user1 -m
ubuntu@~$: sudo useradd user2 -m
ubuntu@~$: tail -n 2 /etc/passwd | awk -F ':' '{print $1}'
user1
user2

First, we add the two users user1 and user2 using the useradd command. Then we use tail with -n 2 to get the last 2 lines of /etc/passwd file (contains user details). The result is sent as input to the next command awk. We use -F ':' as field separator and print the first column {print $} that gives us the user names.

Conclusion

Today's journey covered diverse Linux skills: mastering loops for efficient programming, crafting backup scripts for data security, automating tasks through cron jobs and crontab, and managing users by creating, modifying, and deleting accounts. A day filled with hands-on learning and growth in the DevOps landscape.

"🌱 Keep learning, and spread the knowledge to inspire others. 🚀💡"

Go back to the main page