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.
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
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
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:
1# ====================================================================== 2# SCRIPT NAME: systeminfo.sh 3 4# PURPOSE: Demonstrate simple Bash programming concepts 5 6# REVISION HISTORY: 7 8# AUTHOR DATE DETAILS 9# --------------------- --------------- -------------------------------- 10# Gabriel A. Cánepa 2017-10-21 Initial version 11 12# LICENSE: CC Attribution-ShareAlike 4.0 International 13# ======================================================================
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 can be any command executed by the shell. To use the contents of MYVAR in the script, add
$MYVAR wherever it is needed.
1echo "Starting to run the script..." 2# VARIABLE ASSIGNMENT 3# Show hostname: 4HOST=$(hostname) 5# User executing the script: 6CURRENTUSER=$(whoami) 7# Current date: 8CURRENTDATE=$(date +%F) 9# Host IP address: 10IPADDRESS=$(hostname -I | cut -d ' ' -f1) 11 12# SHOW MESSAGES 13echo "Today is $CURRENTDATE" 14echo "Hostname: $HOST ($IPADDRESS)" 15echo "User info for $CURRENTUSER:" 16grep $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:
1chmod a+x systeminfo.sh 2./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.