I have always included a “read” command at the end of a bash script if I wanted to pause the script, and keep the terminal window open, after the pre-programmed command sequence terminated. Recently, instead of running a script by double-clicking inside the file manager, I tried to run it using a program launcher. I double-clicked on the desktop icon for the launcher, but nothing seemed to happen. The terminal window that would normally display the results of the script’s commands never opened. It turns out that one needs to take additional steps to see what is happening when using a program launcher.
The problem is that “…the terminal closes when the command run inside it has finished…” So, if the command sequence is short, the terminal will open – and immediately close – and you won’t really see anything happen. The solution is that “…you need to write a command that doesn’t terminate immediately.”
In my specific case, I was experimenting with a script to safely remove a USB drive. That other operating system has such a facility, and this gives a notification to the effect that it is “Safe to Remove Hardware”. Under Linux Mint, I right-click on the USB drive’s icon, select “Eject”, but then have to wait until the red light stops flashing, before I pull out the drive.
Some sources indicate that unmounting a USB drive is the safest way to ensure that the operating system doesn’t have any data writes pending before disconnecting the drive. My intention was to have the script issue the umount command and then provide a message that it was safe to remove the drive.
My solution, therefore, was to create a bash script with two options one to note that the USB drive (a Silicon Power 16GB USB memory stick) wasn’t mounted should the script run while this was the case, or to indicate that the USB drive could be removed under the more usual scenario. The actual code for the script is as follows:
# Check that SILICON16GB is mounted
if grep '/media/toaster/SILICON16GB' /etc/mtab > /dev/null 2>&1;
mate-terminal -e "bash -c \"umount /media/toaster/SILICON16GB;
echo USB drive can be removed; echo ; exec bash\""
mate-terminal -e "bash -c \"echo SILICON16GB is not mounted;
echo ; exec bash\""
echo " "
The “trick” in this script is the use of two mate-terminal commands, one for each of the above-noted scenarios, and each including an “exec bash” statement in the executable argument. This final command effectively spawns a second instance of the terminal window which remains on the screen once the executable commands have been completed. This allows the user to review the specific message that is echoed to denote the status of the USB drive.
So, if running my script from the program launcher displays “USB drive can be removed”, I know that I can safely remove the memory stick.
Pausing a bash script
How to safely remove a USB external drive?
Linux Mint: Make a script/shortcut that can be clicked and run and will KEEP THE TERMINAL WINDOW OPEN afterwards [closed]
Avoid gnome-terminal close after script execution?