
저는 bash 코드를 작업 중인데 CheckOtherFiles
파일 ( $1
)이 file 에서 더 최신인지 확인하는 데 사용되는 함수를 발견했습니다 $WS_ROOT/$NomeVista/COMP/COMP_COMPILE
.
CheckOtherFiles
절대 경로가 있는 파일인 하나의 매개변수로 호출됩니다.
코드는 다음과 같습니다.
function CheckOtherFiles {
fver=$1
if [[ -r $fver ]]
then
if [[ -r $WS_ROOT/$NomeVista/COMP/COMP_COMPILE ]]
then
filecheck=`/bin/ls -1t $fver $WS_ROOT/$NomeVista/COMP/COMP_COMPILE | head -1`
if [[ ${filecheck##*/} = ${fver##*/} ]]
then
return 1
fi
fi
fi
return 0
}
bash 매뉴얼을 읽고 찾았습니다.이것유용한 링크이지만 다음의 의미가 불분명합니다.
if [[ ${filecheck##*/} = ${fver##*/} ]]
나는 또한 거기에 오류가 있는지 의심합니다.
그렇다면 이 코드 조각의 의미를 이해하도록 도와주실 수 있나요?
또한 이 기능을 개선하기 위한 어떤 제안이라도 진심으로 감사드립니다.
답변1
연결된 질문에 대답했듯이 이것은하위 문자열 제거:
${string##substring}
Deletes longest match of $substring from front of $string.
이 경우 경로를 제거하고 경로만 유지합니다.basename
:
$ FILE=/etc/default/google-chrome
$ echo ${FILE##*/}
google-chrome
$ basename $FILE
google-chrome
일반적으로 저는 를 고수하겠지만 basename
, 쓰기 전용 코드가 더 빠르다면 더 나은지는 의견의 문제입니다. 어쨌든 나는 그것에 오류가 없다고 본다.
그러나 다음을 고려할 수 있습니다.-nt
그리고 -ot
테스트:
f1 -nt f2
file f1 is newer than f2
f1 -ot f2
file f1 is older than f2
그리고 아마도 다음과 같이 단순화할 수 있습니다.
other_file="$WS_ROOT/$NomeVista/COMP/COMP_COMPILE"
if [[ -r $fver && -r $other_file && ( $fver -nt $otherfile ) ]]
then
return 1
fi
return 0