顯示具有 shell script 標籤的文章。 顯示所有文章
顯示具有 shell script 標籤的文章。 顯示所有文章

星期六, 4月 07, 2012

使用 Shell Script 進行 URL 編/解碼

一段時間之前,我寫了個利用 GNU coreutils 中的 "printf" 程式來進行 URL 解碼的 script 叫作 "urldecode"。今天本來想寫個使用 tinyurl.com 來產生短網址的 script,不過這樣的 script 必須要用到 URL 編碼來把傳給 tinyurl 的網址資料先編碼才行。在查了維基百科的 "Percent-encoding" 頁面之後,我寫了個用來做 URL 編碼的 script 叫 "urlencode


首先來看解碼的部份。解碼是相對簡單的事情,因為 "printf" 接受 "\xHH" 這樣的格式字串("HH" 是一位或兩位數的16位元數值),所以只要先把編碼字串中的 '%' 換成 "\x" 字串後,再整個丟給 "printf" 即可。以下是我的實作:


#!/bin/bash
#
# urldecode - decoding the URL-encoded string
#
# (C)2010 Shang-Feng Yang <storm_DOT_sfyang_AT_gmail_DOT_com>
#
# License: GPLv3

ENC_STR=$@
[ "${ENC_STR}x" == "x" ] && {
TMP_STR="$(cat - | sed -e 's/%/\\x/g')"
} || {
TMP_STR="$(echo ${ENC_STR} | sed -e 's/%/\\x/g')"
}
PRINTF=/usr/bin/printf
exec ${PRINTF} "${TMP_STR}\n"

"urldecode" 程式接受由標準輸入串流中或從命令參數中傳入目標字串。不過這個程式有個明顯的問題,那就是因為是把前處理過的字串整個丟給 "printf" 當作格式字串,所以當輸入字串太長時,程式會發生錯誤。

至於編碼的部份就相對比較麻煩了。我最初的想法是掃描輸入字串找出保留字元,把這些字元轉換後,再代換進輸入字串中。為了做字元轉換,我寫了個叫作 "char2hex" 的 script 來把目標字元轉換成對應的 ASCII 碼的 16 進位數值:


#!/bin/bash
#
# char2hex - returning the hexadecimal value of the given characters
#
# (C)2012 Shang-Feng Yang <storm_DOT_sfyang_AT_gmail_DOT_com>
#
# License: GPLv3

function usage() {
echo -e "Usage:\n"
echo -e "\t$(basename $0) CHARACTER(S)_TO_CONVERT\n"
}

CHAR=$1

[ "x${CHAR}" == "x" ] && { usage; exit 1; }

echo -n "${CHAR}" | od -A n -t x1 | tr -d ' '

這個 script 的運作方式相當直觀,唯一需要稍微提一下的是 "echo" 需要加上 '-n' 選項的原因。"echo" 預設會在輸出的最後加上一個換行字元,所以如果不加 '-n' 選項的話,輸出的結果在結尾會多個 "0a" 在後面。加上 '-n' 選項會關閉這個預設行為。

這種方法似乎相當簡單,但是實際上實作可能就不是那麼容易了。也許是我太笨想不到解法,不過像是光要從輸入字串中挑出保留字元,我就想不到有什麼簡單又不會因為大幅度增加 I/O 導致程式跑得很沒效率的方法。對像我這種懶惰的人來說,為了這個不是主要目的的問題想破頭,顯然不是個可行的辦法。

在看過維基百科 "Percent-encoding" 頁面的 "Percent-encoding reserved characters" 和 "Percent-encoding the percent character" 兩個段落後,發現必須進行編碼的保留字元其實並不多,所以最直接簡單的辦法是使用相對無腦的『列表法』來實作:


#!/bin/bash
#
# urlencode - escaping the reserved characters using URL-encoding
#
# (C)2012 Shang-Feng Yang <storm_DOT_sfyang_AT_gmail_DOT_com>
#
# License: GPLv3

STR=$@
[ "${STR}x" == "x" ] && { STR="$(cat -)"; }

echo ${STR} | sed -e 's| |%20|g' \
-e 's|!|%21|g' \
-e 's|#|%23|g' \
-e 's|\$|%24|g' \
-e 's|%|%25|g' \
-e 's|&|%26|g' \
-e "s|'|%27|g" \
-e 's|(|%28|g' \
-e 's|)|%29|g' \
-e 's|*|%2A|g' \
-e 's|+|%2B|g' \
-e 's|,|%2C|g' \
-e 's|/|%2F|g' \
-e 's|:|%3A|g' \
-e 's|;|%3B|g' \
-e 's|=|%3D|g' \
-e 's|?|%3F|g' \
-e 's|@|%40|g' \
-e 's|\[|%5B|g' \
-e 's|]|%5D|g'

"urlencode" 的內容相當簡單不需要多做解釋。這個 script 同樣可以使用 STDIN 資料流和命令列參數的方式來傳入輸入字串。下面是使用這兩個 script 的一些範例:


$ urlencode http://en.wikipedia.org/wiki/Percent-encoding
http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPercent-encoding
$ echo 'http://en.wikipedia.org/wiki/Percent-encoding' |urlencode
http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPercent-encoding
$ urldecode $(urlencode http://en.wikipedia.org/wiki/Percent-encoding)
http://en.wikipedia.org/wiki/Percent-encoding
$ urlencode http://en.wikipedia.org/wiki/Percent-encoding |urldecode
http://en.wikipedia.org/wiki/Percent-encoding

註:由於應 blogger 要求更新了 blogger 底層的 template,所以上面的程式碼和終端機區塊的排版有點問題...我未來如果有足夠動力的話也許會去改 CSS 把這個問題修好...

繼續閱讀全文

星期日, 10月 30, 2011

ann2srt v0.3

出乎意料之外的,ann2srt v0.2 的英文發布網誌得到了不少使用者回應。反觀中文版的對應發布文章反而靜悄悄...

言歸正傳,感謝在英文網誌上回應並協助測試的讀者 L,ann2srt script 修正了在某些英文註解中會遇到的特殊狀況(例如註解中有換行字元和半形逗號),以及在 Cygwin 環境下因為混用 DOS 與 UNIX 格式的換行字元造成程式執行錯誤的問題,因此釋放出 0.3 版。雖然這些修正其實在上週就完成了,且在上週末經過測試確認可以在 Linux 和 Cygwin 下執行無誤,不過實在是太懶惰了,所以一直沒有正式釋放出來...


廢話不多說,因為也想不出有啥廢話可以繼續扯下去,所以以下就是 v0.3 的原始碼:


#!/bin/bash
#
# Convert the youtube annotation into SRT subtitle
#
# By Shang-Feng Yang
# Version: 0.3
# License: GPL v3
#
# Changelog:
# * v0.3 (Oct/19/2011):
# - Fix the parsing errors caused by comma and newline characters in
# some English annotations
# - Adding transparent dos2unix conversion for compatibility under Cygwin
# * v0.2 (Jan/19/2011):
# - Sort the annotations using the "begin" time as key
# - Minor bugs fixing
# * v0.1 (Dec/7/2010):
# - Initial release


ANN=$1
SRT=$(basename ${ANN} .xml).srt
IFS=$'\n'
I=0

function usage() {
echo -e "Usage:\n"
echo -e "\t$(basename $0) ANNOTATION_FILE\n"
}

function parseXML() {
cat ${ANN} | tr -d '\r' |tr '\n' ' ' | xmlstarlet sel -t -m 'document/annotations/annotation' -v 'TEXT' -o '|' -m 'segment/movingRegion/rectRegion' -v '@t' -o '|' -b -n | tr -d '\r'
}

function reformatTime() {
local H=$(echo $1 |cut -d ':' -f 1)
local M=$(echo $1 |cut -d ':' -f 2)
local S=$(echo $1 |cut -d ':' -f 3)
printf '%02d:%02d:%06.3f' ${H} ${M} ${S} |tr '.' ','
}

function time2sod() {
# Convert time in HH:MM:SS.SSS format into second-of-the-day value
local SOD=$(echo $1 | awk -F ":" '{printf("%f\n", $1*3600+$2*60+$3);}')

echo ${SOD}
}

[ "x${ANN}" = "x" ] && { usage; exit 1; }
[ -f ${ANN} ] || { usage; exit 1; }
[ -f ${SRT} ] && rm ${SRT}
[ -f ${SRT}.tmp ] && rm ${SRT}.tmp

for LINE in $(parseXML); do
C=$(echo ${LINE} |cut -d '|' -f 1)
B=$(echo ${LINE} |cut -d '|' -f 2)
E=$(echo ${LINE} |cut -d '|' -f 3)
echo "$(time2sod ${B})#${B}#${E}#${C}" >> ${SRT}.tmp
done

grep "###" ${SRT}.tmp && {
echo "\"${ANN}\" has no valid annotation!" >&2
rm ${SRT}.tmp
exit 1
}

for LINE in $(cat ${SRT}.tmp|sort -n -t '#'); do
(( I++ ))
C=$(echo ${LINE} |cut -d '#' -f 4)
B=$(reformatTime $(echo ${LINE} |cut -d '#' -f 2))
E=$(reformatTime $(echo ${LINE} |cut -d '#' -f 3))
echo -e "${I}\n${B} --> ${E}\n${C}\n" >> ${SRT}
done

rm ${SRT}.tmp


為了避免因為複製貼上造成的錯誤,所以以上程式也可以從這邊下載:
http://dl.dropbox.com/u/1382119/tmp/ann2srt

基本上 v0.3 和 v0.2 比起來,並沒有太多的改變,只有為了避開註解中出現半形逗號的狀況,所以把本來當作中介格式的 CSV 格式的分隔符號,由逗號改成了 "|" 符號,並且使用 tr 移除沒必要的換行字元和把 DOS 的換行字元轉換成 UNIX 格式。由於 DOS 轉 UNIX 格式實際上只是摻除 carrier return 字元 ("\r" 字元),所以這樣的修改並不會影響在 Linux 下的執行。

繼續閱讀全文

星期四, 1月 20, 2011

ann2srt v0.2

前幾天偶然間在某個電子佈告欄上,看到有人引用了我之前寫的『下載並轉換 Youtube 的 annotation 至 SRT 格式』的 bash script 以及些許文章內容,不過因為該篇文 blog 中發表的 0.1 版是相當簡略的版本,沒有處理時間排序、字幕顏色與位置等問題,並且有一點小小的錯誤,所以昨天花了點時間加上排序的功能,並修正了些錯誤。


這次的修正讓 ann2srt 多用了 awk 以及 sort 做排序的處理。sort 是 GNU coreutils 的一部分,awk/gawk 通常大部分的系統也都預設會安裝,所以應該不會有問題。除此之外,用到的外部程式與 v0.1 一樣。排序的部分,排序的依據是使用該註解的『開始』時間,將所有註解的『開始』時間轉換為以秒為單位後進行排序。

以下是 v0.2 的程式碼:

#!/bin/bash
#
# Convert the youtube annotation into SRT subtitle
#
# By Shang-Feng Yang <storm_DOT_sfyang_AT_gmail_DOT_com>
# Version: 0.2
# License: GPL v3
#
# Changelog:
# * v0.2 (Jan/19/2011):
# - Sort the annotations using the "begin" time as key
# - Minor bugs fixing

function usage() {
echo -e "Usage:\n"
echo -e "\t$(basename $0) ANNOTATION_FILE\n"
}

function parseXML() {
cat ${ANN} |xmlstarlet sel -t -m 'document/annotations/annotation' -v 'TEXT' -o ',' -m 'segment/movingRegion/rectRegion' -v '@t' -o ',' -b -n
}

function reformatTime() {
local H=$(echo $1 |cut -d ':' -f 1)
local M=$(echo $1 |cut -d ':' -f 2)
local S=$(echo $1 |cut -d ':' -f 3)
printf '%02d:%02d:%06.3f' ${H} ${M} ${S} |tr '.' ','
}

function time2sod() {
# Convert time in HH:MM:SS.SSS format into second-of-the-day value
local SOD=$(echo $1 | awk -F ":" '{printf("%f\n", $1*3600+$2*60+$3);}')

echo ${SOD}
}

ANN=$1
SRT=$(basename ${ANN} .xml).srt
IFS=$'\n'
I=0

[ "x${ANN}" = "x" ] && { usage; exit 1; }
[ -f ${ANN} ] || { usage; exit 1; }
[ -f ${SRT} ] && rm ${SRT}
[ -f ${SRT}.tmp ] && rm ${SRT}.tmp

for LINE in $(parseXML); do
C=$(echo ${LINE} |cut -d ',' -f 1)
B=$(echo ${LINE} |cut -d ',' -f 2)
E=$(echo ${LINE} |cut -d ',' -f 3)
echo "$(time2sod ${B})#${B}#${E}#${C}" >> ${SRT}.tmp
done

grep "###" ${SRT}.tmp && {
echo "\"${ANN}\" has no valid annotation!"
rm ${SRT}.tmp
exit 1
}

for LINE in $(cat ${SRT}.tmp|sort -n -t '#'); do
(( I++ ))
C=$(echo ${LINE} |cut -d '#' -f 4)
B=$(reformatTime $(echo ${LINE} |cut -d '#' -f 2))
E=$(reformatTime $(echo ${LINE} |cut -d '#' -f 3))
echo -e "${I}\n${B} --> ${E}\n${C}\n" >> ${SRT}
done

rm ${SRT}.tmp


使用方法與注意事項依然與 v0.1 相同。

繼續閱讀全文

星期三, 12月 08, 2010

下載並轉換 Youtube 的 annotation 至 SRT 格式

很久沒寫網誌了,尤其是英文版的。基本上沒有太多太值得寫文章紀錄的事情,而且我實在太懶了,有些本來想寫的,拖太久以後也就算了。所以現在比較常在 Google Buzz 上寫簡單的紀錄就算了。

廢話說完,進入正題。Youtube 上越來越多人不用他的 caption (字幕)功能來加字幕,反而喜歡用 annotation (浮動註釋)功能來做。網路上其實已經有很多線上或離線工具可以用來下載 Youtube 影片或字幕的工具,像是 get_flash_videos、clive、youtube-dl、Google2SRT、Youtube Subtitle Ripper等等,但是對於如何下載並轉換 annotation 的資訊則很少見。不過今天我找到解決方案了。


首先,我從這篇文章的使用者評論中,知道了怎樣下載 annotation。我也寫了個簡單的 bash script 用 wget 來下載 caption 和 annotation,不過那個 script 實在是太簡單了,所以就不紀錄了。下載回來的 annotation 是 XML 格式的,所以接下來的問題就是如何轉換成一般字幕格式。

現在大部分的播放器都支援許多純文字格式字幕,而轉換的方法在 Google2SRT 的程式碼中應該可以找到,不過我還是自己寫了個 bash script 來把 annotation 由 XML 格式轉換到最簡單的 SRT 字幕格式。

我寫的這個 bash script 稱為 ann2srt,使用 XMLStarlet 來解析 XML。除此之外,其他就只有用到 bash 內建指令,以及一些 coreutils 中的工具,像是 cuttr。由於 XML 格式的 annotations 並沒有照時間順序存檔,所以目前轉換出來的 SRT 字幕自然也沒有照時間順序排列,因此可能在某些播放器上會有相容性的問題。雖然加上排序的功能並不是很難,但是因為 mplayer 顯然可以正確處理亂序的 SRT 字幕,所以就先這樣吧!ann2srt 的程式碼如下:


#!/bin/bash
#
# Convert the youtube annotation into SRT subtitle
#
# By Shang-Feng Yang <storm_dot_sfyang_at_gmail_dot_com>
# Version: 0.1
# License: GPL v3

function usage() {
echo -e "Usage:\n"
echo -e "\t$(basename $0) ANNOTATION_FILE\n"
}

function parseXML() {
cat ${ANN} |xmlstarlet sel -t -m 'document/annotations/annotation' -v 'TEXT' -o ',' -m 'segment/movingRegion/rectRegion' -v '@t' -o ',' -b -n
}

function reformatTime() {
H=$(echo $1 |cut -d ':' -f 1)
M=$(echo $1 |cut -d ':' -f 2)
S=$(echo $1 |cut -d ':' -f 3)
printf '%02d:%02d:%02.3f' ${H} ${M} ${S} |tr '.' ','
}

ANN=$1
SRT=$(basename ${ANN} .xml).srt
IFS=$'\n'
I=0

[ -f ${ANN} ] || { usage; exit 1; }
[ -f ${SRT} ] && rm ${SRT}

for LINE in $(parseXML); do
(( I++ ))
C=$(echo ${LINE} |cut -d ',' -f 1)
B=$(echo ${LINE} |cut -d ',' -f 2)
E=$(echo ${LINE} |cut -d ',' -f 3)
echo -e "${I}\n$(reformatTime ${B}) --> $(reformatTime ${E})\n${C}\n" >> ${SRT}
done


mplayer 的使用者請注意:播放時請使用 -ass 選項開啟 SSA/ASS 支援。由於 annotation 的特性所至,在同一時段可能會有不只一個 annotation 顯示在畫面上,而 mplayer 內建的 SRT 解析器遇到這種狀況時會只顯示其中一個,但使用 -ass 的話,所有時段重疊的 annotation 都會顯示出來。

SRT 是相當簡單的字幕格式,不支援任何特效,所以 annotation 的位置及顏色等資訊無法也一併轉換。如果我接下來有改版的話,大概會改成轉換成 SSA/ASS 格式吧...

繼續閱讀全文

星期六, 2月 02, 2008

使用 wmctrl 來旋轉 compiz 的桌面方塊


Compiz 或 Compiz-Fusion 的 Cube plugin 是個相當有趣的特效 plugin,透過 Cube 以及 Rotate plugin,桌面可以虛擬成一個立體方塊,方塊四周的4個面變成 4 個虛擬桌面,這個我在『我也來貼圖 - Linux 的 3D 桌面』中有提過。旋轉立體方塊可以透過設定好的滑鼠或者鍵盤快速鍵來達成,這在一般的使用狀態下已經足夠了,但假設想要寫 script 來控制時,那該怎麼做呢?

要使用 script 來控制 Cube 的旋轉,方法其實很多,例如透過 Compiz 提供的 DBus 物件來達成。除此之外,方便好用的 wmctrl 雖然沒有直接支援 Cube 的旋轉,但透過一些分析,其實還是可以用 wmctrl 間接控制 Cube 的旋轉。


為了能夠用 wmctrl 控制 Cube 的旋轉,首先我們必須先看看到底 Compiz 是怎麼處理 Cube 的。以我的系統為例,我使用 Ubuntu 7.10 中的 Compiz-Fusion,螢幕解析度設定為 1024×768,先看看在 Cube 的第一個面時,使用 "wmctrl -d" 會看到怎樣的訊息:

$ wmctrl -d
0 * DG: 4096x768 VP: 0,0 WA: 0,25 1024x718 N/A

wmctrl -d 輸出的資訊裡,"DG" 是桌面大小 (Desktop Geometry),"VP" 是 Viewport 座標 (Viewport Position),WA 則是工作區域 (WorkArea) 的座標及大小,最後則是 Desktop 的名稱。從上面的資訊看起來,Compiz 實際的 Cube 實際上只有管理一個寬度很寬的 Desktop 而已,那我們看到的各個面到底是怎麼回事呢?為了搞清楚狀況,將 Cube 向右旋轉到第二面以後,再次以 wmctrl 來看看 Desktop 資訊:

$ wmctrl -d
0 * DG: 4096x768 VP: 1024,0 WA: 0,25 1024x718 N/A

發現了嗎?切換到不同面時,wmctrl -d 顯示的資訊裡,唯一改變的是 Viewport 的位置!換句話說,Compiz 的 Cube 實際上只有管理一個很寬的桌面,然後各個面其實只是不同的 Viewport 而已!這也解釋了為何應用程式視窗可以跨過兩個相鄰面的邊界了!既然知道 Compiz 實際上是這樣管理 Cube 的,那要用 wmctrl 來達成 Cube 的旋轉,其實就只是使用 wmctrl 來切換 Viewport 位置即可!

我寫了個簡單的 BASH script 來做這件事情:

#!/bin/bash
#
# compiz-rotate-wmctrl - Rotate the cube using wmctrl
#
# Author: Shang-Feng Yang
# Released under GPLv3

VER="1.0"


function rotate() {
  # The target face number (begins with 0)
  TVPN=$(( $1 % ${NF} ))

  # The X coordinate of the target viewport
  TVPX=$(( ${TVPN} * ${WW} ))

  # Change to the target viewport
  wmctrl -o ${TVPX},0
}

function usage() {
  echo -e "$(basename $0) v${VER}\n"
  echo -e "Usage:\n"
  echo -e "\t$(basename $0) {left|right|#}\n"
  echo -e "\tWhere:\n"
  echo -e "\t\tleft - rotate the cube to the left"
  echo -e "\t\tright - rotate the cube to the right"
  echo -e "\t\t# - rotate to #th face (begins with 0)\n\n"
  echo -e "Author: Shang-Feng Yang <storm dot sfyang at gmail dot com>"
  echo -e "Released under GPLv3"
}

# The action to be performed. $ACT could be 'left' or 'right' to rotate
# left or right, accordingly. $ACT could also be the number of the face
# to rotate into.
ACT=$(echo $1 |tr '[A-Z]' '[a-z]')

[ "x$ACT" == "x" ] && { usage; exit 1; } || {
  case $ACT in
    left|right|[0-9]|[0-9][0-9])
      ;;
    *)
      usage
      exit 1
      ;;
  esac
}


# The informations about the desktop
INFO=$(wmctrl -d)
# The width of the desktop
DW=$(echo "${INFO}"| awk '{sub(/x[0-9]+/, "", $4); print $4}')
# The width of the workarea
WW=$(echo "${INFO}"| awk '{sub(/x[0-9]+/, "", $9); print $9}')
# The number of faces on the cube
NF=$(($DW/$WW))
# The X coordinate of the viewport
CVPX=$(echo "${INFO}" |awk '{sub(/,[0-9]+/, "", $6); print $6}')
# Current number of the face in all faces (begins with 0)
CVPN=$(( ${CVPX} / ${WW} ))

[ "$ACT" == "right" ] && {
  ACT=$(( ${CVPN} + 1 ))
} || {
  [ "$ACT" == "left" ] && {
    ACT=$(( ${CVPN} - 1 ))
  }
}

rotate ${ACT}


使用方式如下:

  • 不給任何參數,或者參數給錯時時,會顯示簡單的使用說明:

    $ compiz-rotate-wmctrl
    compiz-rotate-wmctrl v1.0

    Usage:

    compiz-rotate-wmctrl {left|right|#}

    Where:

    left - rotate the cube to the left
    right - rotate the cube to the right
    # - rotate to #th face (begins with 0)


    Author: Shang-Feng Yang <storm dot sfyang at gmail dot com>
    Released under GPLv3


  • 使用 left 將 Cube 向左旋轉一個面:

    $ compiz-rotate-wmctrl left


  • 使用 right 將 Cube 向右旋轉一個面:

    $ compiz-rotate-wmctrl right


  • 直接給定某個面的編號時,會直接旋轉至該面,編號由 0 開始:

    $ compiz-rotate-wmctrl 3




這樣的 script 可以用在什麼地方呢?通常是用在一些特殊場合,例如透過非正規方式控制電腦時,像是透過網路遙控,或者使用觸控螢幕時,使用 script 會比較方便。

繼續閱讀全文

星期三, 1月 02, 2008

測試編碼轉換用的 BASH script

最近因為某些狀況,雖然有一些編碼偵測的工具可以用,例如 Mozilla 計畫的 Charset DectorJserv 兄的 charset-dector 等等,但某些情況下並不適用。所以就希望能有測試所有編碼轉換的方法。

所幸 UN*X 的 Shell Script 的威力強大,加上 glibc 提供的 iconv 工具程式,讓要達到測試多種編碼轉換,變得相當容易。雖然直接用 command line 下指令也可以,不過我覺得還是寫成 script 方便重複使用,會是比較好的方式。以下我稱之為 convtest 的 BASH script,就是我之前寫的測試用工具。


#!/bin/bash
#
# convtest:: 編碼轉換測試工具
#
# Shang-Feng Yang <storm dot sfyang at gmail dot com>
#
# License: GPL

TOENC="utf8"

STR="$1"

[ "x${STR}" == "x" ] &&{
INPUT="$(cat - |sed -e 's/\n/\\n/g')";} || {
INPUT=${STR}; }

for FROMENC in $(iconv -l | sed -e 's|\(.\+\)/.*/.*|\1|g'); do
    echo "Encoding=$FROMENC"
    echo -e "${INPUT}" | iconv -c -f ${FROMENC}\
     -t ${TOENC} 2&gt;/dev/null
    echo -e '\n\n'
done


這個 script 還有很多可以改進的地方,像是輸出編碼的自動偵測等等,不過通常這種測試,在非 Unicode 的 locale 下並沒有太大的意義。使用方式很簡單,可以直接把要轉換的字串當作參數傳入:


$ convtest &lt;測試字串&gt; | less


也可以把要轉換的內容用管道以輸出導向的方式傳入:


$ cat target.txt | convtest |less


繼續閱讀全文