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
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:
1 2 3 4 5 6 7 8 9 10 11 12 13
# ====================================================================== # 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 # ======================================================================
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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.
Test your skills. Learn something new. Get help. Repeat.Start a FREE 10-day trial