Compare file of hostnames with table in MySQL DB


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare file of hostnames with table in MySQL DB
# 1  
Old 02-15-2010
Compare file of hostnames with table in MySQL DB

Hi all,

i have a list of files that contains some PC hostname, then i need to enumerate every hostname and check if there's a table with same name of the hosts in MySQL database XYZ.
I need this because have to decide automatically if i have to make a create table or a insert into an existent table.
Database XYZ is composed by a table for every hostname i have (inventory purposes), now i have a scheduled task that creates a inventory text file and put it in a directory. Let's go back to first phrases of this post, i wish to feed a list of inventory files into another file then compare entries from that file with a ls (or similar) of sql directory.
I know for sure that for every old hostname there's a table named like the hostname but in the script that execute the sql code i need to create a new table in case of new hostname that make himself the inventory, so...

Eg.

I have a file named "hostnames" which contain:

Code:
 
pc001
pc002
pc005
...

And a directory "sqldir" which contains sql tables of the machines:

Code:
-rw-r--r-- 1 root root 12 2010-02-15 17:17 pc001.frm
-rw-r--r-- 1 root root 12 2010-02-15 17:17 pc001.MYI
-rw-r--r-- 1 root root 12 2010-02-15 17:17 pc001.MYD
-rw-r--r-- 1 root root 12 2010-02-15 17:17 pc002.frm
-rw-r--r-- 1 root root 12 2010-02-15 17:17 pc002.MYI
..............

Now... Have anybody optimal way or suggests to read the file "hostnames" and then compare every hostname with the list of the directory "sqldir" and set a variable to make possible decide if it's the time to create a new table or append a existent one?

P.S. I have already the statement to create a new one or append, need only to put the right value for the variable which decide "right" or "left"... Smilie

Sorry if i have written too much but i'm not very confident in US/english and i fear to going misunderstood... Thx

Last edited by maxlamax; 02-15-2010 at 12:48 PM.. Reason: errors
# 2  
Old 02-15-2010
try with next
Code:
#!/bin/bash
for  HH in `cat hostnames`
do
    Var=`ls |awk '/'$HH'/ {print 1}'`
    if [ "$Var" = "1" ] ; then
      echo append script for $HH
    else
      echo cretae script for $HH
    fi
done

and the result was:

append script for pc001
append script for pc002
cretae script for pc003
cretae script for pc004
cretae script for pc005
cretae script for pc006
append script for pc007


Last edited by ironmask2004; 02-15-2010 at 06:51 PM..
# 3  
Old 02-16-2010
Thx for the suggest i try right away...

P.S. Get in touch with shell programming i have found that is really hard and request a lot of time and practice anyway... Fortunately "exist" this forum "then" we may "find" the solutions for our problems... Smilie
I hope i have not to put this phrase in CODE wrap... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Getting Rows from a MySQL Table with max values?

I feel stupid for asking this because it seems that MYSQL code isn't working the way that I think it should work. Basically I wrote code like this: select * from `Test_DC_Trailer` HAVING max(DR_RefKey); Where the DR_RefKey is a unique numeric field that is auto iterated (like a primary key)... (7 Replies)
Discussion started by: Astrocloud
7 Replies

2. UNIX and Linux Applications

mysql table disappear

I have set a mysql file to excute everyday morning to generate a html file displayng 2 tables from the database. Sometime they cannot be shown, and it shows the tables are not existed. I have not drop any table, and those 2 tables are not used by any other excution. Anybody know what is happening?... (0 Replies)
Discussion started by: c203040
0 Replies

3. Shell Programming and Scripting

mysql how to select a specific row from a table

i have a table records ------------ id | user | time | event 91 admin | 12:00 | hi 92 admin | 11:00 | hi 93 admin | 12:00 | bye 94 admin | 13:00 | bye 95 root | 12:00 | hi 96 root | 12:30 | hi 97 root | 12:56 | hi how could i only select and display only the user and event from... (6 Replies)
Discussion started by: kpddong
6 Replies

4. Shell Programming and Scripting

Extract Mysql table output to a log file

I need to compare the 2 mysql database tables. there are around 50 tables in each DB. my idea is in DB1 extract result select * from table1; to alog file1 in DB2 extract result select * from table1; to alog file2 now compare log file 1 file 2 pls help me out ... thanks in advance (5 Replies)
Discussion started by: kalyankalyan
5 Replies

5. UNIX for Dummies Questions & Answers

Displaying text from a MySQL table which can be modified

Hi am creating a website for my third year at uni, am trying to create a website where the client can update the content of the site themselves, i will have a news page and i want the content to be draw from my database and displayed on the front end of the site i also want to have an admin side... (3 Replies)
Discussion started by: richeyrich86
3 Replies

6. Programming

API C MYSQL vs lock table ???

(sorry for my english) Hi, i have an app that uses MYSQL API C.. i trying do a timeout until the table is locked by an other thread , in the docs of Mysql i can see that MYSQL_OPT_READ_TIMEOUT is not implemented for linux ¿?¿?.. any body knows a way to do a timeout until the table is locked by... (0 Replies)
Discussion started by: JEscola
0 Replies

7. Shell Programming and Scripting

Help Inserting data in mysql table

Cant understand the error #!/bin/bash temp="" A="" D=$(date +"%Y-%m-%d") H=$(date +"%R") temp=$(wget -q -O - website | grep -o "Temperature:]**" | grep \-E -o "+") mysql -D "weather_wise" -e "INSERT INTO weather (Date, Hour, Degrees) VALUES ($D,$H, $temp)"; my data types for... (11 Replies)
Discussion started by: vadharah
11 Replies

8. UNIX for Dummies Questions & Answers

Editing a mySQL table on a remote machine

Hi all, My problem: I want to connect to a remote computer (in the same office as me) which is running a mySQL server, access a specific table and update it, beofre disconnecting from the server. Is this possible? If so, any links/tutorials which might be of use? I had thought of some sort of... (6 Replies)
Discussion started by: Sn33R
6 Replies

9. Shell Programming and Scripting

MySql: create table error

Hi, iam learning MySql. Iam trieing to create a table in the database "guestbook" at the command line in mysql heres what i type but i get a error mysql>create table guestbook ->( -> name varchar(40) null. -> url varchar(40) null. -> comments ... (3 Replies)
Discussion started by: perleo
3 Replies
Login or Register to Ask a Question