여러 텍스트를 반복하기 위한 쉘 스크립트 에코 텍스트 이름

여러 텍스트를 반복하기 위한 쉘 스크립트 에코 텍스트 이름

나는 이것을하려고 노력하고있다

text1="word1 word2 word3"
text2="word4 word5"
text1="word6 word7 word8"

for var in $text1 $text2 $text3
do
  echo $var" in "(__?__)
done

예상 출력

word1 in text1
word2 in text1
...
word4 in text2
...
word8 in text3
  1. 스크립트는 대시 ->로 실행되므로 bashism은 허용되지 않습니다.
  2. 쉘은 텍스트 처리 도구가 아니라는 것을 알고 있습니다.
  3. 루프는 반복하기 전에 $text1 $text2 $text3을 연결합니까?

답변1

text1="word1 word2 word3"
text2="word4 word5"
text3="word6 word7 word8"
set -f #disable globbing in unquoted var expansions (optional) 
for i in text1 text2 text3; do
    eval "j=\$$i" #i holds name, $j holds the fields
    for k in $j; do #k holds a field
        echo "$k in $i"
    done
done

산출:

word1 in text1
word2 in text1
word3 in text1
word4 in text2
word5 in text2
word6 in text3
word7 in text3
word8 in text3

관련 정보