Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Shell Scripting With Bash For Linux Administration - Part 1

Sep 5, 2018 • 4 Minute Read

Set Up

For additonal context to set up your workspace, please view the first guide in this series: User and Group Management in Linux.

Shell Scripting with Bash

Shell scripts are plain text files that contain a sequence of commands that are run by a shell, one after another. Bash is the default shell in most modern Linux distributions and we will leverage its programming capabilities to create simple scripts. As we gain experience, we can use what we have learned to develop more robust programs.

System administrators often use shell scripts to automate routine tasks. As a rule of thumb, if a task has to be performed periodically (even when it's only once a month), it needs to be automated.

Structure of a Shell Script

The first line in a shell script must indicate the shell (also known as interpreter) that will be used to execute it. For Bash, this means

      #!/bin/bash
    

The above line must be followed by the commands that should be run by the shell, one per line. Although preferences between system administrators may vary, effective and well-maintained shell scripts often include the following sections:

The script header is a commented-out section where the developer can include items such as:

  • Description/purpose of the script

  • Revision history

  • License terms and/or copyright notice

The shell ignores blank and commented-out lines. The former are only meant as information for the author, other reviewers, and people using the program. To comment out a line, simply place a # sign at the beginning.

A common header looks as follows:

      # ======================================================================
# SCRIPT NAME: systeminfo.sh

# PURPOSE: Demonstrate simple Bash programming concepts

# REVISION HISTORY:

# AUTHOR				DATE			DETAILS
# --------------------- --------------- --------------------------------
# Gabriel A. Cánepa	 2017-10-21	  Initial version

# LICENSE: CC Attribution-ShareAlike 4.0 International
# ======================================================================
    

Body

The body of the script is where the sequence of commands is placed, one per line. A command can be executed directly by the shell or have its output be saved into a container known as variable. You can think of variables as boxes where we can store a fixed value (such as text or a number) or the output of a command, for later reuse.

As a best practice, system administrators often use comments in the body to indicate what a given line of code is supposed to do. This also serves as a reminder for himself/herself and others who will later work on the same file.

To store the output of a command in a variable, enclose the command between parentheses and preface them with the dollar sign. Thus, in MYVAR=$(command), the variable MYVAR contains the output of command, where command can be any command executed by the shell. To use the contents of MYVAR in the script, add $MYVAR wherever it is needed.

For example:

      echo "Starting to run the script..."
# VARIABLE ASSIGNMENT
# Show hostname:
HOST=$(hostname)
# User executing the script:
CURRENTUSER=$(whoami)
# Current date:
CURRENTDATE=$(date +%F)
# Host IP address:
IPADDRESS=$(hostname -I | cut -d ' ' -f1)

# SHOW MESSAGES
echo "Today is $CURRENTDATE"
echo "Hostname: $HOST ($IPADDRESS)"
echo "User info for $CURRENTUSER:"
grep $CURRENTUSER /etc/passwd
    

Let's put everything together (#!/bin/bash, header, and body) and save as systeminfo.sh. Next, make the file executable and run it:

      chmod a+x systeminfo.sh
./systeminfo.sh
    

The following image shows the output of the script:

As you can see in the above image, the echo Bash built-in allows us to embed variables and fixed text inside double quotes. Thus, the content of the given variable(s) will be part of the output, along with the text.

Next Steps

Please continue on to the next guide in this series: Shell Scripting With Bash For Linux Administration - Part 2.

Gabriel Cánepa

Gabriel C.

Written Content Author

More about this author