-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.sh
61 lines (47 loc) · 2.03 KB
/
encode.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
#################################################################
# #
# 文字列をURLエンコーディングするシェルスクリプト #
# from SoftwareDesign 393号 使えるシェルスクリプトの書き方 #
# #
#################################################################
# グローバル変数
result=""
# 関数定義
array_to_encoded_strings(){
# evalコマンドで配列を一つの文字列として変数termsに格納する
eval terms=(\${$1[@]})
i=0
# スクリプトに与えた引数の数(#terms[@])分ループ処理を行う
# 比較演算子はそのまま書くとエラーになるため、文字列展開されないシングルクオートで囲う
while [ $i '<' ${#terms[@]} ]
do
if [ $i != 0 ]
then
result="$result\n"
fi
# i番目の要素の値を変数termに格納する
term=${terms[$i]}
# 変数termに格納されている値をechoからnkfに渡し、
# UTF-8コード・MIME Quoted streamに変換して再度変数termに格納する
term=$(echo $term | nkf -W8MQ)
# 変数term値をsedに渡し、「s/」以降の文字列にマッチした部分を
# 次の「/」以降の文字列に全置換して、再度変数termに格納する
term=$(echo $term |
sed -e 's/=$//' \
-e 's/=/%/g' \
-e 's/%7E/~/g' \
-e 's/%5F/_/g' \
-e 's/%2D/-/g' \
-e 's/%2E/./g')
result="$result$term"
# 変数iを後置でインクリメントする
let i++
done
}
# 「$@」:すべての引数を個別で処理する
given_terms=($@)
# 関数を実行する
array_to_encoded_strings given_terms
# 変数resultを展開して、改行を出力する形で標準出力する
echo -e "$result"