Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Shell Scripting Cookbook

You're reading from   Linux Shell Scripting Cookbook Do amazing things with the shell and automate tedious tasks

Arrow left icon
Product type Paperback
Published in May 2017
Publisher
ISBN-13 9781785881985
Length 552 pages
Edition 3rd Edition
Tools
Arrow right icon
Authors (3):
Arrow left icon
Clif Flynt Clif Flynt
Author Profile Icon Clif Flynt
Clif Flynt
Sarath Lakshman Sarath Lakshman
Author Profile Icon Sarath Lakshman
Sarath Lakshman
Shantanu Tushar Shantanu Tushar
Author Profile Icon Shantanu Tushar
Shantanu Tushar
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Shell Something Out FREE CHAPTER 2. Have a Good Command 3. File In, File Out 4. Texting and Driving 5. Tangled Web? Not At All! 6. Repository Management 7. The Backup Plan 8. The Old-Boy Network 9. Put On the Monitors Cap 10. Administration Calls 11. Tracing the Clues 12. Tuning a Linux System 13. Containers, Virtual Machines, and the Cloud

Debugging the script

Debugging frequently takes longer than writing code. A feature every programming language should implement is to produce trace information when something unexpected happens. Debugging information can be read to understand what caused the program to behave in an unexpected fashion. Bash provides debugging options every developer should know. This recipe shows how to use these options.

How to do it...

We can either use Bash's inbuilt debugging tools or write our scripts in such a manner that they become easy to debug; here's how:

  1. Add the -x option to enable debug tracing of a shell script.
        $ bash -x script.sh

Running the script with the -x flag will print each source line with the current status.

You can also use sh -x script.
  1. Debug only portions of the script using set -x and set +x. Consider this example:
        #!/bin/bash 
        #Filename: debug.sh 
        for i in {1..6}; 
        do 
            set -x 
            echo $i 
            set +x 
        done 
        echo "Script executed"

In the preceding script, the debug information for echo $i will only be printed, as debugging is restricted to that section using -x and +x.
The script uses the {start..end} construct to iterate from a start to end value, instead of the seq command used in the previous example. This construct is slightly faster than invoking the seq command.

  1. The aforementioned debugging methods are provided by Bash built-ins. They produce debugging information in a fixed format. In many cases, we need debugging information in our own format. We can define a _DEBUG environment variable to enable and disable debugging and generate messages in our own debugging style.

Look at the following example code:

        #!/bin/bash 
        function DEBUG() 
        { 
            [ "$_DEBUG" == "on" ] && $@ || : 
        } 
        for i in {1..10} 
        do 
          DEBUG echo "I is $i" 
        done

Run the preceding script with debugging set to "on":

        $ _DEBUG=on ./script.sh

We prefix DEBUG before every statement where debug information is to be printed. If _DEBUG=on is not passed to the script, debug information will not be printed. In Bash, the command : tells the shell to do nothing.

How it works...

The -x flag outputs every line of script as it is executed. However, we may require only some portions of the source lines to be observed. Bash uses a set builtin to enable and disable debug printing within the script:

  • set -x: This displays arguments and commands upon their execution
  • set +x: This disables debugging
  • set -v: This displays input when they are read
  • set +v: This disables printing input

There's more...

We can also use other convenient ways to debug scripts. We can make use of shebang in a trickier way to debug scripts.

Shebang hack

The shebang can be changed from #!/bin/bash to #!/bin/bash -xv to enable debugging without any additional flags (-xv flags themselves).

It can be hard to track execution flow in the default output when each line is preceded by +. Set the PS4 environment variable to '$LINENO:' to display actual line numbers:

PS4='$LINENO: ' 

The debugging output may be long. When using -x or set -x, the debugging output is sent to stderr. It can be redirected to a file with the following command:

sh -x testScript.sh 2> debugout.txt

Bash 4.0 and later support using a numbered stream for debugging output:

exec 6> /tmp/debugout.txt 
BASH_XTRACEFD=6
You have been reading a chapter from
Linux Shell Scripting Cookbook - Third Edition
Published in: May 2017
Publisher:
ISBN-13: 9781785881985
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image