純練功。
第一個單純的想法就是 cat /proc/cpuinfo ,然後算裏面以 processor 開頭的有幾行,所以可以很直白的寫下 cat /proc/cpuinfo | grep processor | wc -l
grep 可以帶檔案,這樣就可以省掉 cat:grep processor /proc/cpuinfo | wc -l
可
以用 awk 一行搞定嗎?應該是可以喔~但有點小複雜,先用 regular expression 找出以 processor
開頭的行,然後累加變數,在 END 時再印出結果:awk '/^processor/{x+=1;}END{print x;}'
/proc/cpuinfo
星期四, 4月 11, 2013
星期五, 4月 03, 2009
[Note]Seed(4) - Database
Seed 也支援對資料庫的存取,目前只支援 SQLite。
用法也超級簡單...
基本上只有 constructor 跟用來執行 SQL 的 exec()。以下代碼來自源碼裡 (modules/sqlite/example.js):
取出資料的作法則是將 callback 傳入,以處理一筆 record。上面是用 JSON 輸出整筆 record 內容,其實你也可以將欄位名稱代入 indexer 來取得該欄內容:
用法也超級簡單...
基本上只有 constructor 跟用來執行 SQL 的 exec()。以下代碼來自源碼裡 (modules/sqlite/example.js):
#!/usr/local/bin/seed
Seed.import_namespace("sqlite");
d = new sqlite.Database(Seed.argv[2]);
d.exec("create table t1 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE);");
d.exec("insert into t1 (data,num) values ('This is sample data',3);");
d.exec("insert into t1 (data,num) values ('More sample data',6);");
d.exec("insert into t1 (data,num) values ('And a little more',9);");
d.exec("select * from t1", function(results){Seed.print(JSON.stringify(results))});
取出資料的作法則是將 callback 傳入,以處理一筆 record。上面是用 JSON 輸出整筆 record 內容,其實你也可以將欄位名稱代入 indexer 來取得該欄內容:
d.exec("select * from t1", function(results){Seed.print(results["data"])});
標籤(Label):
gtk,
javascript,
script
星期五, 3月 06, 2009
[Linux]amr 轉 mp3
環境:Ubuntu 8.10
整理一下,把它作成 script:
收工。
參考自Aquarionics 的 blog:How to convert AMR files to MP3
- 安裝必要的套件:
sudo apt-get install amrnb sox lame
- 輸入以下指令:
amrnb-decoder file.amr file.raw # 先轉成 raw 檔
sox -r 8000 -w -c 1 -s file.raw -r 16000 -w -c 1 file.wav # 再轉為 wav
lame babycry.wav babycry.mp3 # 最後轉為 mp3
整理一下,把它作成 script:
#!/bin/bash
# amr2mp3.sh
FILE=`basename $1 .amr` # remove .amr
amrnb-decoder $1 $FILE.raw
sox -r 8000 -w -c 1 -s $FILE.raw -r 16000 -w -c 1 $FILE.wav
lame $FILE.wav $FILE.mp3
rm -f $FILE.raw $FILE.wav
exit 0
收工。
參考自Aquarionics 的 blog:How to convert AMR files to MP3
星期三, 2月 18, 2009
[Note]Seed(1)
在Ubuntu裡安裝 Seed 很簡單,參考PPA for Orange Owners裡,把
執行 script 也很簡單,有兩種方法:
目前官方沒有文件說明 Seed 內部有哪些類別與方法,這很讓人困擾,這兩天看了 source code 跟 example code 之後,大致上有點了解。
Seed 主要的類別是 Seed,提供了如下方法:
deb http://ppa.launchpad.net/orange-owners/ppa/ubuntu intrepid main放到 /etc/sources.list 裡,然後用 sudo apt-get update 更新,sudo apt-get install seed 來安裝即可。
deb-src http://ppa.launchpad.net/orange-owners/ppa/ubuntu intrepid main
執行 script 也很簡單,有兩種方法:
- 直接以 seed 執行:seed your_script.js
- 把 js 檔的第一行改為 #!/usr/bin/env seed,再以 chmod +x 為 js 檔加上執行權限,就可以用 ./your_script.js 執行。
目前官方沒有文件說明 Seed 內部有哪些類別與方法,這很讓人困擾,這兩天看了 source code 跟 example code 之後,大致上有點了解。
Seed 主要的類別是 Seed,提供了如下方法:
- include:用來含括其他 js,讓你可以為程式作適當的切割,不至於讓檔案變得太大而難以維護。
Seed.include("other.js");
- print:印字串。
Seed.print("Hello world!");
- check_syntax:檢查語法,你可以傳 javascript 程式進去檢查,如果有錯,會丟出 exception。
try {
Seed.check_syntax("Seed.print(;");
Seed.print("syntax ok!");
}
catch( e ) {
Seed.print( e.message );
} - spawn:執行外部程式,執行以後會回傳一個 object,這個 object 有兩個屬性:stdout 與 stderr。
var result = Seed.spawn("ls");
Seed.print( "=== spawn result(stdout) ===" );
Seed.print( result.stdout );
Seed.print( "=== spawn result(stderr) ===" );
Seed.print( result.stderr ); - fork:這跟 C 的 fork() 一樣,回傳值是 0,表示是子行程,-1 表示失敗,大於 0 的值,表示是父行程。
var pid = Seed.fork();
if( pid == 0 ) { // child process
var result = Seed.spawn( "ls" );
Seed.print( result.stdout );
Seed.quit();
}
else if( pid == -1 ) {
Seed.print( "cannot create child process." );
}
else { // parent process.
Seed.print( "I am parent process." );
} - quit:離開。
- introspect:這個函數可以用來探知類別成員函數如何使用,安裝 Seed 以後,/usr/share/doc/seed/examples 下有個 introspect.js,就是一個很好的範例。不過我還不是很懂怎麼去用~
- import_namespace:含括其他 library 進來使用,不要跟 include 搞混了,include 是含括其他 js 檔。
標籤(Label):
gtk,
javascript,
script
星期二, 2月 17, 2009
[Note]把檔案內容放到環境變數
在 bash 下很簡單的一件事情,批次檔似乎沒有比較好的解~
在 bash 下:
在批次檔裡,我發現可以用 for 來解:
但缺點是,當檔案有多行時,VERSION 會是最後一行的內容。
在 bash 下:
VERSION=`cat version.txt`
在批次檔裡,我發現可以用 for 來解:
@For /f "" %%a in (version.txt) do (set VERSION=%%a)
但缺點是,當檔案有多行時,VERSION 會是最後一行的內容。
星期二, 10月 21, 2008
[Note]以 bash script 為 sqlite database 產生 C/C++ struct
最近寫的一個 bash script,用來幫你把 sqlite database 裡的 table schema 轉成 C/C++ struct。
這裡只處理 text 與 integer 型態,text 轉成 wstring/string,integer 則轉為 int。轉換的工具是使用 awk/sed。
只要稍稍改動一下,也適用在其他語言上。
這裡只處理 text 與 integer 型態,text 轉成 wstring/string,integer 則轉為 int。轉換的工具是使用 awk/sed。
只要稍稍改動一下,也適用在其他語言上。
#!/bin/bash
if test -z "$1"
then
echo "You need to specify the database file name."
exit -1
fi
db=$1
dbname=`basename $1 .db`
tables=`sqlite3 $db ".table"`
output="db_${dbname}_schema.h"
touch $output
cat >> $output << EOF
#ifndef __db_${dbname}_schema_h__
#define __db_${dbname}_schema_h__
#include <string>
#ifdef _UNICODE
typedef std::wstring db_string;
#else
typedef std::string db_string;
#endif
EOF
for table in $tables
do
echo "typedef struct {" >> $output
sqlite3 $db ".schema $table" | awk '/^\ /{printf("%s %s;",$2,$1);}' | sed -e 's/,/\ /g' | sed -e 's/text/db_string /g' | sed -e 's/integer/int /g' >> $output
struct_name=`echo $table | awk -f cap.awk`
echo "}$struct_name;" >> $output
echo "" >> $output
done
cat >> $output << EOF
#endif
EOF
# call "astyle" to format the code. Beside "astyle", you can use "indent".
astyle $output
exit 0
訂閱:
文章 (Atom)