How to write a shell script to open four terminals and execute a command in each? - linux

So I'm trying to create a shell script to do open up four terminal windows (konsoles preferably) and run a command in each and then keep each of those terminals open so I can continue to execute commands in them if desired.
I tried following the instructions listed here:
How to create a shell script to launch 3 terminals and execute a set of commands in each?
and
How can I make a script that opens terminal windows and executes
commands in them?
and after trying those details the best I have is the following:
#!/bin/bash
# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World"
# didn't do anything
#exit 0
# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL
EDIT:
Using Roberto's answer I get four terminals like this, but I can't enter additional commands, notice how there is no prompt like "mycomputername> ":
EDIT 2:
I found an even better way to do what I want. The script below will execute the commands listed in the cmds array in a separate terminal. So echo 'hello1' will run in one terminal, and echo 'hello2' will run in another terminal. This will continue for as many commands listed in the cmds array
!/bin/bash
# Shell script to open terminals
# and execute a separate command in each
# Commands to run (one per terminal)
cmds=('echo 'hello1'', 'echo 'hello2'')
# Loop through commands, open terminal, execute command
for i in "${cmds[#]}"
do
xterm -e "$i && /bin/tcsh" &
done

Konsole
multiple windows
#!/usr/bin/env bash
konsole --noclose -e echo Hello terminal 1! &
konsole --noclose -e echo Hello terminal 2! &
konsole --noclose -e echo Hello terminal 3! &
konsole --noclose -e echo Hello terminal 4! &
multiple tabs
#!/usr/bin/env bash
konsole --noclose --new-tab -e echo Hello terminal 1! &
konsole --noclose --new-tab -e echo Hello terminal 2! &
konsole --noclose --new-tab -e echo Hello terminal 3! &
konsole --noclose --new-tab -e echo Hello terminal 4! &

You could use a "for" loop, and a "&" to run xterm in background:
#!/bin/bash
# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases
for i in 1 2 3 4
do
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World" &
done
# didn't do anything
#exit 0
# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL

I found this to be quite easily:
#!usr/bin/env bash
echo "Enter the value of n:"
read n
for ((i = 0; i < n; i++ ))
do
xterm -hold -e <enter command> &
# In my case, I used :
# xterm -hold -e sar -P $i 2 5 &
done
And that's pretty much it! Have a good day :)
Note : For those who are newbies, we save this with a file name '.sh'. Also, please note that this will execute n different commands on n different terminals. If you want, you can execute the same command on every terminal, just remove $i from the in do .... done part ;)

On a Linux Mint mate distribution, this will run <commands> in 3 separated terminal windows:
$ cat START.sh
mate-terminal --execute bash -c "<command1>"
mate-terminal --execute bash -c "<command2>"
mate-terminal --execute bash -c "<command3>"
Killing START.sh won't terminate children <commands>.

Related

Linux Gonme: Start multiple terminals and execute a command in each one

How can I implement the following scenario in Ubuntu Linux?
I want to go to my console, then execute ./startdev.sh and then
1) Terminal 1 pops up, starts /home/foobar/bla1.sh
2) Terminal 2 pops up, starts /home/foobar/bla2.sh
3) Terminal 3 pops up, starts /home/foobar/bla3.sh
I already figured that the command "gnome-terminal & disown" starts a new terminal.
However, until now, I don't know how to run a command in that terminal.
I accept any answer that gives me either the entire implementation for startdev.sh or a list of commands that I can use.
Thank you!
Try this content for startdev.sh:
#!/bin/bash
gnome-terminal --command=/home/foobar/bla1.sh & disown
gnome-terminal --command=/home/foobar/bla2.sh & disown
gnome-terminal --command=/home/foobar/bla3.sh & disown
But it is not clear for me why you need to disown the launched processes.
Try this script
If you need to simultaneously pop all terminals
#!/bin/bash
gnome-terminal -e "bash -c '/home/foobar/bla1.sh; sleep 10'" | gnome-terminal -e "bash -c '/home/foobar/bla2.sh; sleep 10'" | gnome-terminal -e "bash -c '/home/foobar/bla3.sh; sleep 10'"
else if you need to run commands one by one terminal then .,
#!/bin/bash
gnome-terminal -e "bash -c '/home/foobar/bla1.sh; sleep 10'"
gnome-terminal -e "bash -c '/home/foobar/bla2.sh; sleep 10'"
gnome-terminal -e "bash -c '/home/foobar/bla3.sh; sleep 10'"
this script will open multiple terminals and execute the command given within the quotes and i have used sleep to hold the terminal from exiting if not added gnome-terminal will execute command and exit immediately.

TCL script to open multiple xterms and fire jobs in the opened xterms

I want to write a script in tcl to perform the below operation.
set ss test1
if ([string compare $ss "test1"]) {
## Open new xterm
Ex:- bsub -Ip -n 1 -M 100 -q <queue_name> xterm &
## go to dir2
Ex:- cd dir2
## then source test1_file in the opened xterm
}
if ([string compare $ss "test2"]) {
## Open new xterm
Ex:- bsub -Ip -n 1 -M 100 -q <queue_name> xterm &
## go to dir2
Ex:- cd dir2
## then source test2_file
}
when I try with xterm -e "cd dir1" & in the unix terminal xterm was opened and it got killed automatically.
Please help me how to do this.
Thanks and Regards,
Gunnesh.
I'm not sure entirely, that this would help, but I use something like:
1) create run.sh containing:
#! /bin/bash
eval "$#"
echo "Press return to continue"
read
In Windows, it would be run.bat:
#echo off
%*
pause
2) to execute a command (say, "dir"), use Tcl command:
exec xterm -e ./run.sh dir
This would display a console, a result of executed command and a prompt "Press...".

Bash - Start a new instance of a command in another terminal seperate from your current terminal

I have a simple bash script (test.sh) set up like this:
#!/bin/bash
args=("$#")
if [[ ( ${args[0]} = "check_capture" ) ]]; then
watch -n 1 'ls -lag /home/user/capture0'
watch -n 1 'ls -lag /home/user/capture1'
watch -n 1 'ls -lag /home/user/capture2'
exit
fi
Files are continuously being written to these target locations capture 0, capture 1, and capture 3. I want to be able to watch these directories using ls command continuously on 3 seperate terminals, and once I run this script (test.sh) from the current terminal, I want it to exit.
Right now it is blocked by each wait, which I know is a blocking bash command waiting for user input control-c. Is there a way I can have the 3 watch commands be executed in seperate terminals then reach the exit statement?
You can start several instances of the terminal in background, each one running a command, like this:
if [[ ... ]]; then
xterm -e 'watch -n 1 "ls -lag /home/user/capture0"' &
xterm -e 'watch -n 1 "ls -lag /home/user/capture1"' &
...
exit
fi
Check man xterm:
-e program [ arguments ... ]
This option specifies the program (and its command line arguments) to be run in the xterm window. It also sets the window title and icon name to be the basename of the program being executed if neither -T nor -n are given on
the command line. This must be the last option on the command line.
The same option works also for xfce-terminal and gnome-terminal.
In addition, xterm (and others) also support setting the title of the window, position, size (called geometry), colors, fonts, and many, many other features.

command to record terminal does not work with bash

I would like to use "script" command, I have the following code
#!/bin/bash
script &
wait
echo "hello"
echo "hello2"
pid=$(pidof script | awk '{print $1}')
kill -9 $pid
I need the script command to capture the output, but after the command "script &" the output is :
Script started, file is typescript
Script done, file is typescript
and script does not record nothing, any idea of why?
This is how you should do it:
script <output-file> <commands>
Example:
script typescript bash -c 'echo "hello"; echo "hello2"'
Script started, output file is typescript
hello
hello2
Script done, output file is typescript
Then check output file created:
cat typescript
Script started on Sat Dec 19 01:54:04 2015
hello
hello2
Script done on Sat Dec 19 01:54:04 2015
There are two ways you can use the script command :
Save only the outputs of your code (i.e. batch mode)
$ script filename bash -c 'echo foo; echo bar'
which will output
Script started, file is filename
foo
bar
Script done, file is filename
Save all what is displayed on your terminal (i.e. interactive mode). To end the scripting, just type exit or hit Ctrl-D
$ script filename
Script started, file is filename
$ echo foo
foo
$ echo bar
bar
$ exit
exit
Script done, file is filename
Note that the batch way is a hack on the interactive classical way of using script.
In your case, just forget about the & and kill stuff and hit Ctrl-D when you want the script to end.

Shell scripting shell inside shell

I would like to connect to different shells (csh, ksh etc.,) and execute command inside each switched shell.
Following is the sample program which reflects my intention:
#!/bin/bash
echo $SHELL
csh
echo $SHELL
exit
ksh
echo $SHELL
exit
Since, i am not well versed with Shell scripting need a pointer on how to achieve this. Any help would be much appreciated.
If you want to execute only one single command, you can use the -c option
csh -c 'echo $SHELL'
ksh -c 'echo $SHELL'
If you want to execute several commands, or even a whole script in a child-shell, you can use the here-document feature of bash and use the -s (read commands from stdin) on the child shells:
#!/bin/bash
echo "this is bash"
csh -s <<- EOF
echo "here go the commands for csh"
echo "and another one..."
EOF
echo "this is bash again"
ksh -s <<- EOF
echo "and now, we're in ksh"
EOF
Note that you can't easily check the shell you are in by echo $SHELL, because the parent shell expands this variable to the text /././bash. If you want to be sure that the child shell works, you should check if a shell-specific syntax is working or not.
It is possible to use the command line options provided by each shell to run a snippet of code.
For example, for bash use the -c option:
bash -c $code
bash -c 'echo hello'
zsh and fish also use the -c option.
Other shells will state the options they use in their man pages.
You need to use the -c command line option if you want to pass commands on bash startup:
#!/bin/bash
# We are in bash already ...
echo $SHELL
csh -c 'echo $SHELL'
ksh -c 'echo $SHELL'
You can pass arbitrary complex scripts to a shell, using the -c option, as in
sh -c 'echo This is the Bourne shell.'
You will save you a lot of headaches related to quotes and variable expansion if you wrap the call in a function reading the script on stdin as:
execute_with_ksh()
{
local script
script=$(cat)
ksh -c "${script}"
}
prepare_complicated_script()
{
# Write shell script on stdout,
# for instance by cat-ting a here-document.
cat <<'EOF'
echo ${SHELL}
EOF
}
prepare_complicated_script | execute_with_ksh
The advantage of this method is that it easy to insert a tee in the pipe or to break the pipe to control the script being passed to the shell.
If you want to execute the script on a remote host through ssh you should consider encode your script in base 64 to transmit it safely to the remote shell.

Resources