Difference between revisions of "Debug a script"
Jump to navigation
Jump to search
m (Text replacement - "http://www.cyberciti.biz/" to "https://www.cyberciti.biz/") |
|||
(8 intermediate revisions by 5 users not shown) | |||
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: | ||
− | < | + | <syntaxhighlight lang="bash" >bash -x script-name</syntaxhighlight> |
OR | OR | ||
− | < | + | <syntaxhighlight lang="bash" >bash -xv script-name</syntaxhighlight> |
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: | ||
− | < | + | <syntaxhighlight lang="bash" >#!/bin/bash -x |
echo "Hello ${LOGNAME}" | echo "Hello ${LOGNAME}" | ||
echo "Today is $(date)" | echo "Today is $(date)" | ||
echo "Users currently on the machine, and their processes:" | echo "Users currently on the machine, and their processes:" | ||
− | w</ | + | w</syntaxhighlight> |
==Use of set builtin command== | ==Use of set builtin command== | ||
− | Bash shell offers debugging options which can be | + | 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 -x : Display commands and their arguments as they are executed. | ||
* 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. |
+ | <syntaxhighlight lang="bash" >#!/bin/bash | ||
### Turn on debug mode ### | ### Turn on debug mode ### | ||
set -x | set -x | ||
# Run shell commands | # Run shell commands | ||
− | echo "Hello $ | + | echo "Hello $(LOGNAME)" |
echo "Today is $(date)" | echo "Today is $(date)" | ||
echo "Users currently on the machine, and their processes:" | echo "Users currently on the machine, and their processes:" | ||
− | |||
### Turn OFF debug mode ### | ### Turn OFF debug mode ### | ||
Line 31: | Line 31: | ||
# Add more commands without debug mode | # Add more commands without debug mode | ||
− | </ | + | </syntaxhighlight> |
+ | |||
+ | 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" | ||
+ | # no file is created as bash will only read commands but do not executes them | ||
+ | >/tmp/debug.txt</syntaxhighlight> | ||
+ | |||
+ | ==See also== | ||
+ | * [[Setting shell options]] | ||
+ | |||
==External links== | ==External links== | ||
− | * [ | + | * [https://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 | {{navigation | ||
|previous=Execute a script | |previous=Execute a script | ||
|next=Chapter 2 Challenges}} | |next=Chapter 2 Challenges}} |
Latest revision as of 15:27, 17 July 2017
← Execute a script • Home • Chapter 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 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.
#!/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:"
### Turn OFF debug mode ###
set +x
# Add more commands without debug mode
Another example using set -n and set -o noexec:
#!/bin/bash
set -n # only read command but do not execute them
set -o noexec
echo "This is a test"
# no file is created as bash will only read commands but do not executes them
>/tmp/debug.txt
See also
External links
- How to debug a Shell Script under Linux or UNIX.