Difference between revisions of "Debug a script"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 1: Line 1:
 +
{{navigation
 +
|previous=Execute a script
 +
|next=Chapter 2 Challenges}}
 +
 
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>
 
<source lang="bash">bash -x script-name</source>
Line 31: Line 35:
 
* [http://www.cyberciti.biz/tips/debugging-shell-script.html How to debug a Shell]  Script under Linux or UNIX.
 
* [http://www.cyberciti.biz/tips/debugging-shell-script.html How to debug a Shell]  Script under Linux or UNIX.
 
[[Category:Introduction to Shells]]
 
[[Category:Introduction to Shells]]
 +
{{navigation
 +
|previous=Execute a script
 +
|next=Chapter 2 Challenges}}

Revision as of 20:11, 21 September 2009

← Execute a scriptHomeChapter 2 Challenges →

You need to run a shell script with -x option from the command line itself:

bash -x script-name

OR

bash -xv script-name

You can also modify shebang line to run an entire script in debugging mode:

#!/bin/bash -x
echo "Hello ${LOGNAME}"
echo "Today is $(date)"
echo "Users currently on the machine, and their processes:"
w

Use of set builtin command

Bash shell offers debugging options which can be turn 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.
#!/bin/bash
### Turn on debug mode ###
set -x

# Run shell commands
echo "Hello ${LOGNAME}"
echo "Today is $(date)"
echo "Users currently on the machine, and their processes:"
w

### Turn OFF debug mode ###
set +x

# Add more commands without debug mode

External links

← Execute a scriptHomeChapter 2 Challenges →