网上看了一个不错的脚本,改了一点点,记录一下,

 

  1. #!/bin/sh 
  2. CSVFILE= 
  3. while getopts c: opt 
  4. do 
  5.         case $opt in 
  6.                 c) CSVFILE=$OPTARG 
  7.                 ;; 
  8.                 '?') echo "Usage: -c csv file name" 
  9.                 ;; 
  10.         esac 
  11. done 
  12.  
  13. TD_STR="" 
  14.  
  15. #this function create a <td> block 
  16. create_td() 
  17.   #echo $1 
  18.   TD_STR=`echo $1 | awk 'BEGIN{FS=","}{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'
  19. #this function create a row html script(<tr>block). 
  20. create_tr() 
  21.   create_td "$1" 
  22.   echo -e "<tr>\n$TD_STR\n<tr/>\n" 
  23. #create html script head 
  24. create_html_head() 
  25.   echo -e "<html>\n<body>\n<h1>$CSVFILE</h1>\n" 
  26. #create html script end 
  27. create_html_end() 
  28.   echo '</body></html>' 
  29. create_table_head() 
  30.   echo -e "<table border="1">\n" 
  31. create_table_end() 
  32.    echo -e "</table>\n" 
  33. create_html_head 
  34. create_table_head 
  35. while read LINE 
  36. do 
  37.  # echo "$LINE" 
  38.   create_tr "$LINE" 
  39. done < $CSVFILE 
  40. create_table_end 
  41. create_html_end