Jan
23
2007
0

Shell Script 變量中的路徑解析

編寫 Shell Script 時,如果變量的值是一個路徑的話,我們可以輕易擷取當中的路徑 (dirname) 或檔名 (basename).

變量 結果 說明
${path} /usr/local/bin/emacs 原本值
${path#/*/} local/bin/emacs 去除第一個資料夾
${path##/*/} emacs 只取檔名 (basname)
${path%/*} /usr/local/bin 只取路徑 (dirname)
${path:15} emacs 取第 15 個字元後的所有字元
${path:10:4} /bin 取第 10 個字元後的 4 個字元

最後兩個用法與 Java 的 subString 相同。

Written by Chris Lam in: Linux / Unix |
Jan
23
2007
0

刪除超大量 (eg 5000+) 檔案

如果嘗試使用 rm -f * 刪除超大量檔案 (eg 5000+),有可能會得到這個結果: Argument list too long

這種情況下唯有使用 FOR 迴圈,以一個指令便可把檔案逐個刪除:
for i in $(find /home/peter -type f); do rm -f $i; done

或者照樣使用 FOR 迴圈寫一個 Shell Script,每次刪除 10 個檔案 (沒測試過, USE AT YOUR OWN RISK):

  1. #!/bin/sh
  2.  
  3. FILES="";
  4. i=1;
  5.  
  6. for i in $(find /home/peter -type f); do
  7.  
  8.    FILES="${FILES} $i"
  9.    if [ $index -eq 10 ]; then
  10.       rm -f $FILES
  11.       i=0
  12.    fi
  13.  
  14.    i=`expr $i + 1`
  15.  
  16. done
Written by Chris Lam in: Linux / Unix |
Jan
22
2007
0

來自全球 43 個國家的客戶

由 DA-PgSQL 開始,直至 DA-Tomcat 及 Rails in DA 3 個軟件,不經不覺已經賣了 license 近半年了。

剛剛興起看看,才發現客戶分別來自全球 43 個國家。

SELECT DISTINCT(country) FROM clients LEFT JOIN orders USING(client_id) WHERE status=’active’ ORDER BY country;

AR
BE
BR
BY
CA
CH
CL
CZ
DK
ES
FI
FR
GB
GE
HK
HU
IE
IL
IN
IS
IT
KW
LT
LV
MX
MY
NL
NO
NZ
PE
PL
RO
RU
SE
SG
SI
SK
TH
TW
UA
US
YU
ZA

Written by Chris Lam in: 生活隨筆 |
Jan
20
2007
0

初嚐 TopStyle Pro

TopStyle Pro 由著名網頁編輯軟件 HomeSite 的作者所制,集編輯 HTML, XHTML 及 CSS 功能於一身。

對於小弟來說,在編寫 CSS 時使用 TopStyle 就最好不過。平日使用的 Editplus 雖然內建了簡單的 Cliptext 功能,但要緊記 CSS 中每一個 attribute 基本上是沒可能的,而每個 attribute 又有其特定的 values,忘記的時候又要上網問 Google 的了。TopStyle 擁有豐富的 Cliptext Library 及少量 Auto Completion,加上 TopStyle 提供的預覽及語法檢查功能,就正好取代 Editplus 來編寫 CSS 了。

但 HTML 及 XHTML 編寫方面,還是離不開最愛的 Editplus = =” 所以使用免費的 Lite 版本 (只有 CSS 編輯功能) 就足夠了。

TopStyle Lite: http://www.newsgator.com/NGOLProduct.aspx?ProdId=TopStyle&ProdView=lite

Written by Chris Lam in: 軟件分享 |
Jan
19
2007
0

在 Tomcat 下運行 JSP + MySQL / PostgreSQL

要在 Tomcat 下使用 MySQL 及 PostgreSQL, 首先要下載 JDBC Driver.

MySQL Connector: http://dev.mysql.com/downloads/connector/j/5.0.html

PostgreSQL Driver: http://jdbc.postgresql.org/download.html#jars

下載後把兩個 jar 檔案放在 /usr/local/tomcat/common/lib (或其他已設置好的 Classpath)

然後 JSP 檔案中要 import java.sql 中的 API classes:

  1. <%@ page import="java.sql.*"%>

JSP + MySQL:

  1. <%@ page contentType="text/html;charset=big5"%>
  2. <%@ page import="java.sql.*"%>
  3.  
  4. <%
  5.  
  6. Class.forName("com.mysql.jdbc.Driver").newInstance();
  7. String db_url ="jdbc:mysql://localhost/db_name";
  8. String db_user="db_user";
  9. String db_pw="db_pwd";
  10. Connection conn=DriverManager.getConnection(db_url, db_user, db_pw);
  11. Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  12. String sql="SELECT id, name FROM table1";
  13. ResultSet rs=stmt.executeQuery(sql);
  14.  
  15. while(rs.next())
  16. {
  17. %>
  18. <%=rs.getString(1)%> <%=rs.getString(2)%>
  19.  
  20. <%
  21. }
  22.  
  23. rs.close();
  24. stmt.close();
  25. conn.close();
  26. %>

JSP + PostgreSQL:

  1. <%@ page contentType="text/html;charset=big5"%>
  2. <%@ page import="java.sql.*"%>
  3. <html>
  4. <body>
  5. <%
  6. //Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  7. Class.forName("org.postgresql.Driver").newInstance();
  8. String db_url ="jdbc:postgresql://localhost/admin_test1";
  9. String db_user="da_admin";
  10. String db_pw="g639604";
  11. Connection conn= DriverManager.getConnection(db_url, db_user, db_pw);
  12. Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  13. String sql="SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
  14. ResultSet rs=stmt.executeQuery(sql);%>
  15. <%while(rs.next()) {%>
  16. <%=rs.getString(1)%>
  17.  
  18.  
  19. <%}%>
  20. <%rs.close();
  21. stmt.close();
  22. conn.close();
  23. %>
  24. </body>
  25. </html>
Written by Chris Lam in: 程式編寫 |

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes