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

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

sitaparijatAsked:
Who is Participating?
 
avizitConnect With a Mentor Commented:
#!/bin/bash
set -v
export AA=test
export BB=test

echo "$LINENO : $AA"
echo "$LINENO : $BB"
0
 
TintinCommented:
you can see what lines are being executed by either doing

sh -x scriptname

or putting

set -x

in the script

0
 
avizitCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TintinCommented:
You can also get a similar result by doing

sh -y scriptname

or setting

set -y
0
 
avizitCommented:
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
 
sitaparijatAuthor Commented:
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
 
avizitCommented:
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
 
sitaparijatAuthor Commented:
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
 
avizitCommented:
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
 
avizitCommented:
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
 
sitaparijatAuthor Commented:
Hi Avizit,
my script is somewhat long can I email as an attachment?
Srini
0
 
avizitCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.