0

example.sh:


x=0
while true
do
  x=$(expr $x + 1)
   clear    #to print no. @ same location
  printf "\n\n     $x"|figlet -f block
  sleep 1;

done

Like, htop/cmatrix/vim/nano/bashtop,etc...
After running it i have to get back to the last prompt,
not like, cat/find/,etc...



closest solution had come up with is to, run script inside a tmux session



what i mant was, i dont want to lose my command outputs i ran before,

like nano/vim/cmatrix it clears the screen then run it, then when we exit out of it like, ^c/q , we are back where we left the prompt, with the history[last ran commands and outputs]

is there a command which does this?




┌──(kali㉿kali)-[~]
└─$ vi

┌──(kali㉿kali)-[~]
└─$ nano

┌──(kali㉿kali)-[~]
└─$ cat copy
file contents

┌──(kali㉿kali)-[~]
└─$

Here, i opened nano, i opened vim, but u cant see my inside vim or nano , but thats not the case of cat, it just print it on the same session , with vim/nano/htop terminal is clean, thats not the case in sed/cat/ps
I wanted to create a script like that,[which will not effect the terminal session(like which run in another dimention)],

I tried reading the [bashtop][1] source code, which also have same behavior, but i couldnt find the code/command which does it
ANDuser
  • 71
  • 8
  • [`watch`](https://www.mankier.com/1/watch)? – Biffen Mar 09 '22 at 12:26
  • Do you mean something like [screen](https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/)? – user1934428 Mar 09 '22 at 12:26
  • @Biffen: The description provided by the OP is a bit confusing, but I think what he means is: When you start (for instance) `nano`, the editor takes over the terminal area, and when you exit the editor, you can see the content of your terminal as it was before running the editor. The OP wants to implement a similar functionality for his own programs. – user1934428 Mar 09 '22 at 12:28
  • From my understanding, `nano` uses the ncurses library, which ofters (for instance) functions for [saving and restoring](https://stackoverflow.com/questions/44930979/how-to-save-a-ncurses-window-in-a-structure#44931961) the screen. Just an idea: You could, using this method, write a small C program which saves the screen content to a file, and another one which reloads the screen from this file. I never tried this, so I can't say how well it works. – user1934428 Mar 09 '22 at 12:36
  • Another possibility would be to catch the stdout of your script into a file, and then use something like [`dialog`](https://linuxconfig.org/how-to-use-ncurses-widgets-in-shell-scripts-on-linux) to display this content in an overlay window. – user1934428 Mar 09 '22 at 12:39
  • Do you **have** to use bash? If you would go for `zsh`, there seems to be a module available providing a ncurses-binding; it's called [zcurses](https://www.zsh.org/mla/users/2019/msg00317.html). – user1934428 Mar 09 '22 at 12:43
  • dialog can do it / dialog is piped to any other program, sometimes when i 'apt upgrade' dialog just popup asking like 'there is a config diference in ssh conf file'... after i chose the option dialg will be gone , but it exit dialog box in such a way that last command output doesnt contain dialog box underscores or colors etc, if you know how to do that pease comment full command like i tried ```dialog --editbox lol.txt 30 60``` it just cleared my screen [clear/tput] – ANDuser Mar 09 '22 at 12:57
  • If you just want to erase the content produce by your script, you have 2 solutions: Adding a clear commend before quit the script or adding systematically >/dev/null 2>/dev/null to command you execute in your script – Alaindeseine Mar 09 '22 at 13:48
  • https://stackoverflow.com/questions/11023929/using-the-alternate-screen-in-a-bash-script – William Pursell Mar 09 '22 at 16:23

2 Answers2

2

vim and less and many other tools access the terminal's alternate screen and then restore the original screen when they are done. You can use tput to access the alternate screen from your shell script:

#!/bin/sh

tput smcup  # begin using the alternate screen

# ...

tput rmcup  # stop using alternate screen (restore terminal to original state)

Note that you don't want to remain in the alternate screen when the script ends, so you may prefer to do:

#!/bin/sh

trap 'tput rmcup' 0
tput smcup
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

In bashtop, they had used tput on previous updates, then they changed it to,

#!/bin/sh

echo -en '\033[?1049h'  #* Switch to alternate screen

# codes to run inside

echo -en '\033[?1049l'  #* Switch to normal screen
ANDuser
  • 71
  • 8
  • You get a nod for sticking to it and actually digging deep enough to find the cause. The `'\033[?1049h'` is an ASNI escape sequence (one I'm not familiar with -- and one I suspect not too many terminals implement) Good job digging for the answer. – David C. Rankin Mar 10 '22 at 06:40