[sh] get updated database name from MySQL into txt file

Posted by

The specific task here: know what databases are running on MySQL using a script on shell “sh” and saving that information on a .txt file.

The code is very simple,

1 !#/bin/sh
3 PATHDB=/path/to/save/txtfile
5 echo “———————————-”
6 #echo “Archivo con actualizacion de las bases de datos que contiene MySQL”
7 #(spanish comment, “uncomment if want to use it.”)
8 echo “File plane text with the updated databases that are running on MySQL”
9 echo “———————————-”
11 START=`date +%s`
13 echo “Start: `date`”
15 mysql -u[user] -p[password] -e ‘show databases;’ > /$PATHDB/[nameoftxtfile.txt]
17 sed -i ‘/linetodelete/d’ $PATHDB/[nameoftxtfile.txt]
19 SPENT=$(((`date +%s` – $START)/60))
21 echo
22 echo “Duration: $SPENT minutes”
23 echo “Date: `date`”
24 exit 0


The script has a variable to save the path where the txt file will be saved. ($PATHDB) after that, some variables to show when the script starts. The line (15) need some changes because you need to use the right “username” and “password“, also, you will need to set the “filename” where the information (results) will be saved.

This script implements the command “sed”, (that command allows you to “filter or transform” text) so, basically is there to remove a specific line from the .txt file. You can use it or comment/remove the line to not use it.

Finally, has a variable to show you how much time spend for this specific task to successfully complete the objective.

To run any script,

1. Open a SSH connection to the host, find the script that you want to run and do the following,


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s