Difference between revisions of "Debug a script"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
m (Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >")
Line 4: Line 4:
  
 
You need to run a shell script with -x option from the command line itself:
 
You need to run a shell script with -x option from the command line itself:
<source lang="bash">bash -x script-name</source>
+
<syntaxhighlight lang="bash" >bash -x script-name</source>
 
OR
 
OR
<source lang="bash">bash -xv script-name</source>
+
<syntaxhighlight lang="bash" >bash -xv script-name</source>
 
You can also modify [[shebang]] line to run an entire script in debugging mode:
 
You can also modify [[shebang]] line to run an entire script in debugging mode:
<source lang="bash">#!/bin/bash -x
+
<syntaxhighlight lang="bash" >#!/bin/bash -x
 
echo "Hello ${LOGNAME}"
 
echo "Hello ${LOGNAME}"
 
echo "Today is $(date)"
 
echo "Today is $(date)"
Line 18: Line 18:
 
* set -v : Display shell input lines as they are read.
 
* set -v : Display shell input lines as they are read.
 
* set -n : Read commands but do not execute them.  This may be used to check a shell script for syntax errors.
 
* set -n : Read commands but do not execute them.  This may be used to check a shell script for syntax errors.
<source lang="bash">#!/bin/bash
+
<syntaxhighlight lang="bash" >#!/bin/bash
 
### Turn on debug mode ###
 
### Turn on debug mode ###
 
set -x
 
set -x
Line 34: Line 34:
  
 
Another example using set -n and set -o noexec:
 
Another example using set -n and set -o noexec:
<source lang="bash">#!/bin/bash
+
<syntaxhighlight lang="bash" >#!/bin/bash
 
set -n # only read command but do not execute them
 
set -n # only read command but do not execute them
 
set -o noexec
 
set -o noexec

Revision as of 22:49, 29 March 2016

← Execute a scriptHomeChapter 2 Challenges →

You need to run a shell script with -x option from the command line itself: <syntaxhighlight lang="bash" >bash -x script-name</source> OR <syntaxhighlight lang="bash" >bash -xv script-name</source> You can also modify shebang line to run an entire script in debugging mode: <syntaxhighlight lang="bash" >#!/bin/bash -x echo "Hello ${LOGNAME}" echo "Today is $(date)" echo "Users currently on the machine, and their processes:" w</source>

Use of set builtin command

Bash shell offers debugging options which can be turned on or off using set command.

  • set -x : Display commands and their arguments as they are executed.
  • set -v : Display shell input lines as they are read.
  • set -n : Read commands but do not execute them. This may be used to check a shell script for syntax errors.

<syntaxhighlight lang="bash" >#!/bin/bash

      1. Turn on debug mode ###

set -x

  1. Run shell commands

echo "Hello $(LOGNAME)" echo "Today is $(date)" echo "Users currently on the machine, and their processes:"

      1. Turn OFF debug mode ###

set +x

  1. Add more commands without debug mode

</source>

Another example using set -n and set -o noexec: <syntaxhighlight lang="bash" >#!/bin/bash set -n # only read command but do not execute them set -o noexec echo "This is a test"

  1. no file is created as bash will only read commands but do not executes them

>/tmp/debug.txt</source>

See also

External links

← Execute a scriptHomeChapter 2 Challenges →