LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-19-2006, 11:22 PM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
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
 
Old 02-20-2006, 12:13 AM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

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

Code:
set -x
Regards,

Alunduil
 
Old 02-20-2006, 07:09 AM   #3
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.
 
Old 02-20-2006, 08:48 PM   #4
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 02-20-2006, 08:53 PM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 02-21-2006, 08:08 AM   #6
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.
 
Old 02-21-2006, 08:36 AM   #7
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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).
 
Old 02-21-2006, 10:24 PM   #8
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 02-21-2006, 10:32 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Personally I also use the -v switch like so:
set -xv
v => verbose
 
Old 02-21-2006, 11:49 PM   #10
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 02-22-2006, 12:44 AM   #11
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
sh -x ./scriptname # also works..
 
Old 02-22-2006, 09:09 AM   #12
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.

Last edited by alienDog; 02-22-2006 at 09:10 AM.
 
Old 02-22-2006, 07:30 PM   #13
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
trickykid,


Quote:
Originally Posted by trickykid
sh -x ./scriptname # also works..
I have tried that your method works. Cool!


regards,
George
 
Old 02-22-2006, 07:32 PM   #14
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 02-23-2006, 05:48 AM   #15
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
Maybe contact your ISP?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
How do you debug a bash script meadensi Linux - Newbie 4 02-23-2005 01:47 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration