2

I have one simple problem, with my script, I need row script in same screen and does not display last information above, see my demo code:

#!/bin/bash

if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
#result=`sensors -A`
count=0
keypress=''
echo -ne "linha 1\nLinha 2\n " > /tmp/temp_script.log
while [ "x$keypress" = "x" ]; do    
  clear  
  let "count++" 
  output="$(cat /tmp/temp_script.log)"
  printf  "$output"\\b
  echo -en $count\\n
  sleep 1 
keypress="`cat -v`"
done
if [ -t 0 ]; then stty sane; fi
exit 0

The result code:

current code

I need to display this information in a manner that does not generate these multiple lines of the first example, and display only the looping of the information in question, as in the second example:

How can I do this:

my objective

And so on, so there will be previous not impressions of the loop at the terminal only what is printing now.

6
  • I'm not sure I understand your question. What do you mean by I need to print this information without multiple lines on terminal row?
    – I0_ol
    Nov 1, 2016 at 4:57
  • I need to print the lines so that it is not leaving this trail back, for example instead of getting the previous lines of information want to return to the line 1 and retype.
    – Bulfaitelo
    Nov 2, 2016 at 9:49
  • Can you post your desired output in the question? This will help make things more understandable.
    – I0_ol
    Nov 2, 2016 at 12:20
  • @I0_ol I edit post do better explain, see if I explained better?, and thank you for the help you are giving me ^^
    – Bulfaitelo
    Nov 2, 2016 at 13:05
  • I don't know the answer to this question. And since it's been 4 days now without an answer, you might want to ask a moderator to move the question to stackoverflow.
    – I0_ol
    Nov 5, 2016 at 5:08

1 Answer 1

1

Assuming that the clear really clears the screen, then the script shown, by itself, will not produce the first screenshot. If you had an additional process which is appending to /tmp/temp_script.log, then that could do something...

On the other hand, if your terminal is not clearing the screen, that's part of the problem. Perhaps that colored line is the prompt from which you ran the script, and you could like to keep that at the top of the screen:

  • normally you would set TERM to a suitable value which corresponds to a terminal description.
  • a "suitable" terminal description tells how to clear the screen.
  • as a side effect, clearing the screen (by convention) also moves the cursor to the upper-left corner of the screen, but
  • clearing the screen from the upper-left corner of the screen would also clear the colored line (which you might want to keep)
  • if you wanted to clear from the second line, you could (on most terminals) do
    printf '\033[2H\033[J'

or

    tput cup 1 0
    tput ed
  • but keep in mind that if your prompt was originally not at the top of the screen, this would just leave whatever was at the top of the screen untouched.

The other part of the problem is that the three lines are printed repeatedly, going down the screen. You could change that to move the cursor where you want to print the counter, e.g.,

    printf '\033[4H'

or

    tput cup 3 0

Putting those together, your script might look like this (and using printf, since something seems amiss with your TERM and/or terminal description):

    #!/bin/bash

    if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
    #result=`sensors -A`
    count=0
    printf '\033[2H\033[J'
    keypress=''
    echo "linha 1"
    echo "Linha 2
    while [ "x$keypress" = "x" ]; do    
      let "count++" 
      printf '\033[4H'
      echo $count
      sleep 1 
      keypress="`cat -v`"
    done
    if [ -t 0 ]; then stty sane; fi
    exit 0

Further reading:

1
  • thank you very much, besides me help you explained very well how to implement the new rules
    – Bulfaitelo
    Nov 6, 2016 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.