Sponsored Content
Top Forums Shell Programming and Scripting Printing the line number in bash script Post 302494048 by aix-guy on Friday 4th of February 2011 03:54:48 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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(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

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.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

7. 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

8. 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

9. 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

10. 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
Test::Refcount(3pm)					User Contributed Perl Documentation				       Test::Refcount(3pm)

NAME
"Test::Refcount" - assert reference counts on objects SYNOPSIS
use Test::More tests => 2; use Test::Refcount; use Some::Class; my $object = Some::Class->new(); is_oneref( $object, '$object has a refcount of 1' ); my $otherref = $object; is_refcount( $object, 2, '$object now has 2 references' ); DESCRIPTION
The Perl garbage collector uses simple reference counting during the normal execution of a program. This means that cycles or unweakened references in other parts of code can keep an object around for longer than intended. To help avoid this problem, the reference count of a new object from its class constructor ought to be 1. This way, the caller can know the object will be properly DESTROYed when it drops all of its references to it. This module provides two test functions to help ensure this property holds for an object class, so as to be polite to its callers. If the assertion fails; that is, if the actual reference count is different to what was expected, a trace of references to the object can be printed, if Marc Lehmann's Devel::FindRef module is installed. This may assist the developer in finding where the references are. See the examples below for more information. FUNCTIONS
is_refcount( $object, $count, $name ) Test that $object has $count references to it. is_oneref( $object, $name ) Assert that the $object has only 1 reference to it. EXAMPLE
Suppose, having written a new class "MyBall", you now want to check that its constructor and methods are well-behaved, and don't leak references. Consider the following test script: use Test::More tests => 2; use Test::Refcount; use MyBall; my $ball = MyBall->new(); is_oneref( $ball, 'One reference after construct' ); $ball->bounce; # Any other code here that might be part of the test script is_oneref( $ball, 'One reference just before EOF' ); The first assertion is just after the constructor, to check that the reference returned by it is the only reference to that object. This fact is important if we ever want "DESTROY" to behave properly. The second call is right at the end of the file, just before the main scope closes. At this stage we expect the reference count also to be one, so that the object is properly cleaned up. Suppose, when run, this produces the following output (presuming "Devel::FindRef" is available): 1..2 ok 1 - One reference after construct not ok 2 - One reference just before EOF # Failed test 'One reference just before EOF' # at demo.pl line 16. # expected 1 references, found 2 # MyBall=ARRAY(0x817f880) is # +- referenced by REF(0x82c1fd8), which is # | in the member 'self' of HASH(0x82c1f68), which is # | referenced by REF(0x81989d0), which is # | in the member 'cycle' of HASH(0x82c1f68), which was seen before. # +- referenced by REF(0x82811d0), which is # in the lexical '$ball' in CODE(0x817fa00), which is # the main body of the program. # Looks like you failed 1 test of 2. From this output, we can see that the constructor was well-behaved, but that a reference was leaked by the end of the script - the reference count was 2, when we expected just 1. Reading the trace output, we can see that there were 2 references that "Devel::FindRef" could find - one stored in the $ball lexical in the main program, and one stored in a HASH. Since we expected to find the $ball lexical variable, we know we are now looking for a leak in a hash somewhere in the code. From reading the test script, we can guess this leak is likely to be in the bounce() method. Furthermore, we know that the reference to the object will be stored in a HASH in a member called "self". By reading the code which implements the bounce() method, we can see this is indeed the case: sub bounce { my $self = shift; my $cycle = { self => $self }; $cycle->{cycle} = $cycle; } From reading the "Devel::FindRef" output, we find that the HASH this object is referenced in also contains a reference to itself, in a member called "cycle". This comes from the last line in this function, a line that purposely created a cycle, to demonstrate the point. While a real program probably wouldn't do anything quite this obvious, the trace would still be useful in finding the likely cause of the leak. If "Devel::FindRef" is unavailable, then these detailed traces will not be produced. The basic reference count testing will still take place, but a smaller message will be produced: 1..2 ok 1 - One reference after construct not ok 2 - One reference just before EOF # Failed test 'One reference just before EOF' # at demo.pl line 16. # expected 1 references, found 2 # Looks like you failed 1 test of 2. BUGS
o Temporaries created on the stack Code which creates temporaries on the stack, to be released again when the called function returns does not work correctly on perl 5.8 (and probably before). Examples such as is_oneref( [] ); may fail and claim a reference count of 2 instead. Passing a variable such as my $array = []; is_oneref( $array ); works fine. Because of the intention of this test module; that is, to assert reference counts on some object stored in a variable during the lifetime of the test script, this is unlikely to cause any problems. ACKNOWLEDGEMENTS
Peter Rabbitson <ribasushi@cpan.org> - for suggesting using core's "B" instead of "Devel::Refcount" to obtain refcounts AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.10.1 2011-04-25 Test::Refcount(3pm)
All times are GMT -4. The time now is 04:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy