CST 8177 - Lab 8

Automating Tasks: homebak

Student Name

Student number

Lab section




Objectives

  1. To learn and understand basic bash scripting language

  2. To automate tasks

  3. To develop basic PDL

Lab Outcome

A good working understanding of how shell scripting works and how to create simple task automation

In-Lab Demo - Demo the homebak script and explain its commands.

Part I: Basic bash scripting

Exercise #1: Creating a script

In this exercise, you will write a script called greetings.

Note: Scripts that are created on a local machine are typically stored in /usr/local/bin or /usr/local/sbin depending and who may run the script. Before you name a script, verify with the command whereis if the name has already been used for a command. If it has, choose a different name for the script. Personal scripts are often placed in ~/bin.

The script will contain a comment section with:

Note: Make certain you have the above information in every script file you create from now on.

Your script will:



To accomplish this follow the steps below:

Step #1

Step #2

#! /bin/bash

Step #3

Add the lines indicated below. Ensure that your script shows your OWN information!

# Name: greetings

# Course: CST8177 - Linux 2

# (your name) - Student ID (your id)

# Purpose: greet the user and display some information

Step #4

Tip: Execute printenv and use grep to search for the entries that contain the account name.

Example: echo $USER

Step #5

Next, you want to print the date and time. This information can be obtained using the date(1) command, with some options thrown in for formatting to make it look good. Re-open your file and add the next command:

Example: Enter the following line exactly as shown below.

echo "It is $(date +%A" "%e" "%B" "%Y", "%R" "%p)."

Note: Notice that we put the date command inside round brackets and preceded by a $ sign, to tell the shell that this is a command to be executed outside the scope of the echo command and for its stdout to be returned.

Now run it to make sure it works properly.

Step #6

Next you want to display a list of all files in the user's home directory using the appropriate environment variable.

Save the file, exit and run it again to make sure it works properly.

Step #7

You want to show the search path.

Note: To display the search path use the echo command to display the contents of the environment variable used for the search path: PATH.

Save the file, exit and run it again to make sure it works properly.

Step #8

Display the time and date again after waiting 1 minute (note: don't actually wait for 1 minute - that's a very long time; test it with only 3 to 5 seconds instead).

Note: Use the sleep(1) command.

Save the file, exit and run it again to make sure it works properly.

Step #9: Installing and testing the script

Copy the script to the /usr/local/bin directory.

Log in as a different user and execute the command.

Step #10

The PDL or description for this script is simple because the flow of execution (flow control) is sequential.

START greetings

DISPLAY user name

DISPLAY date

SHOW user's home directory

SHOW current search path

WAIT for a few seconds

DISPLAY date

END greetings

The complete script is shown below:

# Name: greetings

# Course: CST8177 - Linux 2

# (your name) - Student ID (your id)

# Purpose: greet the user and display some information

echo "Hi $LOGNAME!"

echo "It is $(date +%A", "%B" "%e" "%Y)."

ls $HOME

echo $PATH

sleep 3

date



Part II: Automating administrative tasks

Back up all home directories at regular intervals

Specifications

Create a small script, named homebak (for home backup) that accomplishes all of the following (create a script header as above):

Solution

Example: State that the script is preventing users from logging in, but not how this is implemented: "PREVENT user logins"



Result

Print out your script and any other material for inclusion in your Lab Book.

Record the output of an ls -l for your new home backup .tgz file:

______________________________________________________________________________

Record the entry in rsyslog.conf file if you needed to add one:

______________________________________________________________________________

Describe your scheduling setup, recording any entries made here:

______________________________________________________________________________

You may have had a problem with the scheduling. How did you fix it?

______________________________________________________________________________

______________________________________________________________________________

You have created a new log file (userlog). Next you have to manage the number of log files kept and their frequency using - name the tool to use:

______________________________________________________________________________

Identify any files that need to be modified to enable log rotation for the newly created log file and record your changes:

______________________________________________________________________________

______________________________________________________________________________