LinuxQuestions.org
Help answer threads with 0 replies.
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 03-06-2011, 12:48 PM   #1
shayke23
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Rep: Reputation: 0
How to print lines when debugging shell script


Hi

I am testing some bash script using -fx flags:
#!/bin/bash -fx

Now when the program runs i get output in which i need to trace i my script the different lines.
Is there some built-in flag for line numbers output on debugging?

Thx.
 
Old 03-06-2011, 01:07 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Maybe (never tried it) export PS4='$LINENO: '

set -o functrace might help (also never tried it)
 
Old 03-06-2011, 04:27 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
The member GrapefruiTgirl has in the past posted a detailed PS4 line that gives you some useful information about your script:

http://www.linuxquestions.org/questi...1/#post3780940

A while back I modified it into a shell function to make it more convenient to use. I designed it to be added to your bashrc and exported so it becomes part of the shell environment.
Code:
#function for script debugging prompt
function s_dbg {

     export COLOR="\e[1;31m" CLEAR="\e[0m"
     help="$(  echo "Turn a detailed debugging prompt on or off.  Usage:"
               echo -e "${COLOR}\t$FUNCNAME on [verb] | $FUNCNAME off${CLEAR}"
               echo "Prints line# and last exit code value, as well as function name"
               echo "and relative subshell level if it goes above that of the script."
               echo "'verb' is an optional argument that turns on the shell's verbose behavior."
               echo "This function is designed to be loaded from bashrc and exported for use"
               echo "in scripts.  It doesn't do so well when called directly in an interactive shell."
               )"

     export bss=$((BASH_SUBSHELL+1)) #increment by one to counteract embedded test subshell below

     if [[ ! $* ]]; then
          echo "$help" && return
     elif [[ "$*" =~ verb ]]; then
          verb="-v"
     fi

     if [[ "$*" =~ on ]]; then
          export PS4="${COLOR}"'${LINENO}|ex:$?|${FUNCNAME[0]+Fn:${FUNCNAME[0]}|}$((($BASH_SUBSHELL > bss)) && echo "sbshl:$((BASH_SUBSHELL-1))|") '"$CLEAR"
          set -x $verb
          echo "${FUNCNAME} $@"
          echo "shell level:$SHLVL,subshell level:$BASH_SUBSHELL"
     else
          unset PS4
          set +xv
     fi
} 2>/dev/null   #error redirect keeps function from outputting itself

export -f s_dbg   #export above function for use in scripts
I decided to remove $SHLVL from the prompt itself however, as I felt it has very limited informational value; the number only changes when a new bash instance is started after all.

Once exported, just add s_dbg on (verb) and s_dbg off to your script wherever you want the output to appear.
 
Old 03-07-2011, 04:54 AM   #4
shayke23
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thx guys.
I will try this today.

Thx again.
 
  


Reply

Tags
bash, line, number, script, shell


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
Seperating lines in a shell script slackwarefan Linux - Software 12 10-18-2009 04:10 PM
Align lines in shell script jeesun Linux - General 5 10-16-2009 01:29 AM
Shell script to jump over lines horacioemilio Programming 8 12-02-2007 06:44 PM
Shell script debugging rbautch Programming 11 07-20-2006 11:33 PM
Debugging a Bash Shell Script solarblast Linux - General 1 02-23-2006 02:44 AM


All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
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