Shell script statements not running one after one

677 Views Asked by At

I wrote a shell script that calls some other shell scripts in a new terminal window. It was working fine on my computer. its like this

#!/bin/sh
gnome-terminal -e "sh one.sh"
zenity --info --text "exed one"
gnome-terminal -e "sh 2.sh"
zenity --info --text "exed 2"
firefox "www.aurl1.com" "www.aurl2.com"

According what I understood, the script will first open a terminal and run the first script, after finishing that it will show the zenity then run two in new terminal then show zenity like that. But when the same was exed on another computer, the script is not following this order. It simply opens all terminal side by side not waiting one to finish and showing the dialogues together. Why is this problem ? Thanks in advance.

2

There are 2 best solutions below

4
On

Run them like this:

gnome-terminal -e "sh one.sh" ; zenity --info --text "exed one" ; gnome-terminal -e "sh 2.sh" ; zenity --info --text "exed 2"

What the comma does is it tells the shell to execute the first command, wait for it to finish and only then move on to the next. Otherwise, the behavior you described is normal, it parses the lines one by one, without waiting for anything.

0
On

It used to be that gnome-terminal would run each terminal in its own process. But now gnome-terminal checks on startup if there is already a gnome-terminal process running, and if so, tells the existing terminal process to open a new window with the given arguments. There is no problem with your script: it is waiting for each process to finish in turn, it is just that gnome-terminal is exiting immediately after passing off its work to a different process.

According to man gnome-terminal, you can use the --disable-factory option to disable this behaviour, and run each command in its own process.

Try this:

gnome-terminal --disable-factory -e 'sh -c "echo hi && sleep 10"'
echo now you can run script 2