数据库:mysql
操作系统:FreeBSD(类UNIX操作系统)
上代码
#!/bin/bash
#数据库用户名,注意等号两边不能有空格
USER_NAME=username
#数据库密码
PASSWORD=password
#schema
DB_NAME=schema
#(想写入的)文件的位置
FILE_PATH=/test/2.txt
#内容,覆盖原先内容【>】,注意特殊符号要加上转义符才会正常显示例如【"】
echo "today i feel so sad because of someone \"xiaohong/xiaoming?\"
it may not bu great literature but it certainly had me riveted
classic works of literature"> ${FILE_PATH}
SQL="select field as fieldname from tablename where name<>'aa' "
#注意此处必须要加【`】,$SQL外边必须有双引号
table_data=`mysql -u${USER_NAME} -p${PASSWORD} ${DB_NAME} -e "$SQL"`
for data in $table_data
do
#查出来的数据title也带上了,此处去掉
if [ $data != "fieldname" ]
then
#文件中追加内容【>>】
echo " $data:\"$data\"
an expression of amusement animated their faces
the natural word.both animate and inanimate" >> ${FILE_PATH}
fi
done
exit 0
测试
执行 ss 22.sh
报了警告,意思是说sh文件里边使用密码可能不安全,在意的话就去掉,执行的时候再输入
文件生成了
文件内容