Customization multiple tabs in terminal - linux

Issue: Create a script that opens a terminal window with multiple tabs, each tab requires a different title to identify its purpose, in addition each tab should display a specific commmand (will not execute the command, the user will need to hit Enter to execute the command).
Example: The user needs to execute 3 commands: ifconfig, route -n and top, the user executes the script and it opens the terminal with 3 tabs, the first tab shows in the title Network and the prompt shows root$ ifconfig, the second tab shows in the title Routing and the prompt is like root$ route -n, the third tab shows the title Performance and the prompt shows root$ top. The commands are not running when the script is executed, the user needs to go into each tab and manually hit "Enter" to execute each command.
I am using the following script to open the terminal with multiple tabs, but am stuck trying to get the other features working, any assistance will be highly appreciated:
#bin/bash
tab="--tab"
cmd="bash -c 'pwd';bash"
foo=""
for i in 1 2 3; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[#]}"
exit 0

You can save the state of your terminal using:
gnome-terminal --save-config=FILE
And call the load with:
gnome-terminal --load-config=FILE
Where FILE is the filename you would like to save.
So you can open 3 tabs and give them names etc, then save the config and in your script you can load it.
On the saved file you can edit properties like:
WorkingDirectory=
Title=
As for displaying a command and not running it, I don't have a solution.
You can check this:
http://www.techrepublic.com/blog/linux-and-open-source/how-to-make-your-own-gnome-terminals/

Related

Check Previous Output in Google Cloud Console, is it possible?

I'm trying to view all commands I have entered and their outputs. I understand that I can use 'history' to view all of my recent commands. However, is there a way I can check for previous outputs of those commands? I have tried looking it up and there does not seem to be a way.
It only stores history of the commands you ran (which you can retrieve by typing history). Unless you already have set the scroll-back to a very high number, there is no way to see the outputs that are older than the set value of scroll-back. Also setting this value to a very high number will make your scrolling sluggish since the lines are stored in the memory. Cloud Shell uses Tmux, which has a scroll back buffer of 2000 lines by default.
To store your future commands and their outputs, there are few options:
Using screen
Start a screen session by entering screen. Once you are inside ‘screen’, press Ctrl-a, then :, then enter log. All the I/O will be captured in screenlog files in the directory where you started the screen command.
Using script
You can start by typing script. A script session will start that will capture all the I/O to a file named typescript. You can exit the script session by Ctrl-d and view the logs in the typescript file.
Using tee
tee is a handy tool. You can do something like this:
$ tmux | tee log.txt
This will open a new bash shell inside the one you are already running. When you exit out of this, you can see the outputs in the file called log.txt
Other ways
As Dustin Kirkland suggested in this post, you can also use byobu. Although, I have never used terminal screencasting tools such as Shelr also sounds like an option.
Other ways
Cloud Shell commands to run and a link that explains more. This will exit the terminal and thus delete all the scroll back buffer, resetting it to empty, but in the future it will save 5000 lines rather than 2000.
$ tmux show-options -g | grep history
history-limit 2000
$ echo "set -g history-limit 5000" >> ~/.tmux.conf
$ exit
$ tmux show-options -g | grep history
history-limit 5000
If you type…
$ man tmux
… you can find the documentation for this setting (search by typing '/history-limit' + (enter)).
‘history-limit lines’ set the maximum number of lines held in window history. This setting applies only to new windows - existing window histories are not resized and retain the limit at the point they were created.

I want to run some commands in the same shell script on different tabs/terminal [duplicate]

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.

How to get history of executed commands in screen terminal when we use screen command

I have created screen session with the help of screen command in Linux, for example I have created screen session with 5 bash terminals, after that in one of the created terminal I have sent "pwd" command to all the terminals with the help of following actions:
Pressed ctrla to get me to terminal prompt
Typed :at "#" stuff "pwd^M"
Then I sent an ls command: :at "#" stuff "ls^M to all the terminals
After which I want to send pwd command (#2) again
To send this I was looking for a history of the commands I sent to all terminals in screen (like hitting up arrow in bash to go through the history). All I got was the last command executed.
How do I get to the other commands I've executed in screen? Specifically, in this example, how do I get to the command I typed at #2?
if you type:
history
you will get the history of the commands. Will it help you?
Did you try ctrl+r. This is for bash but also works on screen provided it was already in the history.
Just do ctrl+r and start typing the command. It will autofill the command from the history.
How do I get to the other commands I've executed in screen?
Specifically, in this example, how do I get to the command I typed at #2?
Press Ctrl-A
Type : to get to screen prompt
Hit ↑ twice to go to the second to last command executed

Is there a way to code Ctrl+a :multiuser on when using screen?

I want to run a script that sets up a screen session and then automatically makes it into a multi-user and also adds one of the users on my system.
So far, I have a script that creates the screen session, but I have to manually make it into a multiuser session then also add the user.
As far as I have seen there is no actual coding to do this and the only way to do it is with the Ctrl+a command.
Does anyone know of a way that means the command can be done in a bash script?
You can automatically run custom commands from a configuration file - by default $HOME/.screenrc will be loaded, if it exists, so you can just do:
echo "multiuser on" >> $HOME/.screenrc
to make your default screen start with :multiuser on. If you want to have a separate config from the default, just save the config with an alternative filename, and start screen with the -c option, e.g.
screen -c multiuser.conf
It is possible to do it without entering the screen, using -X. The following lines (run by Alice) start a script in a screen and add access for the user bob.
screen -S "myscreen" -dm bash script-that-i-like.sh
screen -S "myscreen" -X multiuser on
screen -S "myscreen" -X acladd bob
Bob can then join using:
screen -x alice/myscreen

gnome-terminal executes commands from file

I need an example of gnome-terminal command to read lines of text from a file and executes them one by one in different terminal or a tab.
So this would be the process. I would run gnome terminal command and it would read 10 commands from a file. Then it would execute those 10 commands in 10 different tabs/terminals. And of course those tabs/terminals would remain opened. I found this question Avoid gnome-terminal close after script execution?
The third answer from the top is very helpful. I managed to open 1 command from a file. But I need 1 file with 10 command lines to be opened like I wrote above.
Thanks.
I recommend to use screen for this, if that can be acceptable to you.
You could create a commands.screenrc file like this:
screen bash -c 'command1; echo press any key; read'
screen bash -c 'command2; bash'
screen mutt
screen emacs
screen
You can define as many programs as you want. Start screen with:
screen -c commands.screenrc
I don't know what kind of commands you want to run. If you want to see their output, then write like the first example above: execute the command in a bash shell, which will "pause" after the command was executed. Or the second line, which, after running the command will start another bash shell. Otherwise the screen window would exit automatically.
If you are not familiar with screen, you will need to learn some basic key strokes to get around, and to be able to switch between windows. The first few pages of this presentation should be enough to get you started.

Resources