LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2017, 01:58 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
Running Bash script - Output can't be seen because command window closes


I have a bash script that when run the output scrolls by really fast and then the command window closes. You can't see the output.

What would cause this? How to fix?

Code:
echo "======================="
echo 
echo "-- START --"
echo
pwd
#exit
# bash	SET is a bash command - allows you to change the values of shell options
# bash -z file attribute check to see if permissions are ok
# bash $1 positional parameter

echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > tmp_sqlhead.sql
echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;" > tmp_sqlend.sql

if [ -z "$1" ]		# $1 is command line parameter ie: 'accounts' or 'polly' etc.
  then
    echo "-- Dumping all DB ..."
    echo
    for I in $(mysql -urick -prick -e 'show databases' -s --skip-column-names); 
    do
      if [ "$I" = information_schema ] || [ "$I" =  mysql ] || [ "$I" =  phpmyadmin ] || [ "$I" =  performance_schema ] 
      	then
         	echo "-- Skip $I ..."   # exclude these DB's
       	continue
      fi
       d=$(date +%Y%b%d-%T_)	# get current date and time
       echo "-- Dumping ""$d""$I ..."
   	    mysqldump -urick -prick --add-drop-database --databases $I | cat tmp_sqlhead.sql - tmp_sqlend.sql  | gzip -fc > "/home/rick/DB-Web/mydb-bakup-folder/"$d"$I.sql.gz" 
    done
else			# ========  process single file from command line parameter  ============
      I=$1;
      d=$(date +%Y%b%d-%T_)
      echo "-- Dumping ""$d""$I ..."
		mysqldump -urick -prick --add-drop-database --databases $I | cat tmp_sqlhead.sql - tmp_sqlend.sql  | gzip -fc > "/home/rick/DB-Web/mydb-bakup-folder/"$d"$I.sql.gz"   
 fi

# remove tmp files

rm tmp_sqlhead.sql
rm tmp_sqlend.sql

echo
echo "-- FINISH --"
 
Old 09-25-2017, 03:28 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Just add at the bottom of the script:
Code:
read -t 20 || exit
read -p "Press <enter> again to close   "
The script won't terminate until the first read times out in 20 seconds. If you press <enter> during that time, then the second read will wait forever for another <enter>.
 
Old 09-25-2017, 07:50 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks rknichols,

Why is this script doing this. Most of the ones I write don't present this sort of behavior.

See this script which acts normally.

Code:
 #!/bin/bash 
  #Sun 24 Sep 2017 18:23:28 
  
  # This is the companion script to mydb-mysqldump.sh
  # mydb-mysqlrestore.sh  will restore the files dumped by that program
  
  #File: DB-Web/mydb-mysqlrestore.sh
  
folder="/home/rick/DB-Web/mydb-bakup-folder"

# if gzip files unzip them
find "$folder" -type f -iname '*gz' -exec gunzip {} + 

   echo "-- Start"
   echo
   echo -- Restoring --
   echo
    
    for I in $(ls -d "$folder"/* ) 
    	do
    	   echo "$I" | awk -F_ '{print $2}' 	 
    		mysql -urick -prick < $I 
		done


echo

    	
echo "-- Finish"
 
Old 09-25-2017, 08:27 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
What matters is how the script is invoked. If either script is run from an existing command window, then that window will remain open when the script exits and returns control to that command shell**. But, if you launch the script from a menu or a desktop launcher configured as "Run application in a terminal", then there is no shell to return to and the window will close immediately when the script exits. The commands I suggested provide a way to keep such a terminal window open.

** If the command you type is "exec path/to/script", then the invoking shell is overlaid by the one running the script and the window will close immediately upon termination.
 
Old 09-25-2017, 09:17 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Is there a way to not have the 'baby' command window to show the output of a script run through the launcher.

If I can't get the regular display to show output is there a way to change font etc from the 'baby' window. See attachment for what I'm talking about. you have to use a magnifying glass to read Enter Password:
Attached Thumbnails
Click image for larger version

Name:	Selection_003.png
Views:	28
Size:	24.3 KB
ID:	25966  
 
Old 09-26-2017, 12:56 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
I have no idea what do you mean by baby window. You can redirect the output of your shell script into file and use a text editor to check the content (of that file).
 
Old 09-26-2017, 01:07 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
how are you invoking the script(s)?
 
Old 09-26-2017, 02:37 PM   #8
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Quote:
how are you invoking the script(s)?
I am using a launcher as attached.
Attached Thumbnails
Click image for larger version

Name:	mysqldump-launcher.png
Views:	31
Size:	48.1 KB
ID:	25977  
 
Old 09-26-2017, 03:06 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by pizzipie View Post
Is there a way to not have the 'baby' command window to show the output of a script run through the launcher.

If I can't get the regular display to show output is there a way to change font etc from the 'baby' window. See attachment for what I'm talking about. you have to use a magnifying glass to read Enter Password:
It's just using the default terminal. Open the default terminal yourself, change the default font, and that should change it for your script.
 
Old 09-26-2017, 08:19 PM   #10
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Quote:
It's just using the default terminal. Open the default terminal yourself, change the default font, and that should change it for your script.
The default terminal has a white background with black text and a menu with a number of options. The one that shows up when you use the launcher has a black background with white text and a minimal menu where all you can do is change the size a little bit.

We're talking two different animals here.

R

Last edited by pizzipie; 09-26-2017 at 08:21 PM.
 
Old 09-27-2017, 05:36 AM   #11
Ratamahatta
Member
 
Registered: Feb 2012
Location: Germany
Distribution: siduction
Posts: 134

Rep: Reputation: 17
Seems like you have more than one terminal emulater (i.e. "animal" ) installed on your system.

Open a terminal and before and when that problematic script is running enter:
Code:
ps ax
Then compare the outputs. There should be two lines more in the second output. One is the script and one is the terminal emulator used for that script. Then you may be able to start that other terminal emulator (because now you know it's name) and change it's settings.

Another way might be changing the default terminal emulator. On my Debian based system the link is /etc/alternatives/x-terminal-emulator. Should be similar on Ubuntu. Do (assuming the path is the same):
Code:
ls -l /etc/alternatives/x-terminal-emulator
to see what it points to and compare it to the name of your usual terminal emulator.
 
Old 09-29-2017, 11:14 AM   #12
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
A picture is worth a thousand words eh?

Apparently I can't explain myself properly.

R.
Attached Thumbnails
Click image for larger version

Name:	Displays.png
Views:	31
Size:	207.7 KB
ID:	25994  
 
Old 09-29-2017, 12:36 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
That launcher is not creating that window itself. Something in the script is doing that.
 
Old 09-29-2017, 02:33 PM   #14
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
your op says "output can't be seen because command window closes" but now we can see it.
so, we moved on to the next problem, which is too small font?
you can either edit the script, or, since this is clearly xterm in the scrot, you can make a quick web search on how to change xterm's font size. it can even use ttf/otf fonts.
 
Old 09-29-2017, 04:39 PM   #15
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
I apologize to all for mixing up two different threads.

The most recent answers from rknichols and ondoho got me thinking along a different path.

The answer is contained in this link: https://ubuntu-mate.community/t/laun...-terminal/4918

Specifically:
Quote:
MY SOLUTION

There's many solutions, including forgetting the "Application in Terminal" altogether and just run mate-terminal with the -e option but I wanted to have my cake and eat it, too. And it's a one-time command:
Code:
sudo ln /usr/bin/mate-terminal /usr/bin/gnome-terminal
I hope this helps someone else who has a similar problem.

R

Last edited by pizzipie; 09-29-2017 at 04:42 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with running bash script on open terminal window! gnome1919 Linux - Software 2 07-15-2014 07:02 AM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Bash script - output of command into array. systemlordanubis Programming 2 05-02-2014 11:22 PM
bash script - open new window running a command zippity Linux - Newbie 1 07-13-2004 11:16 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration