LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   New Bash Q (https://www.linuxquestions.org/questions/linux-software-2/new-bash-q-4175648735/)

TBotNik 02-20-2019 07:05 PM

New Bash Q
 
All,

Need to write a script for a server to check the current DB installs and install any missing. I have this code with the results and not sure the best/fastest way to determine from the results the actions I need to take:
Code:

mystr="mysql --version";
Quote:

mysql Ver 14.14 Distrib 5.5.62, for debian-linux-gnu (x86_64) using readline 6.3
Code:

instr="ingprenv";
Quote:

ingprenv: command not found
Code:

ifstr="onstat -# onstat -";
Quote:

No command 'onstat' found, did you mean:
Command 'bnstat' from package 'pvpgn' (universe)
Command 'nstat' from package 'iproute2' (main)
Command 'lnstat' from package 'iproute2' (main)
Command 'vnstat' from package 'vnstat' (universe)
onstat: command not found
Code:

pgstr="psql --version";
Quote:

The program 'psql' can be found in the following packages:
* postgresql-client-common
* postgres-xc-client
Try: apt-get install <selected package>
As you can see the existing cmd line commands to find the 4 needed DBs vary immensely in both cmd and also in response.

The DBs are:
Quote:

MySQL
Informix
Ingres
PostGres
The only installed DB by default is MySQL, the other three need installing and the install procedure for MySQL, must be skipped, especially since any tweaks at all on MySQL totally destroys not only the MySQL but also the entire X-Win environment.

Open to suggestions on this. Have to config 5 more servers from the BASH scripts I'm currently writing.

Cheers!

TBNK

TBotNik 02-20-2019 07:15 PM

All,

It appears if I focus on the string "command not found" I can find/eliminate both the Informix and Ingres installs so code like:
Code:

case $i in "command not found"); esac
Should be adequate for these 2 DB connects and/or their installs.

Am I right?

Cheers!

TBNK

michaelk 02-20-2019 07:23 PM

Would that be the same as testing if the file exists? But you need to know the directory which may not be the same for different distribution.

ondoho 02-21-2019 04:34 AM

why aren't you showing us the script?
this makes very little sense to me...

scasey 02-21-2019 02:19 PM

Quote:

MySQL
Informix
Ingres
PostGres
  1. Identify the daemon that results from the installation of each database.
  2. use find (man find) to search for the existence of it
    and/or
  3. use ps to see if it is running.

TBotNik 03-15-2019 06:47 PM

All,

OK found this dpkg cmd:
Code:

dpkg --get-selections | grep mysql-server
Which produces this result:
Quote:

mysql-server install
mysql-server-5.5 install
mysql-server-core-5.5 install
and this cmd:
Code:

service mysql status
Which produces this result:
Quote:

mysql start/running, process 1186
So tried putting them into my script as follows:
Code:

# Check for MySQL
echo "Installing MySQL!";
my_str=$(dpkg -s mysql-server | grep status);
echo "Out=> $my_str";
case $my_str in
        "mysql-server" & "install")
                echo "MySQL Intalled";;
        "not installed")
                echo "MySQL Not installed";;
                #continue;;
esac;

my_str=$(service mysql status);
echo "Out=> $my_str";
case $my_str in
        'mysql')
                echo "MySQL Intalled";;
        *)
                echo "MySQL Not installed";;
                #continue;;
esac;

The first case the "my_str" var does not assign, but the second does.

However neither correctly render the correct outcome as though MySQL is installed, per the direct cmd line entry results, the script always shows "MySQL not installed".

I've tried "", '', and no quotes in the case statement and nothing changes.

What am I doing wrong here? I'm sure I'm not handling the string values right inside the "case" statements, but haven't been using "case" statement, until recently.

Cheers!

TBNK

michaelk 03-15-2019 08:02 PM

Code:

dpkg -s mysql-server | grep Status
The Status has a capitalized S.

and the output should be:
Status: install ok installed

In the second case pun intended you need to use pattern matching or include the exact output before the statement will evaluate to true.

ondoho 03-16-2019 04:50 AM

Quote:

Originally Posted by TBotNik (Post 5974303)
and this cmd:
Code:

service mysql status

are you sure your distro doesn't use systemd?
i'm pretty sure it should be
Code:

systemctl status mysql

TBotNik 03-16-2019 08:44 AM

Quote:

Originally Posted by ondoho (Post 5974375)
are you sure your distro doesn't use systemd?
i'm pretty sure it should be
Code:

systemctl status mysql

ondoho,

systemctl not installed!

Cheers!

TBNK

TBotNik 03-16-2019 08:49 AM

All,

Since I couldn't get the "case" statement to work right went back to:
Code:

# Check for MySQL
my_str=$(service mysql status);
if echo $my_str | grep -q 'running'; then
        echo "MySQL Already Intalled";
else
        echo "Installing MySQL!";
        apt-get install mysql-server;
fi

Now working, so running the same tests for the other DBs, but like PostGres only uses "psql" as the running process, so having to look up all these actual items that would appear in "ps -A" to correctly check all the right processes.

If you know these please chime in with the ps -A name. Since it's my server and all DBS must be available for use on the network, I'm including Oracle and MS-SQL also.

Cheers!

TBNK

michaelk 03-16-2019 09:07 AM

Technically a database can be installed but not running so your test to see if it is installed is not all inclusive.

This may work. If the string is blank mysql is installed.
dpkg -s mysql-server | grep not

ondoho 03-17-2019 01:35 PM

Quote:

Originally Posted by TBotNik (Post 5974431)
ondoho,

systemctl not installed!

Cheers!

TBNK

That would have been the perfect chance to tell us which distro you actually use for this.

Cheers!

o.

TBotNik 03-17-2019 04:01 PM

Quote:

Originally Posted by michaelk (Post 5974434)
Technically a database can be installed but not running so your test to see if it is installed is not all inclusive.

This may work. If the string is blank mysql is installed.
dpkg -s mysql-server | grep not

michaelk,

Yes I will have to put the "start" cmd in after the install, for this to be valid!

Thanks!

TBNK

TBotNik 03-17-2019 04:04 PM

Quote:

Originally Posted by ondoho (Post 5974758)
That would have been the perfect chance to tell us which distro you actually use for this.

Cheers!

o.

ondoho,

Know I stated it in the beginning, but it's Kubuntu 14.04 LTS, and have stated in other posts my reasons for being this far behind on version, expressly that none of the newer LTS version will install. All crash 1/2 - 3/4 through the install process, so always have to revert to 14.04 to have a working server or desktop.

Won't install on new or used machines and have burnt the distros with 5 different .iso makers/burners and all fail. Just won't waste time on junk! We actually got 16.04 installed on one machine and with all the things stripped out, was a bust for development, but didn't matter as the OS crashed after only 23 days of operation.

Have had 0 install and operation issues with Ubuntu/Kubuntu since 8.04 LTS, until we got to 16.04. Obviously the testing required for an OS release is not only not understood, but also not done by the "Johnny Come Lately" developers.

Cheers!

TBNK

ondoho 03-18-2019 03:31 PM

Quote:

Originally Posted by TBotNik (Post 5974821)
Know I stated it in the beginning

no you didn't.
but it's all good, at least we know now.


All times are GMT -5. The time now is 12:59 AM.