for 루프의 tar 명령이 잘못된 번호로 시작됩니다.

for 루프의 tar 명령이 잘못된 번호로 시작됩니다.

backup/에는 다음 파일이 있습니다.

ubuntu@ip-172-31-8-46:~/$ ls backup/
itrpl_dsm_10_1.tif  itrpl_dsm_12_3.tif  itrpl_dsm_2_3.tif  itrpl_dsm_4_3.tif  itrpl_dsm_6_3.tif  itrpl_dsm_8_3.tif
itrpl_dsm_10_2.tif  itrpl_dsm_12_4.tif  itrpl_dsm_2_4.tif  itrpl_dsm_4_4.tif  itrpl_dsm_6_4.tif  itrpl_dsm_8_4.tif
itrpl_dsm_10_3.tif  itrpl_dsm_12_5.tif  itrpl_dsm_2_5.tif  itrpl_dsm_4_5.tif  itrpl_dsm_6_5.tif  itrpl_dsm_8_5.tif
itrpl_dsm_10_4.tif  itrpl_dsm_12_6.tif  itrpl_dsm_2_6.tif  itrpl_dsm_4_6.tif  itrpl_dsm_6_6.tif  itrpl_dsm_8_6.tif
itrpl_dsm_10_5.tif  itrpl_dsm_12_7.tif  itrpl_dsm_2_7.tif  itrpl_dsm_4_7.tif  itrpl_dsm_6_7.tif  itrpl_dsm_8_7.tif
itrpl_dsm_10_6.tif  itrpl_dsm_12_8.tif  itrpl_dsm_2_8.tif  itrpl_dsm_4_8.tif  itrpl_dsm_6_8.tif  itrpl_dsm_8_8.tif
itrpl_dsm_10_7.tif  itrpl_dsm_1_1.tif   itrpl_dsm_3_1.tif  itrpl_dsm_5_1.tif  itrpl_dsm_7_1.tif  itrpl_dsm_9_1.tif
itrpl_dsm_10_8.tif  itrpl_dsm_1_2.tif   itrpl_dsm_3_2.tif  itrpl_dsm_5_2.tif  itrpl_dsm_7_2.tif  itrpl_dsm_9_2.tif
itrpl_dsm_11_1.tif  itrpl_dsm_1_3.tif   itrpl_dsm_3_3.tif  itrpl_dsm_5_3.tif  itrpl_dsm_7_3.tif  itrpl_dsm_9_3.tif
itrpl_dsm_11_2.tif  itrpl_dsm_1_4.tif   itrpl_dsm_3_4.tif  itrpl_dsm_5_4.tif  itrpl_dsm_7_4.tif  itrpl_dsm_9_4.tif
itrpl_dsm_11_3.tif  itrpl_dsm_1_5.tif   itrpl_dsm_3_5.tif  itrpl_dsm_5_5.tif  itrpl_dsm_7_5.tif  itrpl_dsm_9_5.tif
itrpl_dsm_11_4.tif  itrpl_dsm_1_6.tif   itrpl_dsm_3_6.tif  itrpl_dsm_5_6.tif  itrpl_dsm_7_6.tif  itrpl_dsm_9_6.tif
itrpl_dsm_11_5.tif  itrpl_dsm_1_7.tif   itrpl_dsm_3_7.tif  itrpl_dsm_5_7.tif  itrpl_dsm_7_7.tif  itrpl_dsm_9_7.tif
itrpl_dsm_11_6.tif  itrpl_dsm_1_8.tif   itrpl_dsm_3_8.tif  itrpl_dsm_5_8.tif  itrpl_dsm_7_8.tif  itrpl_dsm_9_8.tif
itrpl_dsm_11_7.tif  itrpl_dsm_2_1.tif   itrpl_dsm_4_1.tif  itrpl_dsm_6_1.tif  itrpl_dsm_8_1.tif
itrpl_dsm_11_8.tif  itrpl_dsm_2_2.tif   itrpl_dsm_4_2.tif  itrpl_dsm_6_2.tif  itrpl_dsm_8_2.tif

실행시

ubuntu@ip-172-31-8-46:~/backup$ for (( i=1; i<=12; i++ )); do tar -czvf itrpl_dsm_$i.tar.gz itrpl_dsm_$i_*.tif; done

산출:

itrpl_dsm_10_1.tif
itrpl_dsm_10_2.tif
itrpl_dsm_10_3.tif
itrpl_dsm_10_4.tif
itrpl_dsm_10_5.tif
itrpl_dsm_10_6.tif
itrpl_dsm_10_7.tif
itrpl_dsm_10_8.tif
itrpl_dsm_11_1.tif
itrpl_dsm_11_2.tif
itrpl_dsm_11_3.tif
itrpl_dsm_11_4.tif
itrpl_dsm_11_5.tif
itrpl_dsm_11_6.tif
itrpl_dsm_11_7.tif
itrpl_dsm_11_8.tif
itrpl_dsm_12_3.tif
itrpl_dsm_12_4.tif
itrpl_dsm_12_5.tif
itrpl_dsm_12_6.tif
itrpl_dsm_12_7.tif
itrpl_dsm_12_8.tif

내 의도는 itrpl_dsm_1_*.tif 파일에서 itrpl_dsm_1.tar.gz를 생성하는 것이었지만 분명히 내가 원하는 것을 달성하지 못했습니다.

답변1

${i}not 을 사용해야 합니다 $i. 즉

for (( i=1; i<=12; i++ )); do tar -czvf itrpl_dsm_$i.tar.gz itrpl_dsm_${i}_*.tif; done

bash는 name 변수를 찾았 i_지만 해당 이름을 가진 변수가 없으므로 아무것도 대체되지 않습니다.

따라서 패턴은 가 되고 itrpl_dsm_*.tif와일드 *카드는 확장되어 패턴과 일치하는 모든 파일로 대체됩니다.

관련 정보