Printing the line number in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing the line number in bash script
# 1  
Old 02-04-2011
Printing the line number in bash script

Hi,
I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened.

Thanks,
# 2  
Old 02-04-2011
I guess the main answer to this would be:
since you wrote the script and your script controls the error checking and validations. I would say you would need to output the line number in your error statements. Even if the script would give a line number of a error if it is in a loop or a function call it may not be correct. So as in may other program languages you would need to put the reference to the error as needed. Now if using vi to edit the script to get a reference point in the vi session would be a
Code:
 <esc> :set nu

But if the script changes so do the line numbers. So I think I would lean to a number system that is not reliant on the line number. Then you could search the code for the error number and find it where you expected the problem.

Hope this helps.
# 3  
Old 02-04-2011
I'm not sure whether I conveyed what I wanted. For example in C programming, you can always print the line # using the macro __LINE__. Is there anything similar in bash that I could use of?

Thanks,
# 4  
Old 02-04-2011
In bash:
Code:
#!/bin/bash

echo "Welcome to my script."
echo $LINENO
echo "The above variable should be: 4"

# 5  
Old 02-04-2011
ok in a ksh shell the built in var is LINENO
so
Code:
 if [ chk -ne 0 ]; then #we are in error
     echo "It didn't make it at line: $LINENO"
fi

or something like that...
# 6  
Old 02-04-2011
The $LINENO variable is the current line being executed so:

Code:
#!/bin/bash

err_out()
{
     echo "fatal error on line $1 status= $2 "
     exit 1
}
# example call of err_out
./mycommand
STATUS=$?
if [ $STATUS -ne 0 ] ; then
   l=$(( $LINENO - 3 ))
   err_out $l $STATUS
fi

# 7  
Old 02-04-2011
Thanks a lot guys!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash detecting number of digits in line

Hi I have a problem, I am attempting to write a bash script that goes through a file and can determine how many characters are at a set point in a line starting with QTY+113:100:PCE, If it detects 3 digits (number in bold) then pad it out with 12 zero's If there are only two digits then pad it... (8 Replies)
Discussion started by: firefox2k2
8 Replies

2. Homework & Coursework Questions

Bash script for printing folder names and their sizes

1. The problem statement, all variables and given/known data: The task is to create a script that would reproduce the output of 'du' command, but in a different way: what 'du' does is: <size> <folder name>and what is needed is <folder name> <size>We need to show only 10 folders which are the... (3 Replies)
Discussion started by: UncleIS
3 Replies

3. Shell Programming and Scripting

Bash script for printing folder names and their sizes

Good day, everyone! I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of <size> <folder name>and what we need is <folder name> <size>As for... (1 Reply)
Discussion started by: UncleIS
1 Replies

4. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

5. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable.Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is not working(wrog... (5 Replies)
Discussion started by: learninguser235
5 Replies

6. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable(getlinenumber).Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is... (2 Replies)
Discussion started by: learninguser235
2 Replies

7. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

8. Shell Programming and Scripting

regarding about printing line number

Hello, I am testing some data to get line number at cursor position 9 and found some problem, the code is below.Assume we got 3 attribute. At second attribute, there are some data(eg.A41/A6) missing like at the fourth and six line 11006 A41 1888 11006 ... (7 Replies)
Discussion started by: davidkhan
7 Replies

9. Shell Programming and Scripting

printing line number

hi, i have a file, i need to search for a string , if the line contains i need to print that line number and line , please help thanks in advance Satya (5 Replies)
Discussion started by: Satyak
5 Replies

10. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question