LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to debug shell script? (https://www.linuxquestions.org/questions/linux-software-2/how-to-debug-shell-script-417374/)

George2 02-19-2006 11:22 PM

How to debug shell script?
 
Hello everyone,


I am using RedHat Shrike 9.0. I am wondering how to debug shell script, for example, step-by-step debug to watch the outputs of each line of command.


thanks in advance,
George

alunduil 02-20-2006 12:13 AM

It's as simple as adding this line to the beginning of your file:

Code:

set -x
Regards,

Alunduil

alienDog 02-20-2006 07:09 AM

or changing

Code:

#!/bin/bash
to

Code:

#!/bin/bash -x
provided it's bash script.

You can also run the script with:

Code:

bash -x [yourscript]
from the commandline.

George2 02-20-2006 08:48 PM

Alunduil,


Quote:

Originally Posted by alunduil
It's as simple as adding this line to the beginning of your file:

Code:

set -x
Regards,

Alunduil


Do you mean this command can enable shell script to run step-by-step (like the step-next feature of an IDE, for example, F6 in Eclipse in debug mode)? I have tried and it only prints out the commands (including output of command) contained in the script file and a sign '+' before each command.


regards,
George

George2 02-20-2006 08:53 PM

alienDog,


Quote:

Originally Posted by alienDog
or changing

Code:

#!/bin/bash
to

Code:

#!/bin/bash -x
provided it's bash script.

You can also run the script with:

Code:

bash -x [yourscript]
from the commandline.

I have tried both methods you mentioned. Do you mean this command can enable shell script to run step-by-step (like the step-next feature of an IDE, for example, F6 in Eclipse in debug mode)? I have tried and it only prints out the commands (including output of command) contained in the script file and a sign '+' before each command.


regards,
George

alienDog 02-21-2006 08:08 AM

Quote:

Originally Posted by George2
alienDog,
I have tried both methods you mentioned. Do you mean this command can enable shell script to run step-by-step (like the step-next feature of an IDE, for example, F6 in Eclipse in debug mode)? I have tried and it only prints out the commands (including output of command) contained in the script file and a sign '+' before each command.
regards,
George

Yes, printing them is what it does. I don't think it's possible to run commands "step by step" with bash, but I'll look into it and get back to you.

alienDog 02-21-2006 08:36 AM

Yep. It not possible with default bash. What I'd suggest is placing a 'read' command to the points where you want the execution to pause. This way the execution stops at that point until you hit enter. You could also consider echoing something appropriate to screen at the certain points of the screen.

Code:

command1
command2
...
echo "Debug message #1 [hit enter to continue]"
read
command3
command3
...
echo "Debug message #2 [hit enter to continue]"
read

It will ease finding errors, though it's not the same as being able to execute the scripts step by step. More help on debugging here:

http://www.tldp.org/LDP/Bash-Beginne...ect_02_03.html

You might also want to take a look at separate bashdb debugger:

http://bashdb.sourceforge.net/bashdb.html

For that you will need bash 3.0 or later (you probably have that already, bash --version will tell).

George2 02-21-2006 10:24 PM

alienDog,


Quote:

Originally Posted by alienDog
Yep. It not possible with default bash. What I'd suggest is placing a 'read' command to the points where you want the execution to pause. This way the execution stops at that point until you hit enter. You could also consider echoing something appropriate to screen at the certain points of the screen.

Code:

command1
command2
...
echo "Debug message #1 [hit enter to continue]"
read
command3
command3
...
echo "Debug message #2 [hit enter to continue]"
read

It will ease finding errors, though it's not the same as being able to execute the scripts step by step. More help on debugging here:

http://www.tldp.org/LDP/Bash-Beginne...ect_02_03.html

You might also want to take a look at separate bashdb debugger:

http://bashdb.sourceforge.net/bashdb.html

For that you will need bash 3.0 or later (you probably have that already, bash --version will tell).

Your method works cool on my Linux desktop! But I can not open the first link you recommended. Can you open it now?


regards,
George

chrism01 02-21-2006 10:32 PM

Personally I also use the -v switch like so:
set -xv
v => verbose

George2 02-21-2006 11:49 PM

chrism01,


Quote:

Originally Posted by chrism01
Personally I also use the -v switch like so:
set -xv
v => verbose

I have tried your method and it works very cool!


regards,
George

trickykid 02-22-2006 12:44 AM

sh -x ./scriptname # also works.. ;)

alienDog 02-22-2006 09:09 AM

Quote:

Originally Posted by George2
alienDog,
Your method works cool on my Linux desktop! But I can not open the first link you recommended. Can you open it now?
regards,
George

Strange, it works fine for me? It's one of the linux documentation project pages, so it should work.

George2 02-22-2006 07:30 PM

trickykid,


Quote:

Originally Posted by trickykid
sh -x ./scriptname # also works.. ;)

I have tried that your method works. Cool!


regards,
George

George2 02-22-2006 07:32 PM

alienDog,


Quote:

Originally Posted by alienDog
Strange, it works fine for me? It's one of the linux documentation project pages, so it should work.

I have tried again but still can not access this URL. Too strange.


regards,
George

alienDog 02-23-2006 05:48 AM

Maybe contact your ISP?


All times are GMT -5. The time now is 01:29 AM.