![Bash는 하나의 요소만 사용해야 하는 배열을 확장합니다.](https://rvso.com/image/1482480/Bash%EB%8A%94%20%ED%95%98%EB%82%98%EC%9D%98%20%EC%9A%94%EC%86%8C%EB%A7%8C%20%EC%82%AC%EC%9A%A9%ED%95%B4%EC%95%BC%20%ED%95%98%EB%8A%94%20%EB%B0%B0%EC%97%B4%EC%9D%84%20%ED%99%95%EC%9E%A5%ED%95%A9%EB%8B%88%EB%8B%A4..png)
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`)
파일 이름에 공백이 포함되어 있으면 깨질 수 있습니다.