LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-4175654430/)

jyothinagendra 05-23-2019 03:06 AM

Need help
 
Hi I am newbie to Linux and I need to learn Linux programming .
I need to write a function and try to debug that fuction .
How can I debug the function in Shell script .
Please help me on this .

hydrurga 05-23-2019 03:09 AM

Hi jyothinagendra, and welcome to LQ.

You need to give us more information on what you are trying to achieve. Which script are you trying to debug?

Also, in future, I would suggest that you use a more helpful thread title. "Need help" is not very useful and tells people nothing about your issue before they open the thread. "Need help with debugging Shell script" would, for example, have been better.

berndbausch 05-23-2019 05:43 AM

Quote:

Originally Posted by jyothinagendra (Post 5998023)
How can I debug the function in Shell script .

You can use set -x in the shell script, which makes the shell print each command as it is executed. This is switched off with set +x. Running the script with the -x option (sh -x SCRIPT) has the same effect.

The other debugging tool is echo.

The Bash guide in my signature also has a debugging section that suggests more tools: https://mywiki.wooledge.org/BashGuid...ices#Debugging.

In case you have a particular problem you need to solve, you can also describe it here, as hydrurga suggests.

Turbocapitalist 05-23-2019 05:50 AM

In addition to the above useful replies, especially set -x, I'd also point you to the ShellCheck web site.

If you're looking for online guides, I'd recommend these resources, among others:

What have you gotten written so far and where are you stuck? If you post your script here for assistance, please remember to wrap it in [code] [/code] tags so that the indentation is retained.

rtmistler 05-23-2019 08:26 AM

Welcome to LQ.

One of the links in my signature covers bash script debugging methods.

Enjoy!

Shadow_7 05-23-2019 08:38 AM

For bash you run it with -x. Or debug it the hard way by adding echo's after every line to show location and values of interest. For other compiled languages you need to use gdb. Plus compile the program with debugging turned on.

JJJCR 05-24-2019 03:50 AM

Quote:

Originally Posted by berndbausch (Post 5998055)
You can use set -x in the shell script, which makes the shell print each command as it is executed. This is switched off with set +x. Running the script with the -x option (sh -x SCRIPT) has the same effect.

The other debugging tool is echo.

The Bash guide in my signature also has a debugging section that suggests more tools: https://mywiki.wooledge.org/BashGuid...ices#Debugging.

In case you have a particular problem you need to solve, you can also describe it here, as hydrurga suggests.

Agree with "echo", putting it on the right place to spit out value or message is a good way to debug. Of course, mileage will vary you can't put the echo just anywhere.

This link might help: http://tldp.org/LDP/Bash-Beginners-G...ect_02_03.html

chrism01 05-28-2019 12:21 AM

Personally I like 'set -xv' for a bit more info - YMMV :)

Shadow_7 05-28-2019 04:17 AM

Code:

#!/bin/bash

PARM1="b0rked"
PARM2="fitz"
f_HW() {
  echo "Hello World"
  echo $PARM1" "$PARM2
}

PARM1="broke"
PARM2="fix"
f_HW

exit 0

*sigh* homework... Do the kids even do that anymore?

*sigh* books... Do kids even know what those are? At work we flip material "like a book" to print on the second side correctly. The blank stares I get when I explain, like a ????.

JJJCR 05-28-2019 11:59 PM

Quote:

Originally Posted by Shadow_7 (Post 5999640)
Code:

#!/bin/bash

PARM1="b0rked"
PARM2="fitz"
f_HW() {
  echo "Hello World"
  echo $PARM1" "$PARM2
}

PARM1="broke"
PARM2="fix"
f_HW

exit 0

*sigh* homework... Do the kids even do that anymore?

*sigh* books... Do kids even know what those are? At work we flip material "like a book" to print on the second side correctly. The blank stares I get when I explain, like a ????.

Oops..good spot. The poster ask the question like a pro. hahaha.

homework... Do the kids even do that anymore? Yes they do, they use Wat - t - pad hahaha


All times are GMT -5. The time now is 12:14 PM.