分享一例shell/ target=_blank class=infotextkey>shell脚本,把mysql查询结果保存到文件中。
实现方法:
先删除已经存在的文件,然后后台执行sql语句将其执行结果以一定的格式写入文件。
代码:
#!/bin/bash
# www.jb200.com
if [ -f "/var/lib/mysql/hell.txt" ]; then
rm -f /var/lib/mysql/hell.txt
echo "delete /var/lib/mysql/hell.txt" >> hell.txt
fi
mysql -uroot -plab <<eof
use home;
select yonghuid,xueyuanid into outfile './hell.txt' fields terminated by '|' lines terminated by 'rn' from sns_home_user where yonghuid < 20;
eof