Hi,
I am trying to use mysql select query in a shell script but i need to know how to store a particular value from the output of the query in a script variable.
my script file is given below.
I need to store the value under field CheckSum in the script variable $CHECKSUM_ORIG .There is only 1 entry in the db. The lineCHECKSUM_ORIG=$sqlquery;
doesnt work and gives a error. Please let me know the correct syntax/how to do it. If you have any link to a book on that u can share it as this is the 1st time i m using sql from scripts..
The program below is supposed to calculate the current check sum of a file and compare it with the checksum value already stored in mysql. If the value is same that means that the file is not corrupted.
#!/bin/sh
# Run using ./computeChecks.sh <filename>
# Shell script to compare cksum difference between two files
sqluser="root"
sqlpasswd=""
db="CHECKSUM_DB"
FILE=$1
CHECKSUM_ORIG=""
start () {
CHECKSUM=`cksum "$FILE" | cut -f1 -d" "`
sqlquery="select CheckSum from DbCheckSumTbl where DbName='$FILE'"
#echo -e "$sqlquery"
if [ -n "$sqlpasswd" ]; then
connect=" mysql -u "$sqluser" -p "$sqlpasswd" "
else
connect=" mysql -u "$sqluser" "
fi
eval $connect <<EOF
use $db;
CHECKSUM_ORIG=$sqlquery;
EOF
#**** the line b4 EOF doesnt work *****#
#CHECKSUM_ORIG=$sqlquery;
#CHECKSUM_ORIG=`$CHECKSUM_ORIG| cut -f1 -d ""`
#echo $CHECKSUM_ORIG
#echo $CHECKSUM
if [ "$CHECKSUM_ORIG" != "$CHECKSUM" ]; then
echo "corrupted file: $FILE"
exit 0
else
echo "file ok: $FILE"
exit 1
fi
}
start