1

以下のシェルスクリプトが必要です。

次のように14個の変数があります。

シナリオ 1 入力:

a#@#b#@#c#@#d#@#e#@#f#@#g#@#e#@#f#@#g#@#h#@#i#@#j#@#k#@#l#@#m#@#n

シナリオ 2 入力:

a#@#b#@#c#@#d#@#e#@#f#@#g#@#e#@#f#@#g#@#h#@#i#@#j#@#k#@#l#@#m#@#n#@#

次のように出力したい

op1 = a

op2 = b

op3 = c

op4 = d

op5 = e

op6 = g

op7 = f

op8 = h

op9 = i

op10 = j

op11 = k

op12 = l

op13 = m

op14 = n

ここop1op14、値を格納する必要がある変数があります。

4

1 に答える 1

3

最初に#@#単一の一意の区切り文字 (例: ) に置き換えてから#、それを使用readして配列に読み込みます。配列の要素には文字が含まれています。これを以下に示します。

$ input="a#@#b#@#c#@#d#@#e#@#f#@#g#@#e#@#f#@#g#@#h#@#i#@#j#@#k"
$ IFS='#' read -a arr  <<< "${input//#@#/#}"
$ echo ${arr[0]}
a
$ echo ${arr[1]}
b
$ echo ${arr[13]}
k

# print out the whole array
$ for (( i=0; i<${#arr[@]}; i++ ))
> do
>     echo "Element at index $i is ${arr[i]}"
> done
Element at index 0 is a
Element at index 1 is b
Element at index 2 is c
Element at index 3 is d
Element at index 4 is e
Element at index 5 is f
Element at index 6 is g
Element at index 7 is e
Element at index 8 is f
Element at index 9 is g
Element at index 10 is h
Element at index 11 is i
Element at index 12 is j
Element at index 13 is k
于 2012-12-14T09:25:26.387 に答える