Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    23

    Bash: 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. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.
    Code:
           LINENO Each time this parameter is referenced, the shell substitutes a
                  decimal number representing the current sequential line number
                  (starting with 1) within a script or function.  When not in a
                  script or function, the value substituted is not guaranteed to
                  be meaningful.  If LINENO is unset, it loses its special
                  properties, even if it is subsequently reset.
    -- excerpt from man bash, q.v.
    For shells different from bash, see the appropriate man page. Here is an example in bash:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate special bash parameter LINENO.
    
    # Section 1, setup, pre-solution.
    # Infrastructure details, environment, commands for forum posts. 
    # Uncomment export command to test script as external user.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    set +o nounset
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    C=$HOME/bin/context && [ -f $C ] && . $C
    set -o nounset
    
    pl " Results:"
    pe " This is line $LINENO"
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
    GNU bash 3.2.39
    
    -----
     Results:
     This is line 16
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    453
    $LINENO gives the line number. Where do you want to print it? Printng to stdout is done with pe, as in the example.

    If you want to print the entire script with line numbers for each, for reference, I'm not sure how to do that. Gedit will show line numbers, but it doesn't distinguish between actual script lines and comments, so the line number won't be the same if you have comment lines in the script.

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.
    Quote Originally Posted by sgosnell View Post
    ... If you want to print the entire script with line numbers for each, for reference, I'm not sure how to do that. ...
    Use a command like nl, as in:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s2	Demonstrate printing file with line numbers, "nl".
    
    # Section 1, setup, pre-solution.
    # Infrastructure details, environment, commands for forum posts. 
    # Uncomment export command to test script as external user.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    set +o nounset
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    C=$HOME/bin/context && [ -f $C ] && . $C specimen
    set -o nounset
    pe
    
    FILE=${1-s1}
    
    # Section 2, display input file.
    # Display data file.
    pe " || start [ first:middle:last ]"
    cat $FILE
    pe " || end"
    
    # Section 3, solution.
    pl " Results:"
    nl $FILE
    
    exit 0
    producing:
    Code:
    % ./s2
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
    GNU bash 3.2.39
    specimen (local) 1.17
    
     || start [ first:middle:last ]
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate special bash parameter LINENO.
    
    # Section 1, setup, pre-solution.
    # Infrastructure details, environment, commands for forum posts. 
    # Uncomment export command to test script as external user.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    set +o nounset
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    C=$HOME/bin/context && [ -f $C ] && . $C 
    set -o nounset
    
    pl " Results:"
    pe " This is line $LINENO"
    
    exit 0
     || end
    
    -----
     Results:
         1	#!/usr/bin/env bash
           
         2	# @(#) s1	Demonstrate special bash parameter LINENO.
           
         3	# Section 1, setup, pre-solution.
         4	# Infrastructure details, environment, commands for forum posts. 
         5	# Uncomment export command to test script as external user.
         6	# export PATH="/usr/local/bin:/usr/bin:/bin"
         7	set +o nounset
         8	pe() { for i;do printf "%s" "$i";done; printf "\n"; }
         9	pl() { pe;pe "-----" ;pe "$*"; }
        10	C=$HOME/bin/context && [ -f $C ] && . $C 
        11	set -o nounset
           
        12	pl " Results:"
        13	pe " This is line $LINENO"
           
        14	exit 0
    See man nl for details ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •