Andreas Pabst
Andreas Pabst

Reputation: 105

Unix Shell Script: Reading and working with MySQL Rows line by line

I want a shell script (ubuntu) which fetchs data from a sql table row by row! And i want to have access on EACH row, e.g. to Check with a simple if statement, u know?

#!/bin/bash
TIME=`date +"%T"`
echo true  > log/ausgefuehrt-$TIME.log
/opt/lampp/lampp start
echo $TIME

#ASSIGNING MYSQL
MYSQL=/opt/lampp/bin/mysql
#$MYSQL -e"select * from ftp.ftp" -u root
$MYSQL -e"select * from ftp.ftp"

#STARTING TO FETCH EACH ROW (trying)
WHILE read ROW ;
        do echo $ROW # HERE <<< is the problem.. i dont know how to access each row :/
done
exit

Upvotes: 0

Views: 5591

Answers (1)

dogbane
dogbane

Reputation: 274612

Pipe the output of mysql through a while loop as shown below:

$MYSQL -e"select * from ftp.ftp" | while IFS= read -r ROW
do
    echo "$ROW"
done

Upvotes: 7

Related Questions