|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
insert values into table MySQL ?
I have written a "BASH" script that sends the output of the "date" , "who" , and "top" commands to a file. Instead of writting to this file I would like to redirect the output of each command to a specified field in a table in MySQL. The Bash code is below, I half way know the insert commands for MySQL but I can always look at the " PDF Manual ".
Code:
#! /bin/bash
LOG=/home/BaShScr/testlogger-$(date +%Y$m%d).log #Name of File that is written
ERR=/home/BaShScr/testloggererr.log #Name of error file that is written
if [ ! -e $LOG ] # If Log file does not exist then it is created
then {
date 1> $LOG
echo "" 1>> $LOG # If the Log file has to be created then
who 1>> $LOG # this block of code is executed
echo "" 1>> $LOG
#uptime 1>> $LOG
top -b -n1 1>> $LOG
echo Logged the Date, Logged on users, and System uptime ! # Letting the user know the file had to be created.
read # Script is finished and waiting for ENTER
clear # Clears the buffer
}
elif [ -e $LOG ] # If Log file exists then with same name the file is appended
then {
echo "################################################## ######" 1>> $LOG
date 1>> $LOG
echo "" 1>> $LOG
who 1>> $LOG # If file is getting appended then
echo "" 1>> $LOG # This block of code is being executed
top -b-n1 1>> $LOG
echo Log file appended ! # Letting the user know the file already existed and was appended
read # Script is finished and wating for key stroke
clear # Gives user a nice clean slate
}
else
{
if [ ! -e $ERR ]; then
{
echo 2> $ERR
echo OOPS! Nothing Happened !# User will see this if all else fails
echo Check testlogerror.err file for information # Gives users place to look if they want to knwo the answer to above
}
elif [ -e $ERR ]; then
{
echo 2>> $ERR
echo OOPS! Nothing Happened !# User will see this if all else fails
echo Check testlogerror.err file for information # Gives users place to look if they want to knwo the answer to above
}
fi
}
fi
From the code above you can see what I am asking. For example I am able to [ date 1>> $LOG] and instead I want to [ date 1>> INSERT INTO name_of_table, name_of_field, VALUES ] thanks in advance for any help. |
|
#2
|
|||
|
|||
|
RE: insert values into table MySQL ?
Something like this should work:
echo "INSERT INTO table_name(field1, field2) VALUES("`date`", "`uptime`")" | mysql -uusername -ppassword dbname note that `command` (that's ``, not '' !) gets replaces by the output of that command. Substitute commands, fieldnames etc. as necessary |
|
#3
|
|||
|
|||
|
RE: insert values into table MySQL ?
I guess to make this more understandable::
I am trying to get a script written that grabs the output of these commands and sends it to the correct fields in the databse: The way I have it written now the output of the commands is captured and sent to a log file. ( or you could say redirected) I already have the database created and the table with the fields ( Date, Logged_on, Process_stats) The database name is "testlogger " and the table name is "LOGS" Hopefully this help you understand what I am looking for. The point behind this is to make the insertion of the output from the commands into the databe automated when the script is run. This means more coding but that is what I am trying to learn: Plus in the end I don't have to manually insert the data evey time I want to update the log I am keeping of the date, users logged on, and list of processes. Thanks again:: I hope this helps explain what I am asking :: |
|
#4
|
|||
|
|||
|
RE: insert values into table MySQL ?
Well, then do this
echo "INSERT INTO LOGS(Date, Logged_on, Process_stats) VALUES ("`date`", "`who`", "`top -b -n `")" | mysql -uusername -ppassword testlogger As you can see, echo outputs the SQL statement which gets passed to mysql proggy, which sends it to the database... |
|
#5
|
|||
|
|||
|
RE: insert values into table MySQL ?
Oh i see what you were trying to tell me eariler.
sorry about the confusion. Thank you |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > insert values into table MySQL ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|