Linux bash line number

How to check which line of a bash script is being executed

Is there a way to check which line number of a bash script is being executed «right now»? Using bash -x script.sh looks promising; however, I need to get the current line number.

5 Answers 5

Combine xtrace with PS4 inside the script:

$ cat test.sh #!/usr/bin/env bash set -x PS4='+$: ' sleep 1m sleep 1d $ timeout 5 ./test.sh +3: PS4='+$: ' +5: sleep 1m 
$ cat test.sh sleep 1m sleep 1d $ export PS4='+$: ' $ timeout 5 bash -x ./test.sh +1: sleep 1m 

Yes, there is a way.
There is an array of line numbers where a function has been called.

And call f at any line that you want the line number, for example:

#!/bin/bash f()< echo "$"; > f echo next1 f echo next2 f echo next 3 f 
6 next 1 9 next 2 12 next 3 15 

It could be expanded to show the trail of functions called:

#!/bin/bash f()< for ((i=$-1;i>=0;i--)); do printf ' ' "$" "$"; done echo "$LINENO" > SomeOtherFunction() < echo -n "test the line numbering: "; f; >f echo next 1 echo -n " This line numbering: "; f SomeOtherFunction echo next 2 echo -n " This line numbering: "; f SomeOtherFunction echo next 3 echo -n " This line numbering: "; f 
$ ./script  7 next 1 This line numbering:  7 test the line numbering:   7 next 2 This line numbering:  7 test the line numbering:   7 next 3 This line numbering:  7 

Note that above the echo «$LINENO» output is always the same (7 in this case).

Here’s a solution that borrows parts of l0b0’s and DopeGhoti’s answers (and, to a lesser extent, sorontar’s). Like those answers, mine uses $LINENO to discover the line number; unlike them, I use trap to trigger the reporting. bash’s trap command is described in bash(1):

    The command arg is to be read and executed when the shell receives signal(s) sigspec. … ⁠ ︙
    … If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function …
$ cat -n myscript 1 #!/bin/bash 2 trap 'printf "%3d: " "$LINENO"' DEBUG 3 date 4 sleep 30 5 date 6 sleep \ 7 11 8 date 9 10 ls -l 11 for f in * 12 do 13 echo "$f" && 14 ls -ld "$f" 15 done 16 17 for ((i=0; i 

runs the printf "%3d: " "$LINENO" command before every command in the script, and produces this output:

$ ./myscript 3: Wed, Apr 05, 2017 10:16:17 AM 4: 5: Wed, Apr 05, 2017 10:16:47 AM 7: 8: Wed, Apr 05, 2017 10:16:58 AM 10: total 4 -rwxr-xr-x 1 myusername mygroup 221 Apr 5 10:01 myscript -rwxr-xr-x 1 myusername mygroup 252 Apr 5 10:01 myscript2 -rw-r--r-- 1 myusername mygroup 132 Apr 5 09:59 myscript2.log -rw-r--r-- 1 myusername mygroup 45 Apr 5 08:34 other_file 11: 13: myscript 14: -rwxr-xr-x 1 myusername mygroup 221 Apr 5 10:01 myscript 11: 13: myscript2 14: -rwxr-xr-x 1 myusername mygroup 252 Apr 5 10:01 myscript2 11: 13: myscript2.log 14: -rw-r--r-- 1 myusername mygroup 132 Apr 5 09:59 myscript2.log 11: 13: other_file 14: -rw-r--r-- 1 myusername mygroup 45 Apr 5 08:34 other_file 17: 17: 19: i = 0 19: Wed, Apr 05, 2017 10:16:59 AM 17: 17: 19: i = 1 19: Wed, Apr 05, 2017 10:16:59 AM 17: 17: 19: i = 2 19: Wed, Apr 05, 2017 10:16:59 AM 17: 17: 22: 42 $
  • Like l0b0’s answer, this is minimally invasive — just add line 2.
  • Unlike l0b0’s answer, this doesn’t display the commands themselves — but you didn’t ask for it to do that.
  • The second sleep , which spans script lines 6 and 7, is reported as line 7.
  • Line 11 ( for f in * ) is reported once before each iteration of that for loop.
  • echo "$f" and ls -ld "$f" are correctly reported on their respective lines (13 and 14).
  • Line 17 ( for ((i=0; itwice before each iteration of that for loop, and twice more after the last iteration.
  • Unlike set -x , LINENO and PS4 (which are specified by the POSIX standard), the DEBUG trap is a bash extension and will not work in all shells.
  • The DEBUG trap can run any command(s), and is not restricted to writing to the script’s standard output or standard error.

The question says, «check which line number of a bash script is being executed “right now”» without specifying a user interface. Another approach is to continually write the current line number to a log file:

$ diff myscript myscript2 2c2 < trap 'printf "%3d: " "$LINENO"' DEBUG --- >exec 6> myscript2.log && trap 'printf "%3d\n" "$LINENO" >&6' DEBUG $ ./myscript2 Wed, Apr 05, 2017 10:23:50 AM Wed, Apr 05, 2017 10:24:20 AM Wed, Apr 05, 2017 10:24:31 AM total 4 -rwxr-xr-x 1 myusername mygroup 221 Apr 5 10:01 myscript -rwxr-xr-x 1 myusername mygroup 252 Apr 5 10:01 myscript2 -rw-r--r-- 1 myusername mygroup 24 Apr 5 10:23 myscript2.log -rw-r--r-- 1 myusername mygroup 45 Apr 5 08:34 other_file myscript -rwxr-xr-x 1 myusername mygroup 221 Apr 5 10:01 myscript myscript2 -rwxr-xr-x 1 myusername mygroup 252 Apr 5 10:01 myscript2 myscript2.log -rw-r--r-- 1 myusername mygroup 60 Apr 5 10:23 myscript2.log other_file -rw-r--r-- 1 myusername mygroup 45 Apr 5 08:34 other_file i = 0 Wed, Apr 05, 2017 10:24:31 AM i = 1 Wed, Apr 05, 2017 10:24:31 AM i = 2 Wed, Apr 05, 2017 10:24:31 AM 42 $

We can monitor the execution of this script by monitoring the contents of the myscript2.log file from another terminal. For example, during the second sleep ,

$ tail myscript2.log 3 4 5 7 

Источник

Grep with the Line Number in Output

Global regular expression print is a versatile utility that searches plain text in the system with different regular expressions. We can perform many operations with the help of Grep; we can explore in files, display line number as output, and how to ignore blank spaces, and use Grep recursively. Grep with the line number displays the line number of relevant text present in the file. This function is accomplished with the help of –n. From the page of Grep, we can easily describe different commands.

Prerequisite

To achieve this current goal of obtaining a specific line number of the text, we must have a system to run commands on it which is the Linux operating system. Linux is installed and configured on the virtual machine. After providing a username and password, you will be able to access the applications.

The Line Number for Matching a Word

Generically when we use the Grep command, after the Grep keyword, the word that has to be explored is written and followed by the filename. But, by getting the line number, we will add -n in our command.

Here “is” is the word that is to be explored. The starting line number shows that the related file contains the word in different lines; each line has a highlighted word that shows the matching line to the relevant search.

The Line Number of the Whole Text in the File

The line number of every line in the file has shown by using a particular command. It not only shows the text but also covers the blank spaces and mentions their line numbers too. The numbers are shown on the left side of the output.

Fileb.txt is a filename. Whereas n is for the line numbers, and l shows the filename only. In case we have searched a specific word in any file, it will only show the filenames.

Concurrent to the previous example, here are (except for free space), which are special characters that are mentioned. They are also shown and read by the command to display the line number. Unlike the first example of the article, this simple command shows the line’s number exactly how it is present in the file. As there is no limitation of search declares in command.

Show Only Line Number

To get only the line numbers of data in the respective file, we can easily follow the below command.

The first half command before the operator is understandable because we have discussed earlier in this article. Cut –d is used to cut the command, which means to suppress the display of text in the files.

Provide Output in a Single Line

Following the command above, the output is display on a single line. It removes the extra space between the two lines and only shows the line number mentioned in the previous commands.

The right portion of the command shows that how the output is displayed. The cut is used to cut the command. Whereas second “|” is applied for bringing to the same line.

Show Line Number of the String within the Subdirectory

In order to demonstrate the example on subdirectories, this command is used. It will search for the word “1000” present in files in this given directory. The file number is shown at the starting of the line on the left side of the output, showing the occurrence of 1000 in the prcd folder at 370 ties and in Webmin is 393 times.

This example is good in finding an error occurring chances in your system by checking and sorting particular words from the directory or subdirectory. The /etc/ describes the path of the directory having a folder of services.

Show according to a word in the file

As already described in the examples above, the word helps search the text inside the files or folder. Searched words will be written in inverted commas. On the very left side of the output, a line number is mentioned, showing the occurrence of the name on which line in a file. “6” shows that the word Aqsa is present on line 6 after line 3. Highlighting the specific word makes it easier for the user to understand this concept.

The output shows the whole string in the file, not only the single word present in the string, and it only highlights the given word.

Bashrc

This is a useful example of getting the line number in the output. This will search in all directories, and we don’t have to provide the directory path. By default, it is implemented on all directories. It shows all the output data on the files present in subdirectories, as we don’t have to mention a specific word to be searched through the command.

It is an extension of all folders that are present. By specifying the extension name, we can show the relevant data, i.e., login detailed files.

Search in all Files

This command is used in searching the file in all files having that data. File* shows that it will search from all files. The filename is displayed with the line number after the name at starting of the line. The relevant word is highlighted to show the existence of the word in the text in the file.

Search in Files Extensions

In this example, the word is searched in all files of a specific extension, that is.txt. The Directory that is given in the command is the path of all files provided. The output also shows the way according to the extension. The line number is given after the filenames.

Conclusion

In this article, we have learned how to obtain the line number in the output by applying different commands. We hope this effort will help in gaining enough information regarding the relevant topic.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

Читайте также:  Grub rescue при загрузке linux
Оцените статью
Adblock
detector