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
Serial Port Doesn't Work Properly After Reboot, Unless I Execute Minicom
Minicom Black Background Color Is Not Respected
Trying to Search Files from User Keyword in Bash
Bash Script; How to Use Vars and Funcs Defined After Command
How to Preserve Command Line Spaces in a Linux Application
Different File Owner Inside Docker Container and in Host MAChine
How to Run a Mips Binary on X86 Platform
How to Copy All PDF Files from a Directory and Its Subdirectories to One Location
Linux Sed Command - Using Variable with Backslash
Pass Command-Line Arguments to Grep as Search Patterns and Print Lines Which Match Them All
Bash Variable Assignment Not Working Expected
Python Module Import Error for One User But Not the Other
How to Understand "Cmpl $0X0, -0X30(%Rbp)"/"Je ..."
Prompting for User Input in Assembly Ci20 Seg Fault
Passing Main Script Variables into Perl Modules
Interprocess Communication Using Pipe in Linux
Executing String Sent from One Terminal in Another in Linux Pseudo-Terminal