Following script can select data from MySQL and save to CSV.
$mysql_bin -u xxx -pxxx dbname << EOFMYSQL
SELECT *
INTO OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY ‘\\’
LINES TERMINATED BY '\n'
FROM table;
EOFMYSQL