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.
Related
This question already has answers here:
How to open a new tab in GNOME Terminal from command line? [closed]
(10 answers)
Closed 3 years ago.
I actually want to run some commands of the same script on different tabs or terminal because these commands are activating servers and listening to different ports. So they have to be always active. As well, I want to have a reference to the tab or the terminal so that I can later shut them down at the end of the script. Help please.
I tried a simple script test to see if I could find a way for opening other tabs :
tab=" --tab"
options=()
cmds[1]="echo Banana"
cmds[2]="echo Cat"
for i in 1 2; do
options+=($tab -e "bash -c \"${cmds[i]} ; bash\"" )
done
gnome-terminal "${options[#]}"
exit 0
but I get this as a result :
./test.sh
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
Is there any way to open tabs ? and get a reference to each one so that I can shut down commands related to servers once the treatment is done ?
You could use a programm called tmux. It's a terminal-multiplexer, like screen. With that you can open different sessions and windows (these are like tabs) in the same terminal window. They can be referenced by name or id via script.
It's very probable, that the package manager of your Linux distribution has tmux.
P.S: I have to post an answer instead of a comment as I don't have enough reputation for commenting.
This is what I found for man gnome-terminal ; I think that could help , thanks
gnome-terminal(1) General Commands Manual gnome-terminal(1)
NAME
gnome-terminal — is a terminal emulation application.
SYNOPSIS
gnome-terminal [-e, --command=STRING] [-x, --execute ] [--window-
with-profile=PROFILENAME] [--tab-with-profile=PROFILENAME] [--window-
with-profile-internal-id=PROFILEID] [--tab-with-profile-internal-
id=PROFILEID] [--role=ROLE] [--show-menubar] [--hide-menubar]
[--geometry=GEOMETRY] [--working-directory=DIRNAME] [-?, --help]
DESCRIPTION
GNOME Terminal is a terminal emulation application that you can use to
perform the following actions:
Access a UNIX shell in the GNOME environment.
A shell is a program that interprets and executes the commands that you
type at a command line prompt. When you start GNOME Terminal, the
application starts the default shell that is specified in your system
account. You can switch to a different shell at any time.
OPTIONS
-e, --command=STRING
Execute the argument to this option inside the terminal.
-x, --execute
Execute the remainder of the command line inside the termi‐
nal.
--window-with-profile=PROFILENAME
Open a new window containing a tab with the given profile.
More than one of these options can be provided.
--tab-with-profile=PROFILENAME
Open a tab in the window with the given profile. More than
one of these options can be provided, to open several tabs .
--window-with-profile-internal-id=PROFILEID
Open a new window containing a tab with the given profile ID.
Used internally to save sessions.
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
This question already has answers here:
ANSI Color Codes in VIM
(4 answers)
Closed 5 years ago.
I am sshing from iTerm2 to an Ubuntu machine.
I am using ns3 through waf and redirecting my output to a log file using the below command:
mpirun -n 16 ./waf --run test > log.out 2>&1
The log file that gets generated has output like below:
^[[32mWaf: Entering directory `/home/workspace/ns-allinone-3.25/ns-3.25/build/optimized'^[[0m
^[[32mWaf: Entering directory `/home/workspace/ns-allinone-3.25/ns-3.25/build/optimized'^[[0m
I am unable to understand why the extra characters are being added in the file.
When I use less to view the contents, I see the output perfectly fine.
What should I do to not see the extra in vim.
If your vim echo has('conceal') return 1, try this simple solution to hide ^[[32m
syntax match Special /^[\[32m/ conceal
Note: you'd better copy ^[ from the log file for it's actually a Ctrl-[
This question already has answers here:
how to open a gnome terminal to execute a command with gnome-terminal, constantly?
(2 answers)
Closed 5 years ago.
I need to write a scirpt in python (or bash) which:
open a new linux terminal with a few tabs
do a 'source myfile.csh' command on each of the tab
I've tried with something like this:
os.system("gnome-terminal --tab --working-directory=/repo/ -e 'source myfile.csh' ")
but it didn't work (for example file was not found even though I was in a correct directory or can't open a .csh file blah blah blah)
I've also tried a few other options, but I didn't find a proper soultion for my problem.
I don't know why I can't do a simple "open a new terminal, write a command and execute it"
Maybe is there any simple solution WITHOUT installing any new software (I don't have a root)?
-e will execute a process. However source is an instruction of your shell. What you need to do is to call your shell with -e and pass arguments to the shell in order to execute the source instruction. Should be something like
os.system("gnome-terminal --tab --working-directory=/repo/ -e 'tcsh -c \"source myfile.csh\"'")
This question already has answers here:
How to make a program continue to run after log out from ssh? [duplicate]
(6 answers)
Closed 6 years ago.
I have a script with one of its command let say "cmd" this cmd will move the user from the shell to some other server/shell , now when I run this script after executing "cmd" the shell gets changed (normal behaviour) but the commands following the "cmd" will not run and when I logout from the new terminal to come back to original one then only that commands execute on their own .
Is there any way so that the remaining will execute on the new terminal ?
I'm assuming your "cmd" is ssh opening a session on another server. If thats the case you can instruct ssh to run a command once the session is open: ssh user#server.com your_command_here