0:00
hey everyone welcome back to our channel
0:02
your go-to place for Linux DevOps and
0:04
Cell scripting tutorials In today's
0:07
video we will show you how to debug a
0:09
cell script line by line like a pro
0:11
Whether you are just starting out or
0:13
want to master debugging this video will
0:16
give you the tools you need In this
0:18
video we will cover how to enable
0:20
debugging mode in Cellcript using set- X
0:24
set V and B - X methods Why debugging
0:30
cell script can fail silently or behave
0:33
unexpectedly Debugging helps you to
0:35
catch errors early understand the flow
0:37
and fix issues faster Let's create a
0:40
sample script with the name
0:42
backup.sh For the interest of time I
0:45
have already created a script with the
0:50
backup.sh As you can see this script is
0:53
supposed to back up the files from
0:55
document folder to the backup directory
0:57
For this I have defined a source
0:59
directory which is /homin key documents
1:02
and backup directory is
1:05
/ware/temp/backup and we are using copy
1:08
command to take the backup of this
1:09
document folder to the backup folder
1:12
Let's say it is not working as expected
1:15
Let's debug it First try to execute the
1:20
script So it is failing at the cp
1:24
command In order to enable the debugging
1:28
let's use the first method that is set -
1:30
x which is also known as exact trace For
1:34
this we need to edit the script And at
1:36
the beginning of script we need to add a
1:39
parameter set x Use vi
1:49
set x Go to the end of the file
1:58
X This set X will enable or we can say
2:03
that it will start the debugging mode It
2:06
will print each command before it is
2:08
executed It is great for tracing the
2:10
flow and checking variable values This
2:13
set plus X is used to disable the
2:16
debugging mode Let's save and close the
2:21
Now again execute the
2:25
script You can see it has executed the
2:28
eco command successfully It has exported
2:31
source_dir variable along with
2:35
backup_dir and it has successfully
2:40
command While executing this cp command
2:44
it is throwing an error It says no such
2:47
file or directory means issue is with
2:50
this source Let's try to see the exact
2:54
source path If you do
3:02
techy we don't have any documents folder
3:05
with the name documents Let's try with
3:10
d All right So we know the issue is with
3:15
this small day We need to change it to
3:18
caps Again use vi editor Edit the
3:24
script Change this to
3:28
caps Save and close the file Rerun the
3:33
script This time you should see that the
3:36
command is executed successfully And we
3:39
are also getting a message that backup
3:41
is completed successfully You can also
3:44
verify it using the command
3:50
ls-l/v/temp/backup and there should be a
3:54
documents Great So we can say that our
3:58
cell script issue has been fixed as it
4:00
has been executed successfully Second
4:02
method for debugging the cell script is
4:04
using by- x parameter This is the quick
4:08
way to debug without modifying the
4:10
script It will show you the same
4:12
step-by-step output in your
4:14
terminal Let's disable this exact trace
4:22
first Run the command p space-
4:28
small and then script name
4:31
backup.sh If you hit enter you will see
4:36
executed and we can see the output of
4:41
terminal So using B - X we can also
4:44
debug our shell script The same way that
4:47
we did it using exact trace or set X
4:53
Apart from these methods there is one
4:55
more method that we can use for
4:57
debugging the cell script that is set
4:59
hyphen v parameter which is also known
5:01
as verbose It shows the line as they are
5:04
read even before the execution It is
5:06
generally useful when debugging syntax
5:08
issues or loops For this verbose or or
5:12
the set v method I have a script with
5:19
verbose.sh In order to enable the
5:21
verbose set the parameter set
5:33
script You can see that it has printed
5:36
the command before it is executed And
5:40
after the execution it has also printed
5:44
command That's all from this video If
5:47
you found this helpful give it a thumbs
5:49
up Subscribe for more Linux tips and
5:51
drop your questions in the comment
5:52
section below See you in the next video