hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will talk about debugging our script using bash right so sometimes when things don't go according to the plan you need to determine what exactly causes the script to fail and bash provides extensive debugging feature the most common is to start up the sub shell with the minus X option okay so there are some you know options we can use to debug our script now this is the script I have written in the last blog and this is nothing but just looping around a while loop and printing the value of count right and if any error occurs if the error is - and if the script run successfully we are trapping this thing right this signal right so this was the previous script we were running now normally when you run the script you can just run it like this and it will execute you know perfectly fine and you need don't need to do anything but sometimes sometimes you forgot for example this parenthesis and when I run the script once again I can see the error right so this time it's pretty straightforward and it's a syntax error so it's a syntax error near unexpected token done which is on line 11 so 11 is here right and the problem is here but we are pretty close because the problem is in the while loop right so it's pretty close so bash will give you the you know error and even line number where the problem is coming but if you want to debug more you and use this minus X option so you can run the script with this and then you just need to give for example bash - X option right so just write bash - X and the name of your script and then press Enter and now you will be able to you know see more both operations so you can see what happens first the file is declared and then the trap is set and then the pre ID is created so PID is printed so whatever you know steps or commands are executed in sequence is going to print all of it right so you can see now we are in the you know this while loop and then the sleep of 10 second comes and then you know the counter is increased and then the you know value of count is printed right so whatever steps are happening it's printing everything so in this way whenever some problems occurs you will be straightaway be aware that your script is not working according to the plan and the problem is coming from here okay so this is one option you can use to debug your script so what is the option option is let me clear the terminal and the option is bash - X and then the name of your script so it will run your script with
this debug option and it's going to give you step by step what's happening in your script okay now there is the similar option in your bash script also you can apply here so instead of writing minus X here for example you want to run this script like this and you don't want to use in the command line minus X right so how you can use this debugging inside your script so what you need to do is you need to use the set option okay so set option can be given like this so first of all you can write minus X at the top here also so after this hash-bang bin bash when you write minus X here then when you run the script it will give you the similar you know output okay so you can use minus X option at the top here also okay so this is one way of using minus X the other way was using the terminal right and let me show you the another way of using this minus X which is by using the set option so you can do set minus X and then it will start your debugging from the point where you set minus X okay so what is set minus X so set minus at X activates debugging from the point it's written right so from here activate debugging now there is one more option which is set loss X so you can just write set plus X at any point in the script so just try it like this and it deactivates or stop your debugging from this point wherever plus X is set okay so let me run the script once again and let's see what happens so you can see for two lines until the line six we saw this kind of you know debugging and then the script is running normally okay so it's not going to show you the you know debugging output kind of a thing okay so this is also how you can debug your script so there are basically three ways I have shown you one is by using Bosch minus X and then the script name of your script you can directly give in the terminal debugging option and then it will run fine other is by just adding minus X at the top after shabang bin forward slash bash and then this option then the editing option will be activated and by using the set minus X which starts the debugging from this point and set plus X which stop the debugging at the point where it's written okay so these are some of the debugging options you can use in your bash scripting I hope you have enjoyed this blog
Post a Comment