2

I am looking for a way to print the current line number of the console output

I have tried doing some research on tput but I haven't find something that was suitable

What I am looking for is something where will show something like this:

Hello world
This is a fantastic world - 2
Another random string
A different random string
This is the last string and this is the line 5 of this output.

The important thing is that if in the console, before my command, I have ran some other string the line number should include those. Let's say that before that command I ran a different command that has included 10 lines, the number 2 should be 12 and the line 5 should be 15 and so on. I just need a way to find out in what line the output has been printed.

Any help will be much appreciated

Image

UPDATE: I have been forwarded here How to get the cursor position in bash?

However I tried every method listed there and unfortunately while the code works fine I am not able to adapt it to my use. I am running different commands at the same time and the script won't wait for them to finish before moving to the next one (using & at the end of the command). Each of these commands should be able to save the current cursor position in order to print their results in the shell output. Unfortunately with all the code suggested in the other thread despite the row selected is different the output keeps appearing on the same line. Is there a way to save the output line number in a variable and then print something on the same line afterwards?

This was my original thread: Bash script, update output once command is ended

Porcac1x
  • 105
  • 9
  • 1
    @aaron, ... However, that doesn't help if they want the offset adjusted for things that ran before their program started, as is explicitly the case here. (Also only helps if all writes go through that shim). – Charles Duffy Nov 10 '19 at 13:07
  • @Porcac1x, in general, it's not always possible to know how much content has previously been written to a given file descriptor. A TTY (and stdout is not always a TTY at all!) may (or may not) be able to tell you which line of the screen the cursor is on, but it won't tell you how many lines of scrollback preceded that. If your stdout is going to a file you can try to measure that file, but that doesn't work if it's a FIFO or pipeline, or otherwise non-seekable. – Charles Duffy Nov 10 '19 at 13:10
  • 1
    ...so, first step is to clarify the question: do you want a line number relative to the top of the screen, or to the start of the session? (Would you expect clearing the screen to reset the generated value to 0, to 1, or to not change it at all?) – Charles Duffy Nov 10 '19 at 13:18
  • And not clear when the counting start/reset, missing any visible attempt to solve quesiton. – Dudi Boy Nov 10 '19 at 14:07
  • Hi all and thank you for your messages. No, the output will not go on a file (good question, but unfortunately that option is excluded). @CharlesDuffy I need to be able to get the line number of any string if needed, clearing the screen is an option but just when I run the output, not in the middle of the program nor the end – Porcac1x Nov 10 '19 at 15:56
  • I have uploaded an image that should clarify a little what I am trying to achieve – Porcac1x Nov 10 '19 at 16:02
  • Okay, that looks like a job that `tput` is actually perfectly well suited to, at least when your `TERM` is one with the relevant support. – Charles Duffy Nov 10 '19 at 16:20
  • hey @CharlesDuffy but what is the command fort tput? I read a bit about it but I couldn't find a specific command for that – Porcac1x Nov 10 '19 at 21:11
  • @CharlesDuffy can you please remove the duplicate tag?I am still looking for help with this – Porcac1x Nov 18 '19 at 22:36
  • Please [edit] this such that the question lets someone else reproduce the specific circumstances that make the other answers unusable. (Showing a [mre] for what you're trying to do wrt. combining multiple background tasks is probably appropriate). – Charles Duffy Nov 20 '19 at 01:15
  • ...that said, since background tasks by nature run asynchronously, there's no way to have it provably true that the result of one `tput` command is still accurate as of a subsequent `echo`; unless you actually implement a mutex that all your programs voluntarily use to gate writes to the console, race conditions are an innate problem. – Charles Duffy Nov 20 '19 at 01:16

0 Answers0