2013-01-20 5 views
-1

이 코드에서 excludespec 변수는 부분 문자열과 모두 일치 할 것으로 예상되지만 정확한 표현을 사용하는 tar 명령을 전달하는 대신 실제 실행 파일과 일치하는 실제 파일을 평가하는 것처럼 보입니다.bash 스크립트는 어떻게 파일 glob을 확장합니까?

excludespec=${PWD##*/}\_$USER\_`hostname`.bkcd_backup* 

최종 결과는 아카이브가 제외 목록과 일치하지 않는 생성되는 그래서 타르 출력 :

tar: .: file changed as we read it

그것이 내가 모르는 무언가를 정의하는 문자열인가?

#!/bin/sh 
# bkcd - backup current directory in situ 

DMY_M() { 
    date +%Y%m%d_%H%M 
} 

timestring=$(DMY_M) 
echo `pwd` - $timestring > .bkcdspec 
filename=${PWD##*/}\_$USER\_`hostname`.bkcd_backup.$timestring.tar.gz  
excludespec=${PWD##*/}\_$USER\_`hostname`.bkcd_backup* 
fullexclude="$excludespec"tar.gz  
echo excludespec - $excludespec 
echo filename - $filename 
echo fullexclude - $fullexclude  
tar -cpzf $filename --exclude=$fullexclude . 
rm .bkcdspec 
+1

당신은 할 필요가 없습니다 [같은 질문을 두 번 (http://superuser.com/q/539173/4714) –

답변

1

대체 (globbing)을 억제하는 일반적인 트릭은 당신이하지 않는 경우 (큰 따옴표의 문자열 (당신이 원하는 경우에 역 따옴표 또는 변수가 확장 될) 또는 작은 따옴표로 묶어야하는 것입니다 : 여기

코드입니다). 그래서 여기, 당신이 $excludespec을 제거하고 $fullexclude 설정 한 것 :

fullexclude="${PWD##*/}_${USER}_$(hostname).bkcd_backup*.tar.gz" 

과로 사용 : 일부 파일은 약 --exclude=...value-of-$fullexclude라는 이름의 한 일어난 경우, "$fullexclude" 주위에 따옴표없이 (

tar -cpzf "$filename" --exclude="$fullexclude" . 

, 그러한 파일을 가지고있을 가능성은 적지 만 문제가 발생하기 전에 문제를 해결할 수는 있습니다.)

디버깅을 위해 에코를 표시 할 때 globbing; 다시 따옴표는 :

echo "filename=$filename" 
echo "fullexclude=$fullexclude" 

그렇지 않으면, echo 당신에게 한 번 더 혼란, 이름에 글로브 확장을 수행합니다. 함께 변경 사항을 퍼팅

가 리드 :

#!/bin/sh 
# bkcd - backup current directory in situ 

timestring=$(date +%Y%m%d_%H%M) 
echo "$(pwd) - $timestring" > .bkcdspec 
prefix="${PWD##*/}_${USER}_$(hostname).bkcd_backup" 
filename="$prefix.$timestring.tar.gz" 
fullexclude="$prefix.*.tar.gz" 
echo "filename - $filename" 
echo "fullexclude - $fullexclude" 
tar -cpzf "$filename" --exclude="$fullexclude" . 
rm .bkcdspec 
관련 문제