Visit Our UNIX and Linux User Community

[BASH] Gawk + MYSQL script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [BASH] Gawk + MYSQL script
# 1  
Old 07-26-2013
[BASH] Gawk + MYSQL script


I've got script to write. It should read databases (names, volumes) from table testdatabase and compares it to actually existing databases in /var/lib/mysql/. If there is no informations about database in table - we should see information "There is no declared informations about database in table testdatabase." And if volume of database is too big - we should see information "Database database has too big actual size volume, it exceeds declared size in table testdatabase."

My problem is that even if there is existing database in /var/lib/mysql/ and I put informations about it in table testdatabase - I see statement - "There is no declared...".

Do you have any idea what am I doing wrong?

cd /var/lib/mysql/ || exit 1

gawk '
   cmd = "mysql -u root -ppassword knowdatabases \"SELECT name, volume, date, description FROM testdatabase\""
   while ( cmd | getline ) {
      DATABASE[$1,1] = $2;
      DATABASE[$1,2] = $1;
   cmd = "find /var/lib/mysql/ -maxdepth 1 -mindepth 1 -type d | xargs du -sm"
   while ( cmd | getline ) {
      VOL = $1;
      NAM = $2;
      if ( ! DATABASE[NAM,1] ) {
         print "There is no declared informations about " NAM " in table testdatabase."
      if ( VOL > DATABASE[NAM,1] ) {
         print "Database " NAM " has too big actual size (" VOL "), it exceeds declared size in table testdatabase."
' /dev/null

# 2  
Old 08-02-2013
You could do much more is SQL, with case. Part of the task is to compare lists, which should say if either list is short, something comm is good at. You could sort and rely on order to merge the lists.

Last edited by DGPickett; 08-02-2013 at 07:34 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #237
Difficulty: Easy
Paul Baran was the first to build a theoretical model for communication using packet switching.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using bash script : How to Import data from a dsv file into multiple tables in mysql

HI I have a dsv file that looks like: <<BOF>> record_number|id_number|first name|last name|msisdn|network|points|card number|gender 312|9101011234011|Test Junior|Smith|071 123 4321|MTN|73|1241551413214444|M 313|9012023213011|Bob|Smith|27743334321|Vodacom|3|1231233232323244|M... (4 Replies)
Discussion started by: tera
4 Replies

2. Ubuntu

Bash script for FTP download -Mysql

Hi guys, I recently managed to write up my working script, but now I have a problem. If the file isn't there in the remote server, my actual script jumps it and all ok, but I need something like this: Search file -> if there, then download -> if not, download next file in the list. Any... (7 Replies)
Discussion started by: virtus96
7 Replies

3. Shell Programming and Scripting

Update a mysql column via bash script

Hello, I want to check the value of all MySQL columns.(column name is "status") via bash script. If value is "0" at I want to make only single column value to "1" I have many "0" values on mysql database(on "status" column) "0" means it is a draft post. I want to publish a post. I... (2 Replies)
Discussion started by: tara123
2 Replies

4. Shell Programming and Scripting

syntax issue mysql in bash script

I'm running mysql in a bash script mysql <<EOF query EOF one query is like this: UPDATE $dbname.$prefix"config" SET value = $var WHERE "$prefix" = 'table colname'; with variable but it's giving an error i'm not sure what to put for "$prefix" the table... (3 Replies)
Discussion started by: vanessafan99
3 Replies

5. Shell Programming and Scripting

syntax issue with quotes in mysql command for a bash script

i'm trying to write a bash script that executes a mysql statement mysql -sN -e INSERT INTO "$database"."$tableprefix"users (var1, var2,var3) VALUES (123, '1','') i don't know where to put the quotes it doesnt work with this one: ` it seems i can only put double quotes around the... (0 Replies)
Discussion started by: vanessafan99
0 Replies

6. Shell Programming and Scripting

How to run bash script from within MySQL

Can someone tell me the syntax to run a shell script from within MySQL? I know there is a way to do it, but I can't remember the syntax. I thought it was something like: mysql> \. /user/myscript; but I get this error: Failed to open file '/user/myscript;', error: 2 Please help!... (4 Replies)
Discussion started by: peterv6
4 Replies

7. Shell Programming and Scripting

mysql and bash...

Ok the issue I have is a bit complicated, for me, and my understanding of shell programming and bash is non-existent. Background: I do web development for a team of programmers that make custom kernels for the Android OS. The guys are using a bash script (through gerrit or github, like I said... (4 Replies)
Discussion started by: Nyght
4 Replies

8. Shell Programming and Scripting

Create mysql database with bash script - confused

Hi, i have the following: db="create database xxx;GRANT ALL PRIVILEGES ON xxx.* TO user@localhost IDENTIFIED BY 'password';FLUSH PRIVILEGES;quit;" mysql -u root -p$mysql_pass -e "$db" I don't understand why this is failing, it works fine when run from cmd but when is run in a bash script,... (1 Reply)
Discussion started by: ktm
1 Replies

9. Shell Programming and Scripting

Passing a MySql password from bash script

Hi all, I am running this script on Mandrakelinux release 10.1, #1 SMP I have also installed 'expect' separately. I have created an Rsync script, but before any Rsync command does run, a MySql dump must be done first, and I am battling a bit to pass the MySql password from... (2 Replies)
Discussion started by: codenjanod
2 Replies

10. Shell Programming and Scripting

gawk and bash

Hi. I'm having trouble using gawk within a bash script and I can't figure out why. I have a command that takes in a data file with two columns, the first one numbers and the second words. My code takes each line, and prints the word its corresponding number of times. The code works from the... (2 Replies)
Discussion started by: cdislater
2 Replies

Featured Tech Videos