Solved

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

Posted on 2004-09-16
12
355 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dog bark java program 15 82
Hibernate methods 2 58
pre4 challenge 19 88
Making an alias 7 54
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now