Run a Shell Script in New Terminal from Current Terminal

Run a shell script in new terminal from current terminal

Here's a simple example to get you started:

To write a shell script, do this on your command prompt:

echo -e '#!/bin/sh\n echo "hello world"' > abc.sh

This writes:

#!/bin/sh
echo "hello world"

To a file called abc.sh

Next, you want to set it to executable by:

chmod +x abc.sh

Now, you can run it by:

./abc.sh

And you should see:

hello world

On your terminal.

To run it in a new terminal, you can do:

gnome-terminal -x ./abc.sh

or, if it's xterm:

xterm -e ./abc.sh

Here's a list of different terminal emulators.

Alternatively, you just run it in your current terminal, but background it instead by:

./abc.sh &

Bash script. Open new terminal and run command

To execute a command without waiting for it to finish, put it in the background with &.

#!/bin/bash 
COUNTER=0
xterm -e mosquitto_sub -h 192.168.1.103 -t test &
mosquitto_pub -h 192.168.1.103 -t test -m "Connected"
cd Desktop/ScreenTool/image/
while [ $COUNTER == 0 ]; do
tesseract c.png output
if grep -q Click "/root/Desktop/ScreenTool/image/output.txt"; then
mosquitto_pub -h 192.168.1.103 -t test -m "Rain is here"
echo -en "\007"
fi
cat "/root/Desktop/ScreenTool/image/output.txt"
sleep 3;
done

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

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


Related Topics



Leave a reply



Submit