Advanced scripting continued Flashcards

(15 cards)

1
Q

What is print/trace/x-debugging?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Have your script close upon an error (anything on returning a return code of 0). Combine this with your debugger option

A

!/bin/bash -xe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show every line of the script before it is displayed. Do not print with variable or wildcard expansion

A

!/bin/bash -v

combining with the x, you can see what the output is too

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Show all the debugging options

A

help set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Create a boolean that runs a command

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show a simple way of turning a debugging menu off and on with a variable

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show a function that shows the command before using it

A

fucntion debug() {
echo ‘Executing $@’
$@
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the PS4 varaible?

A

It is displayed prior to your commands when using -x.
By default it is a plus sign, but we can change that.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Variable that shows your script name

A

$BASH_SOURCE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

variable to show current script line

A

$LINENO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What two variables can we combine to show the script name and line number when running -x?

A

!/bin/bash -x

PS4=’+ $BASH_SOURCE: $LINENO:’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Line Feed and Carriage return?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the two ways to find carriage returns?

A

cat -v script.sh
file script.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you fix carriage returns?

A

dos2unix
unix2dos

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly