Advanced scripting continued Flashcards
(15 cards)
What is print/trace/x-debugging?
-x at the beginning of your code after your shebang:
#!/bin/bash -x
To do this for only a portion, do this at the beginning and end of the code you want to debug.
set -x
code
set +x
prints commands and their arguments as they are executed. Variable’s will show up as their value. This makes it easy to see what’s going on. This is called variable and wildcard expansion
Have your script close upon an error (anything on returning a return code of 0). Combine this with your debugger option
!/bin/bash -xe
Show every line of the script before it is displayed. Do not print with variable or wildcard expansion
!/bin/bash -v
combining with the x, you can see what the output is too
Show all the debugging options
help set
Create a boolean that runs a command
DEBUG=true
$DEBUG&&echo ‘debug is on’
DEBUG=false
$DEBUG||echo ‘debug is off’
This works because bash reads this as a rc. So if seomthing’s true, the and statement goes next.
If something is false, it goes to try the next code.
Show a simple way of turning a debugging menu off and on with a variable
DEBUG=echo
$DEBUG ls
Since we’ve made DEBUG into a comment, it will give us nothing, thus allowing the ls command to run.
If the debug is on, then it will echo all parts of the script.
Show a function that shows the command before using it
fucntion debug() {
echo ‘Executing $@’
$@
}
What is the PS4 varaible?
It is displayed prior to your commands when using -x.
By default it is a plus sign, but we can change that.
Variable that shows your script name
$BASH_SOURCE
variable to show current script line
$LINENO
What two variables can we combine to show the script name and line number when running -x?
!/bin/bash -x
PS4=’+ $BASH_SOURCE: $LINENO:’
What is Line Feed and Carriage return?
All files have them
Linux has Line Feeds
Windows has Carriage Returns and line feeds
Found at end of sentences
show them with
cat -v script.sh (displays non-printable characters)
^M <- Carriage Return
What are the two ways to find carriage returns?
cat -v script.sh
file script.sh
How do you fix carriage returns?
dos2unix
unix2dos