Solved

How to display the line number with in the shell script being executed

Posted on 2004-09-16
12
362 Views
Last Modified: 2013-12-26
I want to know some way to display the line number of the shell script being executed. For example my script might contain identical echo statements. I want to know the output being displayed is a result of which line with in the script.

for example my script might contain wit some conditional statements
echo VALUE IS $AAA
...
....
....
echo VALUE IS $BBB
....

when I run theabove script if I get a response like
VALUE IS ZEBRA
But I have no way of knowing which line resulted in this output. Is there some environmental variable that shows which line it is.

Any tricks/tips?

Regards, Srini

0
Comment
Question by:sitaparijat
  • 7
  • 3
  • 2
12 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 12081160
you can see what lines are being executed by either doing

sh -x scriptname

or putting

set -x

in the script

0
 
LVL 11

Expert Comment

by:avizit
ID: 12081169
if its bash script you can have

set -v in the script

e.g
#!/bin/bash
set -v
export AA=test
export BB=test

echo $AA
echo $BB

this wont give line numbers but wil print each line before executing


0
 
LVL 48

Expert Comment

by:Tintin
ID: 12081176
You can also get a similar result by doing

sh -y scriptname

or setting

set -y
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 11

Expert Comment

by:avizit
ID: 12081187
for csh you can

1. invoke the script as 'csh -v scriptname'

2. have 'set verbose' in the script

in ksh

1. you can try

ksh -x scriptname or ksh -v scriptname




0
 

Author Comment

by:sitaparijat
ID: 12082183
Thanks for the prompt response. I think I did not quite make my self clear. For e.g the shell script is

for example my script might contain wit some conditional statements (with line num shown)
10 echo VALUE IS $AAA
...
....
....
30 echo VALUE IS $BBB
....

I want to tweak the script in such a way that when I run it it should show some thing like
Sciprt name(10):  VALUE IS ZEBRA

or
Sciprt name(30) VALUE IS ZEBRA

depending on the line that gave the output. Some thing like how the errors are indicated by shell e.g.
pp.sh[461]: test: Specify a parameter with this command.

The solutions you provided are good but when the script is a big noodle ( I am an Asian :-)) will result in so many lines to be reviewed. I hope you get the idea.

0
 
LVL 11

Accepted Solution

by:
avizit earned 125 total points
ID: 12082203
#!/bin/bash
set -v
export AA=test
export BB=test

echo "$LINENO : $AA"
echo "$LINENO : $BB"
0
 
LVL 11

Expert Comment

by:avizit
ID: 12082252
you can remove the "set -v" from above as it is no longer necessary ,

the only problem with doing things this way is that now you have to modify wherever
there is an "echo" .....

0
 

Author Comment

by:sitaparijat
ID: 12082417
AVIZIT,
This is what I was looking for,

 I created a new function
Debug()
{
if [ $DEBUG = "Y" ]
then
echo "DEBUG:" $*
echo
fi
}

which looks at an option argumanet if it is 'Y' it will print the line if I can echo the line number.

I search for an option -d
while [ -n "$1" ]; do
case $1 in
-d) DEBUG=$2;export DEBUG;shift 2;; # -d takes an argument X or Y -> shift by 2
*)  break;;
esac
done


In my script I use my own function like below:
Debug $LINENO : PKMS_ENV:$PKMS_ENV

This makes troubleshooting easy,

There is a small issue, which I hoped I can make only change in the Debug function & would work but did not.  I can not put the $LINENO in the Debug function directly , like below:
Debug()
{
if [ $DEBUG = "Y" ]
then
echo "DEBUG:" $LINENO  $*
echo
fi
}

if I do it always shows the line number of the Debug function....Any suggestion for this?

Thanks
0
 
LVL 11

Expert Comment

by:avizit
ID: 12082761
I got your problem somewhat , but sorry to say I dunno what can be done at the moment ,

but can you write a small script which demonstrates your case i.e with a Debug() function and a call to it etc .

I will ask my friends if they know ..
0
 
LVL 11

Expert Comment

by:avizit
ID: 12082765
though this might help

from
http://www.faqs.org/faqs/unix-faq/shell/bash/

B1) What's new in version 2.05b?
.....
o the expansion of $LINENO inside a shell function is only relative to the
  function start if the shell is interactive -- if the shell is running a
  script, $LINENO expands to the line number in the script.  This is as
  POSIX-2001 requires
....

0
 

Author Comment

by:sitaparijat
ID: 12098898
Hi Avizit,
my script is somewhat long can I email as an attachment?
Srini
0
 
LVL 11

Expert Comment

by:avizit
ID: 12098914
can you just write a small version which demonstrate the problem ?
secondly from my previous post , it looks like it cannot be done easily ..
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question