组装MySQL执行文件(重定向示例)

来源:互联网 发布:服装设计用什么软件 编辑:程序博客网 时间:2024/06/11 02:16
#!/bin/bash  outfile='merber.sql'  IFS=','  while read lname fname address city state zip  do  cat >> $outfile << EOF  INSERT INTO members (lname,fname,address,city,state,zip) VALUES('$lname', '$fname', '$address', '$city', '$state', '$zip');EOFdone < $1

示例要求:从一个csv文件(merber.csv)里读取内容组成sql语句,保存在merber.sql里。

root@xxxxx:~# cat merber.csv Blum,Richard,123 Main St.,Chicago,IL,60601Blum,Barbara,123 Main St.,Chicago,IL,60601Bresnahan,Christine,456 Oak Ave.,Columbus,OH,43201Bresnahan,Timothy,456 Oak Ave.,Columbus,OH,43201

有几点说明:

  1) done < $1里。必须写在同一行里。read 在while模块里,需要从重定向的外部文件里读取内容。$1表示命令行的第一个参数,即要读取内容的文件

 2)  cat  >> $outfile <<  EOF这一行里 。注意EOF是边界符号,cat读取两个EOF里面的内容,追加写入到$outfile里



原创粉丝点击