Bash는 하나의 요소만 사용해야 하는 배열을 확장합니다.

Bash는 하나의 요소만 사용해야 하는 배열을 확장합니다.

1분보다 오래된 Raspberry Pi의 이미지를 가져오는 간단한 bash 스크립트는 다음과 같습니다.

#!/bin/bash

time=`date +"%FT%H_%M_%S"`
imagedir="/root/pilapse/images/"
files=`ssh pi find /home/pi/weatherPi-images/ -type f -mmin +1`

echo -e "\033[1;32mFetching images from Raspberry Pi\033[1;00m"

for currFile in "${files[@]}"; do
   echo -e "rsync -a --remove-source-files --info=progress2 -e ssh pi:$currFile $imagedir\n"
done

echo -e "rsync [...]"여기에는 디버깅을 위한 자리 표시자만 있습니다.

예상 출력:

rsync -a --remove-source-files --info=progress2 -e ssh pi:/home/pi/weatherPi-images/2016-02-01T13_22_20.jpg /root/pilapse/images/
rsync -a --remove-source-files --info=progress2 -e ssh pi:/home/pi/weatherPi-images/2016-02-01T13_14_07.jpg /root/pilapse/images/

실제 출력:

rsync -a --remove-source-files --info=progress2 -e ssh 

horizon:/home/pi/weatherPi-images/2016-02-01T13_22_20.jpg
    /home/pi/weatherPi-images/2016-02-01T13_14_07.jpg
    /home/pi/weatherPi-images/2016-02-01T13_18_45.jpg
    /home/pi/weatherPi-images/2016-02-01T13_13_37.jpg /root/pilapse/images/

어떻게 든 bash는 여기에서 배열을 확장하는 것 같지만 왜 그럴까요? 위의 foreach-loop에 대한 내 이해에는 currFile경로가 하나만 포함되어 있습니다.

답변1

files배열로 생성하지 않았습니다 . 배열에는 괄호가 필요합니다:

files=(`ssh pi find /home/pi/weatherPi-images/ -type f -mmin +1`)

파일 이름에 공백이 포함되어 있으면 깨질 수 있습니다.

관련 정보