"읽기"의 단어를 분할하여 배열로 저장하시겠습니까?

"읽기"의 단어를 분할하여 배열로 저장하시겠습니까?

입력을 어떻게 받고 read, 단어를 공백으로 나누고, 해당 단어를 배열에 넣으려면 어떻게 해야 합니까?

내가 원하는 것은:

$ read sentence
this is a sentence
$ echo $sentence[1]
this
$ echo $sentence[2]
is
(and so on...)

나는 그것을 텍스트 모험을 위한 영어 문장을 처리하는 데 사용합니다.

답변1

이 명령을 사용하는 경우 bash해당 read명령 에 대한 옵션이 있습니다 -a.

~에서help read

Options:
  -a array  assign the words read to sequential indices of the array
        variable ARRAY, starting at zero

그래서

$ read -a s
This is a sentence.

결과 배열의 인덱스는 0이므로

$ echo "${s[0]}"
This
$ echo "${s[1]}"
is
$ echo "${s[2]}"
a
$ echo "${s[3]}"
sentence.
$ 

답변2

@steeldriver와 비슷한 답글

#!/bin/bash
printf "Input text:" && read UI ;
read -a UIS <<< ${UI} ;
X=0 ;
for iX in ${UIS[*]} ; do printf "position: ${X}==${iX}\n" ; ((++X)) ; done ;

관련 정보