This question already has an answer here:
Save and restore terminal content
(1 answer)
Closed 4 years ago.
A number of commands (eg watch, less) are able to temporarily clear the tty to display a full screen of information, then when the command exits restore the original tty content.
Is there a way to achieve this in a bash script?
Use tput. Here's a minimal example:
#!/bin/bash
tput smcup # save the screen
clear # clear the screen
echo this is some text on a blank screen
echo press any button to exit..
read -n1
tput rmcup # reset the screen
Related
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
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
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
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to execute a Linux command in Bash with the input coming from a file or echo, and then switch back to standard input as if the command's input was not redirected.
So basically, I want to feed the first part from an interactive command with a predefined text, and as soon as that text is "consumed" I want to continue using the keyboard (stdin).
Some examples:
Prefill editor and then continue typing manually
(echo blablabla; cat) | nano
Auto remove first file, then manually confirm removing second file
touch dummyfile1.txt; touch dummyfile2.txt
(echo y; cat) | rm -i dummyfile*.txt
Fill in password for dummyuser, and than let the user fill in the password for the zip file
(echo dummypassword; cat) | su dummyuser -c "unzip pwdprotectedfile.zip"
Here, the echo fills in the first part of the command, and then cat takes over to copy stdin to stdout to manually fill in the remaining part of whatever the command needs.
The (echo ; cat) method is the closest thing that (almost) works. But the problem here is that at the end an extra enter key press is needed to return to the command prompt.
How to do this properly without the extra key press needed?
The real situation I need this for is to run su -c somecommand, fill in the password automatically (from a secure source) and then let the user answers the questions asked by somecommand.
If your intent is to programmatically generate a content for the nano editor, it is as simple as telling nano to edit the standard input by specifying - as file name.
echo "blablabla" | nano -
Now consider that echo behaviour is not portable across shell versions, so prefer it printf '%s\n' "blablabla" for a single line of text.
To be courteous with the user, you can invoke his preferred editor as set in the EDITOR environment variable, with fall-back to vi if the EDITOR environment variable is not set.
printf '%s\n' "blablabla" | "${EDITOR:-vi}"
If you are using bash, you can replace piping printf with an here-string instead:
#!/usr/bin/env bash
"${EDITOR:-vi}" - <<<"blablabla"
This question already has answers here:
How to store the output of a command in a variable at the same time as printing the output?
(4 answers)
Closed 3 years ago.
Then I call command in my bash script like command it's outputs to my terminal but isn't saving anywhere. If I want to save output I use output=$(command) but now command output isn't showing to my screen unless command would be completed so I can call echo "${output}". But the problem is: I don't want to wait until command will be completed. So, is there any way to call command, show output while it's running and later save everything to a variable?
It could work, if I call it twice:
command
output=$(command)
but it's not that good I think.
sry for my engrish.
I think you want a tool called tee:
The tee utility copies standard input to standard output, making a copy in zero
or more files. The output is unbuffered.
It will output to the screen and also to a file. you use it like this:
cat file1.txt | tee -a file2.txt
Use of cat is just an example. Any command on the left side of the pipe should work.
This question already has answers here:
Using the "alternate screen" in a bash script
(3 answers)
Closed 6 years ago.
I'm building a bash script, with an interactive ASCII-menu, and want it to restore the terminal as it was before, like "vim" does, or "less".
I guess, i have to redirect the output to an other shell or something like that. But all I have found, was for redirecting files, or opening new terminal windows.
You're looking for the ti and te terminal capabilities, which can be most easily accessed in bash (or on the command line) with the commands tput smcup to switch to the alternate screen and tput rmcup to restore the original screen.
This question already has answers here:
How do I put an already-running process under nohup?
(10 answers)
Closed 9 years ago.
I am running a process in remote server(redhat linux) which takes very long time.
And I need be away for sometime, I want it to be running in background and don't stop when log out.
Just like
nohup command &
Is there any method can do this?
You can use GNU Screen or tmux
those are terminal multiplexer which allow you to start a process and detach it from the console. You can then logout and re-attach later.
eg. with screen
screen -S title
# start your process inside screen
Ctrl A D # to detach
logout
... later ...
login
screen -r title # re attach